Saturday, 17 June 2023

Java Chapter 4

( Conditional Statement )

> L o o p s <
[Edited and compiled bySouradeep Roy using Intelij IDEA (community version)
=============================================================================================================================

For Loop:
==============

4.0  Write a program in java to print your name 10 times:-
_______________________________________________________________________________________________

CODE:-
-------

package com.mycompany.justforfun;

import java.util.Scanner;

public class Justforfun
{

    public static void main(String[] args)

    {
        Scanner input =new Scanner(System.in);

        int i;

        String nm;

        System.out.println("Please enter your good name");

        nm=input.next();

        for(i=0;i<=10;i++)
        {
            System.out.println("My name is"+nm);
        }
}

}

Output:-
--------
Please enter your good name
Souradeep
My name is Souradeep
My name is Souradeep
My name is Souradeep
My name is Souradeep
My name is Souradeep
My name is Souradeep
My name is Souradeep
My name is Souradeep
My name is Souradeep
My name is Souradeep
My name is Souradeep
------------------------------------------------------------------------

4.1   Write a program in Java to perform incremention on the left(0-10) and decrementation on the right(10-0).....____________________________________________________________________________________________________________________

CODE:-
-------
package com.mycompany.justforfun;

import java.util.Scanner;

public class Justforfun
 {

    public static void main(String[] args)
    {

    Scanner input =new Scanner(System.in);

    int i,n=10;

    System.out.println("i,n\n");

    for(i=0;i<=10;i++)

    {

  System.out.println(i+"     "+n);

  n--;
}

}
}

Output:-
--------

i,n

0     10
1     9
2     8
3     7
4     6
5     5
6     4
7     3
8     2
9     1
10    0
-----------------------------------------------------------------------------------------------------------------------------

4.2        Write a program to print 0 to n.
____________________________________________________________________________________________________________________

CODE:-
-------

package com.mycompany.justforfun;

import java.util.Scanner;

public class Justforfun
{

    public static void main(String[] args)

    {

      Scanner input =new Scanner(System.in);

      int i,n;

      System.out.println("Enter a range");

      n=input.nextInt();

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

      {
          System.out.println("\n"+i);
      }
}
}

Output:-
--------
Enter a range
10

1

2

3

4

5

6

7

8

9

10
------------------------------------------------------------------------
Same program can also be written in different ways such as:


Code:
-----

package com.mycompany.justforfun;

import java.util.Scanner;

public class Justforfun {

    public static void main(String[] args)
    {
      Scanner input =new Scanner(System.in);
      int i,n;
      System.out.println("Enter a range");
      n=input.nextInt();
      i=1;
      for(;i<=n;)
      {
          System.out.println("\n"+i);
          i++;
      }
}
}

    
Code:-
------

package javaapplication1;
import java.util.Scanner;

public class JavaApplication1 {

    public static void main(String[] args)
    {
        
 Scanner input=new Scanner(System.in);  
int i,n;
System.out.println("Enter the limit: ");
n=input.nextInt();
i=0;
for(;i<=n; i=i+1)
{
System.out.println("\n"+i);
}
}   
}

(Both will generate the same output)
----------------------------------------------------------------------------------------------------------------------------

4.3                                        Wap to print a sequence in reverse order:
____________________________________________________________________________________________________________________


    
Code:-
------

package javaapplication1;
import java.util.Scanner;

public class JavaApplication1 {

    public static void main(String[] args)
    {
        
 Scanner input=new Scanner(System.in);  
 int i,n;
 System.out.println("Enter a defnite range");
 n=input.nextInt();
 for(i=n;i>0;i--)
 {
     System.out.println("\n"+i);
 }
}   
}

Output:-
--------
Enter a defnite range
10

10

9

8

7

6

5

4

3

2

1
------------------------------------------------------------------------------------------------------------------------

4.3                                Write a program to make a multiplication table.
____________________________________________________________________________________________________________________

    
Code:-
------

package javaapplication1;

import java.util.Scanner;

public class JavaApplication1 {

    public static void main(String[] args)
    {
        
Scanner input=new Scanner(System.in);  

int x,c,n,i;

System.out.println("Enter the number for multiplication table ");

x=input.nextInt();

System.out.println("Enter the range");

n=input.nextInt();

for(i=1;i<=n;i++)
{
    c=x*i;

    System.out.println(x+"*"+i+"="+c);
}

}   
}

Output:-
--------
Enter the number for multiplication table
5
Enter the range
10
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50
BUILD SUCCESSFUL (total time: 6 seconds)
------------------------------------------------------------------------------------------------------------------------


4.4                        Write a program to show odd numbers only in a range.
____________________________________________________________________________________________________________________


    
Code:-
------

package javaapplication1;

import java.util.Scanner;

public class JavaApplication1
 {

    public static void main(String[] args)

    {
        
Scanner input=new Scanner(System.in);  

int i,n;

System.out.println("Enter the limit: ");

n=input.nextInt();

for(i=1;i<=n;i+=2)              //Start i from 1
{

System.out.println("\t"+i);
}

}   
}

Output:-
--------
Enter the limit:
10

    1
    3
    5
    7
    9
BUILD SUCCESSFUL (total time: 3 seconds)
------------------------------------------------------------------------------------------------------------------------

4.5                                Write a program to show even numbers only in a range.            
____________________________________________________________________________________________________________________


    
Output:-
--------
Enter the limit:
10

    0
    2
    4
    6
    8
    10
BUILD SUCCESSFUL (total time: 2 seconds)
------------------------------------------------------------------------------------------------------------------------

4.6                            Write the output of the following numbers:-
 
                            (1+2+3+4+5………………………..n)
____________________________________________________________________________________________________________________



    
Code:-
------

package javaapplication1;

import java.util.Scanner;

public class JavaApplication1
{

    public static void main(String[] args)
    {
        
Scanner input=new Scanner(System.in);  

int i,n;

int sum=0;

System.out.println("Enter the limit: ");

n=input.nextInt();

for(i=1;i<=n;i++)  
           
{
    sum+=i;

System.out.println("\t"+sum);

}

}   
}

Output:-
--------
Enter the limit:
10

    1
    3
    6
    10
    15
    21
    28
    36
    45
    55
BUILD SUCCESSFUL (total time: 2 seconds)
------------------------------------------------------------------------------------------------------------------------

4.7                Write the output of the following numbers
                    1^2+2^2+3^2+4^2+5^2………………………..n^2
____________________________________________________________________________________________________________________

Code:-
------

package javaapplication1;

import java.util.Scanner;

import java.io.*;

public class JavaApplication1
{

    public static void main(String[] args)
    {
        
Scanner input=new Scanner(System.in);  

int i,n;

int sum=0;

System.out.println("Enter the limit: ");

n=input.nextInt();

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

 sum+=Math.pow(i,2);                    //use math fuction

System.out.println("\n"+sum);
}

}   
}


