Sie sind auf Seite 1von 9

Q1. WAP to accept an integer between 1 to 10 and display it in words. Display a proper error message for wrong input.

Ans :

public class num_words

public void main(int n)

switch(n)

case 1 : System.out.println("one");

break;

case 2 : System.out.println("two");

break;

case 3 : System.out.println("three");

break;

case 4 : System.out.println("four");

break;

case 5 : System.out.println("five");

break;

case 6 : System.out.println("six");

break;

case 7 : System.out.println("seven");

break;

case 8 : System.out.println("eight");

break;

case 9 : System.out.println("nine");

break;

case 10 :System.out.println("ten");

break;

default :System.out.println("WRONG INPUT!!");

}
Q2. WAP to accept three integers and find and display the greatest integer of the three.

Ans:

public class greatest

public void main(int x , int y , int z)

if(x>y && x>z)

System.out.println(x+" is greatest");

else if(y>x && y>z)

System.out.println(y+" is greatest");

else

System.out.println(z+" is greatest");

}
Q3. WAP to accept a number and display its reverse.

Ans:

import java.util.Scanner;

public class reverse

public void main()

Scanner scn =new Scanner(System.in);

System.out.print("Enter a number : ");

int n = scn.nextInt();

int rev = 0, rem;

while(n!=0)

rem = n%10;

n = n/10;

rev = (rev+rem) *10;

rev /= 10;

System.out.println(rev);

}
Q4. Write a menu-driven program to find the following:

i) Sum of square of digits of a number


ii) Factors of a number

Ans:

import java.util.Scanner;

public class square_factor

{ void main() {

Scanner scn = new Scanner(System.in);

System.out.println("\tMENU\n1.Sum of square of digits of a number\n2.Factors of a number");

System.out.print("Enter your choice : ");

int chc = scn.nextInt();

System.out.print("Enter a number : ");

int n = scn.nextInt();

switch(chc) {

case 1:int sum = 0, rem;

while(n!=0) {

rem = n%10;

n = n/10;

sum += (rem*rem); }

System.out.println("sum = "+sum);

break;

case 2:System.out.println("Factors of "+n+" are :");

for(int i = 1;i<=n;i++)

if(n%i==0)

System.out.println(i);

break;

default:System.out.println("WRONG INPUT !!!");

} } }
Q5. WAP to accept two integers and perform arithmetic operations as entered by the user

i) ‘+’ for addition


ii) ‘-‘ for subtraction
iii) ‘*’ for multiplication
iv) ‘/’ for division

Ans:

import java .util.Scanner;

public class calculator

void main()

Scanner scn = new Scanner(System.in);

System.out.print("Enter first value : ");

int n1 = scn.nextInt();

System.out.print("Enter second value : ");

int n2 = scn.nextInt();

System.out.print("Enter arithmetic operator : ");

char op = scn.next().charAt(0);

switch(op)

case '+' : int sum = n1+n2;

System.out.println("sum of "+n1+" and "+n2+" is "+sum);

break;

case '*' : int prod = n1*n2;

System.out.println("product of "+n1+" and "+n2+" is "+prod);

break;

case '/' : float div = (float)n1/n2;

System.out.println("divison of "+n1+" by "+n2+" is equal to "+div);

break;

default : System.out.println("WRONG INPUT !!!");

}
Q6. Write a menu-driven program to find the following:

i) Reverse of a number.
ii) Sum of digits of the number

Ans:

import java.util.Scanner;

public class reverse_sumDigits {

void main(){

Scanner scn = new Scanner(System.in);

System.out.println("\tMENU\n1.Reverse of a number\n2.Sum of digits of the number");

System.out.print("Enter your choice : ");

int chc = scn.nextInt();

System.out.print("Enter a number : ");

int n = scn.nextInt();

switch(chc){

case 1:int rev = 0, rem ,b = n;

while(n!=0) {

rem = n%10;

n = n/10;

rev = (rev+rem) *10; }

rev /= 10;

System.out.println("reverse of "+b+" is "+rev);

break;

case 2:int sum = 0, rem1;

while(n!=0) {

rem1 = n%10;

n = n/10;

sum += rem1;

System.out.println("sum = "+sum);

break;

default:System.out.println("WRONG INPUT !!!");

} } }
Q7. WAP to find the factorial of a number

Ans:

import java.util.Scanner;

public class factorial

void main()

Scanner scn = new Scanner(System.in);

System.out.print("Enter a number : ");

int n = scn.nextInt();

int fact = 1;

for(int i = 1;i<=n;i++)

fact = fact*i;

System.out.println("Factorial of "+n+" is "+fact);

Q8. WAP to print the square of all the even numbers between 1 to 50

Ans:

public class even_square

void main()

for(int i = 2;i<=50;i+=2 )

int square = i*i;

System.out.println(i+"*"+i+" = "+square);

}
Q9. WAP to check whether a number is a prime number or not

Ans:

import java.util.Scanner;

public class prime

void main()

Scanner scn = new Scanner(System.in);

System.out.print("Enter a Number : ");

int n = scn.nextInt();

for(int i = 2;i<n;i++)

if(n%i==0)

System.out.println(n+" is not prime");

return;

System.out.println(n+" is prime");

}
Q10. WAP to check whether a number is Armstrong number or not

import java.util.Scanner;

public class armstrong

public void main()

Scanner scn =new Scanner(System.in);

System.out.print("Enter a number : ");

int n = scn.nextInt();

int rem , sum = 0 , bup = n;

while(n!=0)

rem = n%10;

n = n/10;

sum += rem*rem*rem;

if (bup==sum)

System.out.println(bup+" is armstorng number");

else

System.out.println(bup+" is not a armstorng number");

Das könnte Ihnen auch gefallen