(1) Write a java program to a number and check whether it is prime or not.
(2) Write a program using a function called area( ) to compute the area of a
i) circle(π R²) where PI= 3.14
ii) square(side × side)
iii) rectangle(length, width)
(3) Write a java program to input any number and check the number is Armstrong or not. For e.g. 153 = 1 x 3 + 5 x 3 + 3 x 3 = 1 +125 + 27 = 153
tanishqsingh:
Omg.!! So many programs in a single Ques,.:O :O .;'D
Answers
Answered by
1
For prime number check... You may find a bug . But the program works. I will attach the file as the code is note intended.
import java.util.Scanner;import java.util.Set;
public class Prime {
public int mainMenu(){
int choice=0;
Scanner in = new Scanner(System.in); System.out.println("\n|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|\n "); System.out.println("1. Check Number");
System.out.println("2. Exit"); System.out.println("\n|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|\n "); choice=in.nextInt(); if(choice==2){ System.out.println("Exited"); } return choice; } public void operations(int choiceOp){ int flag=0; int num; int back=0; int check=choiceOp; Scanner in = new Scanner(System.in); if(choiceOp!=2 ||choiceOp==1 ){ while(choiceOp==1){ System.out.println("Enter a number to check prime or not\n"); System.out.println(); num=in.nextInt(); for(int i=2;i<num;i++){ if(num % i == 0){ flag= 1; break; } } if(flag==1){ System.err.println("The number is not a prime number"); } if(flag==0){ System.out.println("The number is a prime number"); } System.out.println("\n|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|\n "); System.out.println("3.TRY AGAIN "); System.out.println("4.MAIN MENU"); System.out.println("\n|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\| "); back=in.nextInt();
if(back==3){ choiceOp=1; } else if(back==4){ int ret=mainMenu(); if(ret==2){ break; } } else{ System.out.println("Not a valid choice"); int ret=mainMenu(); if(ret==2){ break; } } }
} }
public static void main(String args[]){ Prime prime=new Prime(); int temp =prime.mainMenu(); prime.operations(temp); }}
import java.util.Scanner;import java.util.Set;
public class Prime {
public int mainMenu(){
int choice=0;
Scanner in = new Scanner(System.in); System.out.println("\n|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|\n "); System.out.println("1. Check Number");
System.out.println("2. Exit"); System.out.println("\n|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|\n "); choice=in.nextInt(); if(choice==2){ System.out.println("Exited"); } return choice; } public void operations(int choiceOp){ int flag=0; int num; int back=0; int check=choiceOp; Scanner in = new Scanner(System.in); if(choiceOp!=2 ||choiceOp==1 ){ while(choiceOp==1){ System.out.println("Enter a number to check prime or not\n"); System.out.println(); num=in.nextInt(); for(int i=2;i<num;i++){ if(num % i == 0){ flag= 1; break; } } if(flag==1){ System.err.println("The number is not a prime number"); } if(flag==0){ System.out.println("The number is a prime number"); } System.out.println("\n|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|\n "); System.out.println("3.TRY AGAIN "); System.out.println("4.MAIN MENU"); System.out.println("\n|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\| "); back=in.nextInt();
if(back==3){ choiceOp=1; } else if(back==4){ int ret=mainMenu(); if(ret==2){ break; } } else{ System.out.println("Not a valid choice"); int ret=mainMenu(); if(ret==2){ break; } } }
} }
public static void main(String args[]){ Prime prime=new Prime(); int temp =prime.mainMenu(); prime.operations(temp); }}
Attachments:
Similar questions