Output:-
--------
Enter the limit:
10

1

5

14

30

55

91

140

204

285

385
BUILD SUCCESSFUL (total time: 2 seconds)
------------------------------------------------------------------------------------------------------------------------

4.9            Write the output of the following numbers:1^1+2^2+3^3+4^4+5^5………………………..n^n

____________________________________________________________________________________________________________________


Code:-
------


package javaapplication1;

import java.util.Scanner;

import java.io.*;

public class JavaApplication1
{

    public static void main(String[] args)
    {
        
Scanner input=new Scanner(System.in);  

int i,n;

int sum=0;

System.out.println("Enter the limit: ");

n=input.nextInt();

for(i=1;i<=n;i++)     
      
{
     sum+=Math.pow(i,i);

System.out.println("\n"+sum);
}

}   
}

Output:-
--------
run:
Enter the limit:
10

1

5

32

288

3413

50069

873612

17650828

405071317

2147483647
BUILD SUCCESSFUL (total time: 3 seconds)
___________________________________________________________________________________________________________________________

4.10                      Write the output of the following numbers
                        (1 + 1/2 + 1/3+ 1/4 + 1/5..............1/n)
___________________________________________________________________________________________________________________________

Code:-
------

package javaapplication1;

import java.util.Scanner;

import java.io.*;

public class JavaApplication1
{

