0% found this document useful (0 votes)
18 views8 pages

Class 20

Uploaded by

M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

Class 20

Uploaded by

M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

ii) while loop

==============
It will execute the source code how long our condition is true.

syntax:
-------
while(condition)
{
-
- //code to be execute
-
}

ex:
----
class Test
{
public static void main(String[] args)
{
int i=1;

while(i<=10)
{
System.out.print(i+" "); // infinite 1
}
}
}

ex:
----
class Test
{
public static void main(String[] args)
{
int i=11;

while(i<=10)
{
System.out.print(i+" "); //nothing
}
}
}

Q) Write a java program to display 10 natural numbers?

ex:
---
class Test
{
public static void main(String[] args)
{
int i=1;

while(i<=10)
{
System.out.print(i+" "); //1 2 3 4 5 6 7 8 9 10

i++;
}
}
}

Q) Write a java program to perform sum of 10 natural numbers?

class Test
{
public static void main(String[] args)
{
int i=1,sum=0;

while(i<=10)
{
sum+=i;

i++;
}

System.out.println(sum);
}
}

Q) Write a java program to display factorial of a given number?

input:
n=5

output:
120

ex:
---
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt();

int i=n,fact=1;

while(i>=1)
{
fact*=i;

i--;
}

System.out.println(fact);
}
}

Q) Write a java program to display multiplication table of a given number?


import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt();
int i=1;
while(i<=10)
{
System.out.println(n+" * "+i+" = "+n*i);
i++;
}
}
}

Q) Write a java program to perform sum of digits of a given number?

input:
123

output:
6

ex:
---
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt(); //123

int rem,sum=0;

while(n>0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}

System.out.println(sum);
}
}

Q) Write a java program to find out given number is armstrong or not?

input:
153

output:
It is armstrong number (1*1*1+5*5*5+3*3*3) (1+125+27)(153)
ex:
----
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt(); //153

int temp=n;

int rem,sum=0;

while(n>0)
{
rem=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}

if(sum==temp)
System.out.println("It is armstrong number");
else
System.out.println("It is not armstrong number");
}
}

Q) Write a java program to display reverse of a given number?

input:
123

output:
321

ex:
---
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt(); //123

int rem,rev=0;

while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
System.out.println(rev);
}
}

Q) Write a java program to check given number is palindrome or not?

input:
121

output:
It is a palindrome number

ex:
---
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt(); //123

int temp=n;

int rem,rev=0;

while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}

if(temp==rev)
System.out.println("It is palindrome number");
else
System.out.println("It is not palindrome number");
}
}

Q) Write a java program to display reverse of a given number?

input:
123

output:
ThreeTwoOne

ex:
---
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt(); //123
while(n>0)
{
switch(n%10)
{
case 0: System.out.print("Zero"); break;
case 1: System.out.print("One"); break;
case 2: System.out.print("Two"); break;
case 3: System.out.print("Three"); break;
case 4: System.out.print("Four"); break;
case 5: System.out.print("Five"); break;
case 6: System.out.print("Six"); break;
case 7: System.out.print("Seven"); break;
case 8: System.out.print("Eight"); break;
case 9: System.out.print("Nine"); break;
}
n=n/10;
}
}
}

iii) for loop


=============
It will execute the source code how long our condition is true.

syntax:
-------
for(initialization;condition;incrementation/decrementation)
{
-
- //code to be execute
-
}

You might also like