Write a program to print number
from 1 to 100
for(int i =1;i<=50;i++)
for(int i=20; i>=1;i--)
for (int i = 100; i>=5; i-=5)
1 3 5 7
class number 100
{ public static void main()
{
for(int i=1 ; i<=100; i++) 12345
{ 1 2 3 4
[Link](i+” “);
}
}}
Write a program to print even numbers from 20 to 50
– // to print even numbers from 20 to 50 i i<=50 i+=2
– class even 20 22
– {public static void main() 22 24
– { 24 26
– for( int i =20;i<=50; i+=2) .
– {[Link] (i); } .
– }} . 50
50 52
Write a program to find product of odd
numbers from 1 to 30
// to print product of odd numbers
// from 1 to 30
i i<=50 P=1 class odd
1 1*1 1 {public static void main()
3 1*3 3 { int p =1;
5 3*5 15 for( int i =1;i<=30; i+=2)
7 15*7 45
{ p=p*i; }
.
[Link] (p);
.
}}
30
Write a program to find sum of all even numbers from 2
to 15
// to print sum of even numbers
// from 2 to 15
i i<=50 S=0
class even
2 0+2 2
{public static void main()
4 2+4 6
{ int s=0;
6 6+6 12
8 12+8 20 for( int i =2;i<=15; i+=2)
10 20+10 30 { s=s+ i; }
12 30+12 42 [Link] (i);
14 42+14 56 }}
Write a program to find product of odd
numbers from 1 to 30
// to print product of odd numbers
// from 1 to 30
i i<=50 P=1
1 1*1 1 class even
3 1*3 3 {public static void main()
5 3*5 15 { int p =1;
7 15*7 45
for( int i =1;i<=30; i+=2)
.
. {[Link] (i); }
30 }}
Write a program to input 20 numbers and find the smallest number
// program to find smallest number
import [Link];
class small
{public static void main()
{ Scanner sc=new Scanner([Link]);
i a s int a=0,s=0;
1 25 18 [Link](“Input the first number ”);
2 21 s=[Link]();
3 31
[Link](“Input the rest of numbers by one”);
4 35
for( int i =1;i<=19 ; i++)
5 15 15
{ a=[Link]();
6 5 5
7 1 1 if (s>a)
s=a;}
[Link] (s);}}
Write a program to input a number and find its factors
// program to find the factors of a number
import [Link];
class small
{public static void main()
6 a s
{ Scanner sc=new Scanner([Link]);
10 1 1
int a=0;
2
[Link](“Input a number ”);
3
4 a=[Link]();
5 0 for( int i =1;i< =a ; i++)
6 { if(a%i==0)
7 [Link] (i);
}} }
Write a program to input a number and find the
number is a perfect number or not.
A number is said to be perfect if the sum of factors of
that number is same as original number
// program to find perfect number or not
import [Link];
6 a S=0
6 1 0+1=1 class small
6 2 1+2=3 {public static void main()
6 3 3+3=6 { Scanner sc=new Scanner([Link]);
6 4 int a=0,s=0;
6 5 [Link](“Input a number ”);
a=[Link]();
for( int i =1;i<a ; i++)
{ if(a%i==0)
s=s+i;}
if (s==a)
[Link] (a+” is perfect number “);
else
[Link] (a+” is not a perfect number “);
}}
Write a program to input a number and find its factorial .
The factorial of a number is the product of all numbers from
one to that number
eg factorial of 5
1 * 2 *3*4*5 =120
// program to find perfect number or not
i f=1 f=1 import [Link];
1 1*1 1 class small
2 1*2 2 {public static void main()
3 2*3 6 { Scanner sc=new Scanner([Link]);
4 6*4 24 int a=0,f=1;
5 24*5 120 [Link](“Input a number ”);
a=[Link]();
for( int i =1;i<=a ; i++)
{ f=f*I; }
[Link] (“the factoial of “ +a+”is” +f );
}}
The factorial of 4 is 24
Write a program to input a number and find whether
the number is a prime number or not
A number is said to be prime if there are no other
factors other than 1 and the original number
e.g. 7 19
// program to find prime number or not
a= 8 C=0
2 8%2 0 1 import [Link];
3 8%3 class small for( int i =1;i< = a ; i++)
4 8%4 0 1+1 {public static void main() { if(a%i==0)
5 8%5 { Scanner sc=new Scanner([Link]); c=c+1;}
6 8%6
int a=0,c=0; if (c==2)
7 8%7
[Link](“Input a number ”);
2
a=[Link]();
for( int i =1;i< a ; i++)
a= 7 C=0 for( int i =2;i<a ; i++)
{ if(a%i==0)
2 7%2 1 7%1 =0 { if(a%i==0)
c=c+1;}
3 7%3 2 c=c+1;}
4 7%4 3 if (c==1)
if (c==0)
5 7%5 4 7%4
[Link] (a+” is prime number “);
6 7%6 5
else
6
[Link] (a+” is not a prime number “);
}}
Abundant number – A number which is smaller than its sum of factors eg, 12
factors are 1,2,3,4 and 6 whose sum will be 16 and 12 is smaller than this
number
Deficient number - A number which is larger than its sum of factors eg, 9
factors are 1 and 3 whose sum will give 4 and 9 is bigger than 4
Perfect number – A number is equal to the sum of factors eg. 6 factors are 1,2
and 3 when added will give 6. This will not be abundant and deficient numbers
// program to find perfect number or not
import [Link];
6 a s class small
9 1 0 {public static void main()
2
{ Scanner sc=new Scanner([Link]);
3 0
int a=0,s=0;
4
[Link](“Input a number ”);
5
6 a=[Link]();
1+2+3+ 1+3=4 for( int i =1;i<a ; i++)
4+6=
{ if(a%i==0) s=s+I;}
16
if (a<s)
[Link] (a+” is abundant number “);
else if(s==a)
[Link] (a+” is a perfect number “);
else
[Link](a+”is deficient number “);
0 1 1 2 3 5 8 13 21
Write a program to print first 50 fibonocci numbers
Fibonocci numbers first number is 0 and 2nd number 1 . The third number will always the be sum of last
two numbers
0 1 1 2 3 5 8 13 21 // program to find fibonocci number or not
a b c class fibo
0 1 1 {public static void main()
1 1 2
{ int a =0,b=1; int c =0;
1 2 3
[Link](a + “ ” + b;);
2 3 5
3 5 8
5 8 13 for( int i =3 ;i< =50 ; i++)
8 13 21 { c=a+b;
[Link] (c + “ “);
0 1 a =b;b=c;}
}}
Write a program to print the following series
1 3 5 7 9 . . .. . 21
// to print series
class series
{public static void main()
{ int a;
for( int i =1;i<=21; i+=2)
{
[Link] (i); }
}}
Write a program to print the following series
i a
1 8 27 64 125 1 1
2 8
// to print series 3 9
4 16
class series
5 25
{public static void main()
{ int a;
for( int i =1;i<=10; i++)
{ a = i*i *i;
[Link] (a); }
}}
Write a program to print the following series
1 8 27 64 …….1000
// to print series
class series
{public static void main()
{ int a;
for( int i =1;i<=10; i++)
{ a= i*i*i*I;
[Link] (i); }
}}