    public static void main(String[] args)
    {
        
Scanner input=new Scanner(System.in);  

float i,n,sum=0;

System.out.println("Enter The Limit : \t");

n=input.nextFloat();

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

{
sum+=1/i;

System.out.println("\n"+sum);

}

}   
}

Output:-
--------
Enter The Limit :     
5

1.0

1.5

1.8333334

2.0833335

2.2833335
BUILD SUCCESSFUL (total time: 2 seconds)
----------------------------------------------------------------------------------------------------------------------------

4.11        Write the output of the following numbers (1+1/2^2 +1/3^2 +1/4^2 +1/5^2 ………………1/n^2)
___________________________________________________________________________________________________________________________




Code:-
------
package javaapplication1;

import java.util.Scanner;

import java.io.*;

public class JavaApplication1
{

    public static void main(String[] args)
    {
        
Scanner input=new Scanner(System.in);  

float i,n,sum=0;

System.out.println("Enter The Limit : \t");

n=input.nextFloat();

for(i=1;i<=n;i++)
{
    sum+=1/Math.pow(i, 2);
 
System.out.println("\n"+sum);
}    
}   
}

Output:-
--------
Enter The Limit :     
6

1.0

1.25

1.3611112

1.4236112

1.4636111

1.4913889
BUILD SUCCESSFUL (total time: 10 seconds)
----------------------------------------------------------------------------------------------------------------------------
    
4.12        1+1/2^2 +1/3^3 +1/4^4 +1/5^5 ………………1/n^n    
___________________________________________________________________________________________________________________________

Code:-
------



package javaapplication1;

import java.util.Scanner;

import java.io.*;

public class JavaApplication1
{

    public static void main(String[] args)
    {
        
Scanner input=new Scanner(System.in);  

float i,n,sum=0;

System.out.println("Enter The Limit : \t");

n=input.nextFloat();

for(i=1;i<=n;i++)
{
    sum+=1/Math.pow(i, i);

System.out.println("\n"+sum);

}

}   
}

Output:-
--------

run:
Enter The Limit :     
6

1.0

1.25

1.287037

1.2909433

1.2912632

1.2912847
BUILD SUCCESSFUL (total time: 2 seconds)
----------------------------------------------------------------------------------------------------------------------------

4.13            Write a Java program to print Fibonacci Series of given range.
___________________________________________________________________________________________________________________________

Code:-
------

package javaapplication1;
import java.util.Scanner;
import java.io.*;

public class JavaApplication1
{

    public static void main(String[] args)
    {
        
Scanner input=new Scanner(System.in);  
int n,a=0,b=1,c,i;
System.out.println("Enter the Limit =\t");
n=input.nextInt();
System.out.println(a+"  " +b);
for(i=2;i<=n;i++)
{
c=a+b;
System.out.println("   \n  "+c);
a=b;
b=c;
}
}   
}

Output:-
--------
run:
Enter the Limit =    
5

0  1
 
   1
 
   2
 
   3
 
   5
BUILD SUCCESSFUL (total time: 2 seconds)
----------------------------------------------------------------------------------------------------------------------------

4.14            Write a Java program to get Factorial of given number.
___________________________________________________________________________________________________________________________



Code:-
------
package javaapplication1;

import java.util.Scanner;

public class JavaApplication1
{

    public static void main(String[] args)

    {
        Scanner input=new Scanner(System.in);

        int i,fact=1;

        int num;

        System.out.println("Enter a number to calculate factorial");

        num=input.nextInt();

        for(i=1;i<=num;i++)

        {
            fact*=i;
        }

        System.out.println("The Result of the number"+num+"is"+fact);
    }
    
}

Output:-
--------
run:
Enter a number to calculate factorial
6

The Result of the number 6 is 720

BUILD SUCCESSFUL (total time: 2 seconds)
----------------------------------------------------------------------------------------------------------------------------

4.15            Write a Java program to get    Addition of odd number
_____________________________________________________________________________________________________________________________


Code:-
------
package javaapplication1;

import java.util.Scanner;

public class JavaApplication1
{

    public static void main(String[] args)
    {

 Scanner input=new Scanner(System.in);

int n,i,sum=0;

System.out.println("Enter How Many Times = ");

n=input.nextInt();

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

{
sum=sum+i;

System.out.println("  \t  "+sum);
}

}  
}

