import java.util.*;// since we are creating object of scanner class
class Loops
{
public static void main(String args[])
{
int i,j,fact=1,rev=0,r,ch,n;
do{
System.out.println("\n\t ****Menu****");
System.out.println("1. find ood even number(if el)");
System.out.println("2. Factorial of a number(for loop)");
System.out.println("3. print partter of a(right angle triangle inserted for loop)");
System.out.println("4. reverse of number (while loop)");
System.out.println("5. Exit\n");
System.out.print("Enter Choice");
Scanner sc=new Scanner(System.in);
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("enter number ");
n=sc.nextInt();
if(n%2==0)
{
System.out.println("entered no."+n+" is even");
}
else
{
System.out.println("entered no."+n+" is odd");
}
break;
case 2:
System.out.println("enter number ");
n=sc.nextInt();
for (i=2;i<=n;i++)
{
fact=fact*i;
}
System.out.println("FActorial of "+n+";"+fact);
break;
case 3:
for(i=1;i<=4;i++)
{//start outer for loop
for(j=1;j<=i;j++)
{//start outer for loop
System.out.print("*");
}//end of outer for loop
System.out.println(" ");
}//end outer for loop
break;
case 4:
System.out.println("enter number");
n=sc.nextInt();
while(n!=0)
{
r=n%10;
rev=(rev*10)+r;
n=n/10;
}
System.out.println("reverse of given number:" +rev);
break;
case 5:
System.exit(0);
break;
default:
System.out.println("please enter the valid choice");
}//end of while
}while(true);
}//end of main
}//end of class
Output:
****Menu****
1. find ood even number(if el)
2. Factorial of a number(for loop)
3. print partter of a(right angle triangle inserted for loop)
4. reverse of number (while loop)
5. Exit
Enter Choice1
enter number
3
entered no.3 is odd
****Menu****
1. find ood even number(if el)
2. Factorial of a number(for loop)
3. print partter of a(right angle triangle inserted for loop)
4. reverse of number (while loop)
5. Exit
Enter Choice2
enter number
5
FActorial of 5;120
****Menu****
1. find ood even number(if el)
2. Factorial of a number(for loop)
3. print partter of a(right angle triangle inserted for loop)
4. reverse of number (while loop)
5. Exit
Enter Choice3
*
**
***
****
****Menu****
1. find ood even number(if el)
2. Factorial of a number(for loop)
3. print partter of a(right angle triangle inserted for loop)
4. reverse of number (while loop)
5. Exit
Enter Choice4
enter number
1234
reverse of given number:4321
****Menu****
1. find ood even number(if el)
2. Factorial of a number(for loop)
3. print partter of a(right angle triangle inserted for loop)
4. reverse of number (while loop)
5. Exit
Enter Choice5
------------------
(program exited with code: 0)
Press return to continue
class Loops
{
public static void main(String args[])
{
int i,j,fact=1,rev=0,r,ch,n;
do{
System.out.println("\n\t ****Menu****");
System.out.println("1. find ood even number(if el)");
System.out.println("2. Factorial of a number(for loop)");
System.out.println("3. print partter of a(right angle triangle inserted for loop)");
System.out.println("4. reverse of number (while loop)");
System.out.println("5. Exit\n");
System.out.print("Enter Choice");
Scanner sc=new Scanner(System.in);
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("enter number ");
n=sc.nextInt();
if(n%2==0)
{
System.out.println("entered no."+n+" is even");
}
else
{
System.out.println("entered no."+n+" is odd");
}
break;
case 2:
System.out.println("enter number ");
n=sc.nextInt();
for (i=2;i<=n;i++)
{
fact=fact*i;
}
System.out.println("FActorial of "+n+";"+fact);
break;
case 3:
for(i=1;i<=4;i++)
{//start outer for loop
for(j=1;j<=i;j++)
{//start outer for loop
System.out.print("*");
}//end of outer for loop
System.out.println(" ");
}//end outer for loop
break;
case 4:
System.out.println("enter number");
n=sc.nextInt();
while(n!=0)
{
r=n%10;
rev=(rev*10)+r;
n=n/10;
}
System.out.println("reverse of given number:" +rev);
break;
case 5:
System.exit(0);
break;
default:
System.out.println("please enter the valid choice");
}//end of while
}while(true);
}//end of main
}//end of class
Output:
****Menu****
1. find ood even number(if el)
2. Factorial of a number(for loop)
3. print partter of a(right angle triangle inserted for loop)
4. reverse of number (while loop)
5. Exit
Enter Choice1
enter number
3
entered no.3 is odd
****Menu****
1. find ood even number(if el)
2. Factorial of a number(for loop)
3. print partter of a(right angle triangle inserted for loop)
4. reverse of number (while loop)
5. Exit
Enter Choice2
enter number
5
FActorial of 5;120
****Menu****
1. find ood even number(if el)
2. Factorial of a number(for loop)
3. print partter of a(right angle triangle inserted for loop)
4. reverse of number (while loop)
5. Exit
Enter Choice3
*
**
***
****
****Menu****
1. find ood even number(if el)
2. Factorial of a number(for loop)
3. print partter of a(right angle triangle inserted for loop)
4. reverse of number (while loop)
5. Exit
Enter Choice4
enter number
1234
reverse of given number:4321
****Menu****
1. find ood even number(if el)
2. Factorial of a number(for loop)
3. print partter of a(right angle triangle inserted for loop)
4. reverse of number (while loop)
5. Exit
Enter Choice5
------------------
(program exited with code: 0)
Press return to continue
No comments:
Post a Comment