Output:-
--------
run:
Enter How Many Times =
3

        1
        4
        9
BUILD SUCCESSFUL (total time: 4 seconds)
----------------------------------------------------------------------------------------------------------------------------
                                
4.16                        WAP to find this series 1+1/5+1/9+1/13+……                        
_____________________________________________________________________________________________________________________________                                

Code:-
------

package javaapplication1;

import java.util.Scanner;

public class JavaApplication1
{

    public static void main(String[] args)

    {

 Scanner input=new Scanner(System.in);

float sum=0;

int n,i;

System.out.println("Enter the limit= ");

n=input.nextInt();

System.out.println("\nResult of above series=\n");

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

{

sum=sum+(float)1/i;

System.out.println("  \t  "+sum);
}

}  
}


Output:-
--------
run:
Enter the limit=
5

Result of above series=

        1.0
        1.2
BUILD SUCCESSFUL (total time: 5 seconds)
----------------------------------------------------------------------------------------------------------------------------

4.17                            Write a program in java to print an Asci chart using for loop
___________________________________________________________________________________________________________________________


Code:-
------

package javaapplication1;

import java.util.Scanner;

public class JavaApplication1
{

    public static void main(String[] args)

    {
Scanner input=new Scanner(System.in);

int i;

for(i=0;i<=255;i++)

{
System.out.println("The ASCI value of "+(char)i+"   =   "+i);

}

}  
}

Output:-
--------
The ASCI value of ì   =   236
The ASCI value of í   =   237
The ASCI value of î   =   238
The ASCI value of ï   =   239
The ASCI value of ð   =   240
The ASCI value of ñ   =   241
The ASCI value of ò   =   242
The ASCI value of ó   =   243
The ASCI value of ô   =   244
The ASCI value of õ   =   245
The ASCI value of ö   =   246
The ASCI value of ÷   =   247
The ASCI value of ø   =   248
The ASCI value of ù   =   249
The ASCI value of ú   =   250
The ASCI value of û   =   251
The ASCI value of ü   =   252
The ASCI value of ý   =   253
The ASCI value of þ   =   254
The ASCI value of ÿ   =   255
BUILD SUCCESSFUL (total time: 0 seconds)
----------------------------------------------------------------------------------------------------------------------------

4.18                    Write a program to show the series of leap years within a range specified.
___________________________________________________________________________________________________________________________


Code:-
------
package javaapplication1;

import java.util.Scanner;

public class JavaApplication1

{

    public static void main(String[] args)

    {
Scanner input=new Scanner(System.in);

int s ,e,i;

System.out.println("Enter start year : ");

s=input.nextInt();

System.out.println("Enter end year : ");

e=input.nextInt();

System.out.println("\n\nLeap years are following :\n\n");

for(i=s;i<=e;i++)

{

if(i%4==0)

{

System.out.println(" \t" +i);

}
}

}  
}
Output:-
--------
run:
Enter start year :
2000
Enter end year :
2022


Leap years are following :


     2000
     2004
     2008
     2012
     2016
     2020

BUILD SUCCESSFUL (total time: 17 seconds)
----------------------------------------------------------------------------------------------------------------------------

                                                Nested For
                                                For  within   For
4.19        Illustrative Example:                    
___________________________________________________________________________________________________________________________

Code:-
______

package javaapplication1;

import java.util.Scanner;

public class JavaApplication1
{

    public static void main(String[] args)

    {

Scanner input=new Scanner(System.in);

{

int x,y;

for(x=0;x<=5;x++)

{

for(y=0;y<=5;y++)
{

System.out.println("(x,y)=("+x+","+y+")");
}

}  
}
}
}

Output:-
--------
run:
(x,y)=(0,0)
(x,y)=(0,1)
(x,y)=(0,2)
(x,y)=(0,3)
(x,y)=(0,4)
(x,y)=(0,5)
(x,y)=(1,0)
(x,y)=(1,1)
(x,y)=(1,2)
(x,y)=(1,3)
(x,y)=(1,4)
(x,y)=(1,5)
(x,y)=(2,0)
(x,y)=(2,1)
(x,y)=(2,2)
(x,y)=(2,3)
(x,y)=(2,4)
(x,y)=(2,5)
(x,y)=(3,0)
(x,y)=(3,1)
(x,y)=(3,2)
(x,y)=(3,3)
(x,y)=(3,4)
(x,y)=(3,5)
(x,y)=(4,0)
(x,y)=(4,1)
(x,y)=(4,2)
(x,y)=(4,3)
(x,y)=(4,4)
(x,y)=(4,5)
(x,y)=(5,0)
(x,y)=(5,1)
(x,y)=(5,2)
(x,y)=(5,3)
(x,y)=(5,4)
(x,y)=(5,5)
BUILD SUCCESSFUL (total time: 0 seconds)
----------------------------------------------------------------------------------------------------------------------------

4.20
                                Write a program to show this shape.    
*
**
***
****
*****
******
*******
********
___________________________________________________________________________________________________________________________


Code:-
______

package javaapplication1;

import java.util.Scanner;

public class JavaApplication1
{

    public static void main(String[] args)

    {
Scanner input=new Scanner(System.in);

{
int i,n,j;

System.out.println("Enter the number  of rows");

n=input.nextInt();

//outer loop for rows

for(i=0;i<n;i++)
{
    //inner loop for coloumns

    for(j=0;j<=i;j++)

    {
        System.out.print("*");
    }
    
    System.out.println();
}
}

}
}

Output:
--------
run:
Enter the number  of rows
10

*
**
***
****
*****
******
*******
********
*********
**********

BUILD SUCCESSFUL (total time: 2 seconds)
---------------------------------------------------------------------------------------------------------------------------

4.21                    Write a program to show this shape.    



1
12
123
1234
12345
123456
1234567
___________________________________________________________________________________________________________________________



Code:-
------

package javaapplication1;

import java.util.Scanner;

public class JavaApplication1
{

    public static void main(String[] args)

    {

Scanner input=new Scanner(System.in);

{

int i, n, j, m; //since we are not printing string an extra is being taken and later must printed as well as incremented

System.out.println("Enter the number  of rows");

n=input.nextInt();

//outer loop for rows

for(i=0;i<n;i++)
{
     m=1;//intialising m with 1

    //inner loop for coloumns

    for(j=0;j<=i;j++)

    {
        System.out.print(m+ "");

        m++; // m being incremented to get the output
        
    }
    
    System.out.println();
}
}

}
}


Output:
--------
run:
Enter the number  of rows
10
1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678910
BUILD SUCCESSFUL (total time: 3 seconds)
---------------------------------------------------------------------------------------------------------------------------


4.22            Write a program to generate all combinations of 1, 2 and 3 using for loop.
___________________________________________________________________________________________________________________________



Code:-
------
package javaapplication1;

import java.util.Scanner;

public class JavaApplication1
{

    public static void main(String[] args)

    {
Scanner input=new Scanner(System.in);

{

 int i=1,j=1,k=1;

for (i=1;i<=3;i++)

{

for (j=1;j<=3;j++)

{

for (k=1;k<=3;k++)

System.out.println(i+""+j+""+k);

}

}

System.out.println("\n press any key to exit......");
}

}
}


Output:
--------
run:
111
112
113
121
122
123
131
132
133
211
212
213
221
222
223
231
232
233
311
312
313
321
322
323
331
332
333

 press any key to exit......
BUILD SUCCESSFUL (total time: 0 seconds)
---------------------------------------------------------------------------------------------------------------------------

4.23                        Q. Write a program to show this shape.

                                *****
                                ****
                                ***
                                **
                                *
___________________________________________________________________________________________________________________________

Code:-
------
package javaapplication1;

import java.util.Scanner;

public class JavaApplication1
{

    public static void main(String[] args)

    {

Scanner input=new Scanner(System.in);

{

int r,c,a;

System.out.println("enter the no");

a=input.nextInt();

for(r=a;r>=0;r--)

{

for(c=0;c<=r;c++)

{

System.out.print("*");

}

System.out.println();

}
}

}
}


Output:
--------
run:
enter the no
5
******
*****
****
***
**
*
BUILD SUCCESSFUL (total time: 2 seconds)
___________________________________________________________________________________________________________________________

4.24                                Write a program to produce the following output  
                                                  
                                1
                              2  3
                            4  5  6
                           7  8  9  10

-----------------------------------------------------------------------------------------------------------------------------

Code:-
------
package javaapplication1;

import java.util.Scanner;

public class JavaApplication1
{

    public static void main(String[] args)

    {
Scanner input=new Scanner(System.in);

{
int i,j,k,l,sp;

sp=20;

for (i=1,k=1;i<5;i++)

{

for (l=1;l<=sp;l++)
System.out.print(" ");
sp-=2;
for(j=1;j<=i;j++,k++)
System.out.print("  "+k);
System.out.println();
}
System.out.println("\n press any key to exit......");
}
}
}



Output:
-------
run:
                      1
                    2  3
                  4  5  6
                7  8  9  10

 press any key to exit......
BUILD SUCCESSFUL (total time: 0 seconds)
-----------------------------------------------------------------------------------------------------------------------------
4.25                Write a program of following series
                         (1 + 1/1*2*3 + 1/1*2*3*4)
___________________________________________________________________________________________________________________________

Code:-
------
package javaapplication1;

import java.util.Scanner;

public class JavaApplication1
{

    public static void main(String[] args)
    {

Scanner input=new Scanner(System.in);

{

int i,n,j;

float s = 0,t=1;

System.out.println("Enter your range");

n=input.nextInt();

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

{

for(j=1;j<=i;j++)

{

t=t*j;

}

s+=1/t;

System.out.println(" "+s);
}
}

}
}


Output:
-------
run:
Enter your range
6
 1.0
 1.5
 1.5833334
 1.5868056
 1.5868345
 1.5868345
BUILD SUCCESSFUL (total time: 2 seconds)
-----------------------------------------------------------------------------------------------------------------------------
4.25                            Write a program to show prime numbers only in a range.        

___________________________________________________________________________________________________________________________


CODE:-
------

package javaapplication1;

import java.util.Scanner;

public class JavaApplication1
{

    public static void main(String[] args)
    {

         int s1, s2, s3, flag = 0, i, j;

        Scanner s = new Scanner(System.in);

        System.out.println ("Enter the lower limit :");

        s1 = s.nextInt();

        System.out.println ("Enter the upper limit :");

        s2 = s.nextInt();

        System.out.println ("The prime numbers in between the entered limits are :");

        for(i = s1; i <= s2; i++)

         {
             for( j = 2; j < i; j++)

             {
                 if(i % j == 0)

                 {
                     flag = 0;

                     break;
                 }

                 else

                 {
                     flag = 1;
                 }
             }

             if(flag == 1)

             {
                 System.out.println(i);
             }
         }
    }
}

Output:-
--------
run:
Enter the lower limit
2
Enter the upper limit
20
The Prime numbers in the above mentioned range are :
3
5
7
11
13
17
19
BUILD SUCCESSFUL (total time: 4 seconds)
-----------------------------------------------------------------------------------------------------------------------------
                                    [    While Loop    ]    
                                    ____________________


4.26        Write a program in java to print all natural numbers using While loop                    

___________________________________________________________________________________________________________________________

CODE:-
------

package javaapplication1;

import java.util.Scanner;

public class JavaApplication1
{

    public static void main(String[] args)
    {
Scanner input=new Scanner(System.in);

int n;

System.out.println("Enter a range:");

n=input.nextInt();

int i=0;
while(i<=n)
{
    System.out.println(" "+i);
    i++;
}

}
}

 
Output:-
--------
run:
Enter a range:-
10

 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
BUILD SUCCESSFUL (total time: 2 seconds)
-----------------------------------------------------------------------------------------------------------------------------
4.27                    Write a JAVA program to reverse any number.
___________________________________________________________________________________________________________________________


CODE:-
------

import java.util.Scanner;

public class Main{

    public static void main(String[] args)
            {
                Scanner input=new Scanner(System.in);
                int a,b=0;
              System.out.println("Enter a Group of Number : ");
              a= input.nextInt();
                while(a!=0)
                {
                    b=b*10+a%10;
                    a=a/10;
                }
             System.out.println("The reverse of the entered no is = "+b);

        }
    }

Output:
-------
Enter a Group of Number :
12345

 The reverse of the entered no is = 54321

Process finished with exit code 0
-----------------------------------------------------------------------------------------------------------------------------
4.28                    Write a Java program to find out value of a power number without using pow().
___________________________________________________________________________________________________________________________


CODE:-
------
import java.util.Scanner;

public class Main
{

    public static void main(String[] args)
            {
                Scanner input=new Scanner(System.in);

                int p=0,b=0,r=1,i=1;

                System.out.println("\n Enter Number: ");

                b=input.nextInt();

                System.out.println("\n Enter Power: ");

                p=input.nextInt();

                while(i<=p)

                {
                    r=r*b;
                    i++;
                }

                System.out.println(b+" Raised to "+p+" = "+r);

            }
        }

Output:
-------

 Enter Number:
    5

 Enter Power:
    2

5 Raised to 2 = 25

Process finished with exit code 0
-----------------------------------------------------------------------------------------------------------------------------
4.29                Write a Java program to find out sum of digit of given number.        

___________________________________________________________________________________________________________________________

CODE:-
------
import java.util.Scanner;

public class Main
{

    public static void main(String[] args)
            {
                Scanner input=new Scanner(System.in);
                int a,b=0,sum=0;
                System.out.println("\n Enter Number: ");
                a= input.nextInt();
                while(a!=0)
                {
                    b=a%10;
                    sum+=b;
                    a/=10;
                }
                System.out.println("The Sum of the digit ="+sum);

            }
        }



Output:
-------

Enter Number:
123
The Sum of the digit =6
-----------------------------------------------------------------------------------------------------------------------------
4.30                        Write a Java program to count number of digits in a number.
___________________________________________________________________________________________________________________________

CODE:-
------
import java.util.Scanner;

public class Main
{

    public static void main(String[] args)

            {
               Scanner input=new Scanner(System.in);

               int n,count=0;

               System.out.println("Enter a Group of Number: ");

                n=input.nextInt();

                while(n!=0)

                {
                    count++;
                    n=n/10;
                }

              System.out.println("\nThe Number of Digit = "+count);

            }

            }




Output:
-------
Enter a Group of Number:
69354

The Number of Digit = 5
-----------------------------------------------------------------------------------------------------------------------------
4.31                            Write a program to check a number is Armstrong number or not
___________________________________________________________________________________________________________________________


CODE:-
------
import java.util.Scanner;

public class Main
{

    public static void main(String[] args)

            {
                Scanner input=new Scanner(System.in);

                int number, sum = 0, temp, remainder;

               System.out.println("Enter an Integer\n");

                number= input.nextInt();

                temp = number;

                while( temp != 0 )
                {
                    remainder = temp%10;

                    sum = sum + remainder*remainder*remainder;

                    temp = temp/10;
                }
                if ( number == sum )

                    System.out.println("Entered Number is an Armstrong Number");

                else
                    System.out.println("Entered Number is not an Armstrong Number.\n");
            }
            }

Output:
-------

Enter an Integer

153
Entered Number is an Armstrong Number

Process finished with exit code 0
-----------------------------------------------------------------------------------------------------------------------------
4.32                     Write a Java program to check whether the given integer is Magic number or not.
___________________________________________________________________________________________________________________________


CODE:-
------
import java.util.Scanner;

public class Main
{

    public static void main(String[] args)

            {
                Scanner input=new Scanner(System.in);

                int rem, rev=0, n;

                System.out.println("Enter the Required Number:");

                n=input.nextInt();

                while(n>0)
                {
                    rem=n%10;

                    rev=(10*rev)+ rem;

                    n=n/10;
                }
                if(rev%9== 0)

                    System.out.println("Magic Number.");

                else

                    System.out.println("Not a Magic number.");
            }
            }


Output:
-------
Enter the Required Number:
81
Magic Number.

Process finished with exit code 0
-----------------------------------------------------------------------------------------------------------------------------
                                            Do While Loop:
                                        ____________________


4.33                Write a program in Java to print numbers from 1 to 10 using Do While loop
___________________________________________________________________________________________________________________________

CODE:-
------
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
            {
                Scanner input=new Scanner(System.in);

                int i=0;

                do

                {

                System.out.println(" The Output is : "+i);

                i++;

                }

                while(i<=10);
            }
            }

Output:
-------

 The output is : 0
 The output is : 1
 The output is : 2
 The output is : 3
 The output is : 4
 The output is : 5
 The output is : 6
 The output is : 7
 The output is : 8
 The output is : 9
 The output is : 10

Process finished with exit code 0
-----------------------------------------------------------------------------------------------------------------------------
4.34                        Write a Java program to reverse any number using Do while loop.
___________________________________________________________________________________________________________________________

CODE:-
------
import java.util.Scanner;

public class Main

{
    public static void main(String[] args)

            {
                Scanner input=new Scanner(System.in);

                int a;

                int b=0;

                System.out.println("Enter the number");

                a=input.nextInt();

                do {

                    b=b*10+a%10;

                    a=a/10;
                }
                while(a!=0);

                System.out.println(" The output is = "+b);

            }
            }

Output:
-------
Enter the number
 321
The output is = 123
-----------------------------------------------------------------------------------------------------------------------------
4.35                Write a Java program to find out sum of digit of given number using Do while loop.
___________________________________________________________________________________________________________________________


CODE:-
------
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)

            {
                Scanner input=new Scanner(System.in);

                int a,sum=0;

                int b=0;

                System.out.println("Enter the number");

                a=input.nextInt();

                do {

                    b=a%10;
                    sum+=b;
                    a=a/10;
                }

                while(a!=0);

                System.out.println(" The output is = "+sum);
            }
            }


Output:
-------
Enter the number
123

The output is = 6

Process finished with exit code 0
-----------------------------------------------------------------------------------------------------------------------------
4.36                    Write a C program to find out value of a power number without using pow().
____________________________________________________________________________________________________________________________




CODE:-
------
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
            {
                Scanner input=new Scanner(System.in);

                int r=1,p=0;

                int i=1,b=0;

                System.out.println("Enter the number");

                b=input.nextInt();

                System.out.println("Enter the Power");

                p=input.nextInt();

                do {

                    r=r*b;
                    i++;
                }
    
                while(i<=p);

                System.out.println(b+" Raised to "+p+ " = " +r);
            }
            }



Output:
-------
Enter the number
2
Enter the Power
2
2 Raised to 2 = 4

Process finished with exit code 0
-----------------------------------------------------------------------------------------------------------------------------
4.37            Write a java program to count number of digits in a number.
____________________________________________________________________________________________________________________________



CODE:-
------
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
            {
                Scanner input=new Scanner(System.in);

                int a,b=0,count=0;

                System.out.println("Enter the number");

                a=input.nextInt();

                do {

                    count+=1;
                    a=a/10;
                }

                while(a!=0);

                System.out.println("The Number of Digit = "+count);
            }
            }

Output:
-------
Enter the number
4563
The Number of Digit = 4

Process finished with exit code 0
-----------------------------------------------------------------------------------------------------------------------------
4.38                    Write a program to check a number is Armstrong number or not
____________________________________________________________________________________________________________________________


CODE:-
------
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
            {
                Scanner input=new Scanner(System.in);

                int remainder,temp,sum=0;

                int number;

                System.out.println("Enter the number");

                number=input.nextInt();

                temp=number;

                do {

                    remainder = temp%10;

                    sum = sum + remainder*remainder*remainder;

                    temp = temp/10;
                }
                while(temp!=0);

                if ( number == sum )

                    System.out.println("Entered Number is an Armstrong Number");

                else

                    System.out.println("Entered Number is not an Armstrong Number");


            }
            }    

Output:-
--------
Enter the number
153
Entered Number is an Armstrong Number

Process finished with exit code 0

(Have a nice day and happy Coding)-------------------------------------------------






No comments:

Post a Comment

JAVA PROGRAMMING - OOPS CHAPTER 1

Object-Oriented Programming Chapter 1: Basics by Souradeep Roy Using IntelliJ IDEA Community version:- _____________________________________...