Sharpen Your Skills: Programming Languages Practice Q&A

Programming practice questions

for, while, do while loop in Java

 
1

Question- Write a program to print its name 12 times by using for loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
for(int i=1;i<=12;i++)
{
System.out.println("Welcome to CSDT Patna.");
}

}

}
2

Question- Write a program to print 1 to 21 by using for loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
for(int i=1;i<=21;i++)
{
System.out.println(i);
}

}

}
3

Question- Write a program to print table of any given number by using for loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
for(int i=1;i<=10;i++)
{
System.out.println(n*i);

}

}

}

4

Question- Write a program to print sum of given series 1+2+3+4+.....+n. where n is natural number and enter by user. using for loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter nth Term");
int n=sc.nextInt();
int s=0;
for(int i=1;i<=n;i++)
{
s=s+i;

}
System.out.println("Sum of series is::"+s);
}

}
5

Question- Write a java program to find all even number from 1 to 100 using for loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{

for(int i=2;i<=100;i=i+2)
{

System.out.println(i);

}

}

}

--------------or---------------

package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{

for(int i=1;i<=100;i++)
{
if(i%2==0)
System.out.println(i);

}

}

}
6

Question- Write a java program to find all odd number from 1 to 100 using for loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{

for(int i=1;i<=100;i=i+2)
{

System.out.println(i);

}

}

}

--------------or---------------

package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{

for(int i=1;i<=100;i++)
{
if(i%2!=0)
System.out.println(i);

}

}

}

7

Question- Write a java program to find all even and odd number from 1 to 100 with massage using for loop. Eg. 2 is even, 7 is odd



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{

for(int i=1;i<=100;i++)
{
if(i%2==0)
{
System.out.println(i+" is Even");
}
else
{
System.out.println(i+" is Odd");
}

}

}

}
8

Question- Write a java program to find sum of even and odd number from 1 to 100.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
int es=0,ods=0;
for(int i=1;i<=100;i++)
{
if(i%2==0)
{
es=es+i;

}
else
{
ods=ods+i;

}

}
System.out.println("Sum of Even is"+es);
System.out.println("Sum of Odd is"+ods);
}

}
9

Question- Write a Java program to print sum of given series 1+3+5+7+.....+n. using for loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter nth Term");
int n=sc.nextInt();
int s=0;
for(int i=1;i<=n;i=i+2)
{
s=s+i;

}
System.out.println("Sum of Odd number series is::"+s);
}

}

-----------------------
or
----------------------
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter nth Term");
int n=sc.nextInt();
int s=0;
for(int i=1;i<=n;i=i++)
{
if(i%2!=0)
{
s=s+i;
}
}
System.out.println("Sum of Odd number series is::"+s);
}

}
10

Question- Write a Java program to print sum of given series 2+4+6+8.....+n. using for loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter nth Term");
int n=sc.nextInt();
int s=0;
for(int i=2;i<=n;i=i+2)
{
s=s+i;

}
System.out.println("Sum of Even series is::"+s);
}

}

-------------
or
----------
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter nth Term");
int n=sc.nextInt();
int s=0;
for(int i=1;i<=n;i++)
{
if(i%2==0)
s=s+i;

}
System.out.println("Sum of series is::"+s);
}

}
11

Question- Write a program to print its name 12 times by using while loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
int i=1;
while(i<=12)
{
System.out.println("Welcome to CSDT Patna.");
i++;
}

}

}
12

Question- Write a java program to print 1 to 21 by using while loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
int i=1;
while(i<=21)
{
System.out.println(i);
i++;
}

}

}
13

Question- Write a java program to print table of any given number by using while loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int i=1;
while(i<=10)
{
System.out.println(n*i);
i++;

}

}

}
14

Question- Write a java program to print sum of given series 1+2+3+4+.....+n. where n is natural number and enter by user. using While loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter nth Term");
int n=sc.nextInt();
int s=0;
int i=1;
while(i<=n)
{
s=s+i;
i++;
}
System.out.println("Sum of series is::"+s);
}

}
15

Question- Write a java program to find all even number from 1 to 100 using While loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{

int i=1;
while(i<=100)
{
if(i%2==0)
{
System.out.println(i);
}
i++;
}

}

}
16

Question- Write a java program to find all odd number from 1 to 100 using While loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{

int i=1;
while(i<=100)
{

System.out.println(i);
i=i+2;
}

}

}

17

Question- Write a java program to find all even and odd number from 1 to 100 with massage using While loop. Eg. 2 is even, 7 is odd



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{

int i=1;
while(i<=100)
{
if(i%2==0)
{
System.out.println(i+" is Even");
}
else
{
System.out.println(i+" is Odd");
}
i++;
}

}

}
18

Question- Write a java program to find sum of even and odd number from 1 to 100 by using while loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
int es=0,ods=0;
int i=1;
while(i<=100)
{
if(i%2==0)
{
es=es+i;

}
else
{
ods=ods+i;

}
i++;
}
System.out.println("Sum of Even is"+es);
System.out.println("Sum of Odd is"+ods);
}

}
19

Question- Write a Java program to print sum of given series 1+3+5+7+.....+n. using While loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter nth Term");
int n=sc.nextInt();
int s=0;
int i=1;
while(i<=n)
{
if(i%2!=0)
{
s=s+i;
}
i++;
}
System.out.println("Sum of Odd number series is::"+s);
}

}
20

Question- Write a Java program to print sum of given series 2+4+6+8.....+n. using While loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter nth Term");
int n=sc.nextInt();
int s=0;
int i=2;
while(i<=n)
{
s=s+i;
i=i+2;
}
System.out.println("Sum of Even series is::"+s);
}

}
21

Question- Write a JAVA program for finding factorial of any given number by using WHILE loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter number");
int n=sc.nextInt();
int f=1;
int i=1;
while(i<=n)
{

f=f*i;
i++;
}

System.out.println("Factorial  is::"+f);
}

}
22

Question- Write a java program for print 21 to 1 by using WHILE loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
int i=21;
while(i>=1)
{
System.out.println(i);
i--;
}

}

}
23

Question- Write a java program to print its name 12 times by using do while loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
int i=1;
do
{
System.out.println("Welcome to CSDT Patna.");
i++;
}
while(i<=12);
}

}
24

Question- Write a Java program to print 1 to 21 by using do while loop.



Answer-
mport java.util.Scanner;
class Test
{
public static void main(String []args)
{
int i=1;
do
{
System.out.println(i);
i++;
}
while(i<=21);
}

}
25

Question- Write a java program for print 21 to 1 by using do while loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
int i=21;
do
{
System.out.println(i);
i--;
}
while(i>=1);
}

}
26

Question- Write a java program to print table of any given number by using do while loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int i=1;
do
{
System.out.println(n*i);
i++;

}
while(i<=10);
}

}
27

Question- Write a java program to print sum of given series 1+2+3+4+.....+n. where n is natural number and enter by user. using do while loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter nth Term");
int n=sc.nextInt();
int s=0;
int i=1;
do
{
s=s+i;
i++;
}
while(i<=n);
System.out.println("Sum of series is::"+s);
}

}
28

Question- Write a java program to find all even number from 1 to 100 using do while loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{

int i=1;
do
{
if(i%2==0)
{
System.out.println(i);
}
i++;
}
while(i<=100);

}

}
29

Question- Write a java program to find all even number from 1 to 100 using do while loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{

int i=1;
do
{

System.out.println(i);
i=i+2;
}
while(i<=100);
}

}

30

Question- Write a java program to find all even and odd number from 1 to 100 with massage using do while loop. Eg. 2 is even, 7 is odd



Answer-
import java.util.Scanner;
class Test
{
public static void main(String []args)
{

int i=1;
do
{
if(i%2==0)
{
System.out.println(i+" is Even");
}
else
{
System.out.println(i+" is Odd");
}
i++;
}
while(i<=100);
}

}
31

Question- Write a java program to find sum of even and odd number from 1 to 100 by using do while loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
int es=0,ods=0;
int i=1;
do
{
if(i%2==0)
{
es=es+i;

}
else
{
ods=ods+i;

}
i++;
}
while(i<=100);
System.out.println("Sum of Even is"+es);
System.out.println("Sum of Odd is"+ods);
}

}
32

Question- Write a Java program to print sum of given series 1+3+5+7+.....+n. using do while loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter nth Term");
int n=sc.nextInt();
int s=0;
int i=1;
do
{
if(i%2!=0)
{
s=s+i;
}
i++;
}
while(i<=n);
System.out.println("Sum of Odd number series is::"+s);
}

}
33

Question- Write a Java program to print sum of given series 2+4+6+8.....+n. using do while loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter nth Term");
int n=sc.nextInt();
int s=0;
int i=2;
do
{
s=s+i;
i=i+2;
}
while(i<=n);
System.out.println("Sum of Even series is::"+s);
}

}
34

Question- Write a JAVA program for finding factorial of any given number by using do while loop.



Answer-
package csdtpatna;
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter number");
int n=sc.nextInt();
int f=1;
int i=1;
do
{

f=f*i;
i++;
}
while(i<=n);
System.out.println("Factorial  is::"+f);
}

}
35

Question- Write a java program that reads a set of integers, and then prints the sum of the even and odd integers by using do while.



Answer-
import java.util.Scanner;

public class ReadSetIntegers
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
     
        int number;
        char choice;
        int evenSum = 0;
        int oddSum = 0;
        
        do
        {
            System.out.print("Enter the number ");
            number = console.nextInt();
        
            if( number % 2 == 0)
            {
                evenSum += number;
            }
            else
            {
                oddSum += number;
            }
        
            System.out.print("Do you want to continue y/n? ");
            choice = console.next().charAt(0);
            
        }while(choice=='y' || choice == 'Y');
        
        System.out.println("Sum of even numbers: " + evenSum);
        System.out.println("Sum of odd numbers: " + oddSum);
    }  
}
36

Question- Write a java program to calculate HCF of Two given number by using do while.



Answer-
import java.util.Scanner;

public class FindHcf
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
     
        int dividend, divisor;
        int remainder, hcf = 0;
        
        System.out.print("Enter the first number ");
        dividend = console.nextInt();
        
        System.out.print("Enter the second number ");
        divisor = console.nextInt();        
        
        do
{
            remainder = dividend % divisor;
            
            if(remainder == 0)
            {
                hcf = divisor;
            }
    else
            {
                dividend = divisor;
                divisor = remainder;
    }
            
        }while(remainder != 0);

        System.out.println("HCF: " + hcf);
    }  
}
37

Question- Write a do-while loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user whether he or she wishes to perform the operation again. If so, the loop should repeat; otherwise it should terminate.



Answer-
import java.util.Scanner;

public class SumAgain
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
     
        int number1, number2;
        char choice;
        
        do
        {
            System.out.print("Enter the first number ");
            number1 = console.nextInt();
          
            System.out.print("Enter the second number ");
            number2 = console.nextInt();
            
            int sum = number1 + number2;
            System.out.println("Sum of numbers: " + sum);
        
            System.out.print("Do you want to continue y/n? ");
            choice = console.next().charAt(0);
            
            System.out.println();
            
        }while(choice=='y' || choice == 'Y');
    }  
}
38

Question- Write a java program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered by using do while..



Answer-
import java.util.Scanner;


public class CountNumbers
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
        
        int number,          
            countPositive = 0, 
            countNegative = 0,
            countZero = 0;

        char choice;
        do
        {
            System.out.print("Enter the number ");
            number = console.nextInt();
        
            if(number > 0)
            {
                countPositive++;
            }
            else if(number < 0)
            {
                countNegative++;
            }
            else
            {
                countZero++;
            }
        
            System.out.print("Do you want to continue y/n? ");
            choice = console.next().charAt(0);
            
        }while(choice=='y' || choice == 'Y');
        
        System.out.println("Positive numbers: " + countPositive);
        System.out.println("Negative numbers: " + countNegative);
        System.out.println("Zero numbers: " + countZero);
    }  
}

39

Question- Write a java program to enter the numbers till the user wants and at the end the program should display the largest and smallest numbers entered by using do while Loop.



Answer-
import java.util.Scanner;

public class FindMaxMin
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
        
        int number;
        int max = Integer.MIN_VALUE;  // Intialize max with minimum value
        int min = Integer.MAX_VALUE;  // Intialize min with maximum value

        char choice;
        do
        {
            System.out.print("Enter the number ");
            number = console.nextInt();
        
            if(number > max)
            {
                max = number;
            }
            
            if(number < min)
            {
                min = number;
            }
        
            System.out.print("Do you want to continue y/n? ");
            choice = console.next().charAt(0);
            
        }while(choice=='y' || choice == 'Y');
        
        System.out.println("Largest number: " + max);
        System.out.println("Smallest number: " + min);
    }  
}
40

Question- Write a java program that generates a random number and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." The program should use a do while loop that repeats until the user correctly guesses the random number.



Answer-
import java.util.Scanner;

public class GuessMyNumber
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
        
        int number, // To hold the random number
            guess,  // To hold the number guessed by user
            tries = 0; // To hold number of tries
       
        number = (int) (Math.random() * 100) + 1; // get random number between 1 and 100
        
        System.out.println("Guess My Number Game");
        System.out.println();
        
        do
        {
            System.out.print("Enter a guess between 1 and 100 : ");
            guess = console.nextInt();
                
            tries++;
                
        if (guess > number)
        {
            System.out.println("Too high! Try Again");
        }
        else if (guess < number)
        {
            System.out.println("Too low! Try Again");
        }
        else
        {
    System.out.println("Correct! You got it in " + tries + " guesses!");
        }
        
        }while (guess != number);
    }  
}
41

Question- Two numbers are entered through the keyboard. Write a java program to find the value of one number raised to the power of another. (Do not use Java built-in method)



Answer-
import java.util.Scanner;

public class PowerDemo
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
     
        int base;
        int power;
        int result = 1;
        
        System.out.print("Enter the base number ");
        base = console.nextInt();
        
        System.out.print("Enter the power ");
        power = console.nextInt();

        for(int i = 1; i <= power; i++)
        {
    result *= base;
        }

        System.out.println("Result: "+ result);
    }
}
42

Question- Write a java program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number.



Answer-
import java.util.Scanner;

public class TestPrime
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
     
        int number;
        
        System.out.print("Enter the positive integer ");
        number = console.nextInt();
        
        boolean flag = true;
        
        for(int i = 2; i < number; i++)
{
    if(number % i == 0)
            {
                flag = false;
                break;
            }
        }

if(flag && number > 1)
        {
            System.out.println("Number is prime");
        }
else
        {
            System.out.println("Number is not prime");
        }
        
    }  
}
43

Question- Write a Java program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )



Answer-
public class ArmstrongNumber
{
    public static void main(String[] args)
    {
        int digit1,  // To hold first digit (Ones) of number 
            digit2,  // To hold second digit (Tens) of number
            digit3;  // To hold third digit (Hundreds) of number

for(int number = 1; number <= 500; number++)
{
            int temp = number;
    digit1 = temp % 10;

            temp = temp / 10;
            digit2 = temp % 10;
            
            temp = temp / 10;
            digit3 = temp % 10;

    if(digit1*digit1*digit1 + digit2*digit2*digit2 + digit3*digit3*digit3 == number)
            {
        System.out.println(number);
            }
}
    }  
}
44

Question- Write a Java program to print Fibonacci series of n terms where n is input by user : 0 1 1 2 3 5 8 13 24 .....



Answer-
import java.util.Scanner;

public class FibonacciSeries
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
        
        int number;  // To hold number of terms

        int firstTerm = 0,
            secondTerm = 1,
            thirdTerm;
 
        System.out.print("Enter number of terms of series : ");
        number = console.nextInt();
 
        System.out.print(firstTerm + " " + secondTerm + " ");
 
        for(int i = 3; i <= number; i++)
{
            thirdTerm = firstTerm + secondTerm;
            System.out.print(thirdTerm + " ");
            firstTerm = secondTerm;
            secondTerm = thirdTerm;
}
    }  
}
45

Question- Write a Java program to calculate the sum of following series where n is input by user. 1 + 1/2 + 1/3 + 1/4 + 1/5 +…………1/n.



Answer-
import java.util.Scanner;

public class SumOfSeries
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
        
        int number;  // To hold number of terms

        double sum = 0;

        System.out.print("Enter number of terms of series : ");
        number = console.nextInt();
  
        for(int i = 1; i <= number; i++)
{
            sum += 1.0/i;
}
        
        System.out.println("sum: " + sum);
    }  
}
46

Question- Compute the natural logarithm of 2, by adding up to n terms in the series 1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n. where n is a positive integer and input by user.



Answer-
import java.util.Scanner;

public class Ln2
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
        
        int number;  // To hold number of terms

        System.out.print("Enter number of terms of series : ");
        number = console.nextInt();

        double sum = 0;
        int sign = 1; 
        
        for(int i = 1; i <= number; i++)
{
            sum += (1.0 * sign) / i;
            sign *= -1;
}
        
        System.out.println("log2: " + sum);
    }  
}
47

Question- Write a Java program for input integer number from user and then outputs the number with the digits reversed. For example, if the input is 1234, the output should be 4321.



Answer-
import java.util.Scanner;

public class FindReverseOfNumber
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
     
        int rev = 0,rem;
        
        System.out.print("Enter a  number ");
        int n = sc.nextInt();
        
        while(n>0)
        {
        rem=n%10;
        rev=rev*10+rem;
        n=n/10;

         }
        System.out.println("Reverse of number is: "+ rev);
    }
}

48

Question- Write a java program for checking enter number is palindrome or not. for example. Input 121 then its reverse is 121 then number is palindrome.



Answer-
import java.util.Scanner;

public class CheckNumberPelidrom
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
     
        int rev = 0,rem;
        
        System.out.print("Enter a  number ");
        int n = sc.nextInt();
        int t=n; 
        //Enter number n is store in t variable becz n is lastly remain Zero(0) 
        while(n>0)
        {
        rem=n%10;
        rev=rev*10+rem;
        n=n/10;

         }
       if(t==rev)
       {
        System.out.println("Number is Palindrome: "+ t);

       }
     else
       {
       System.out.println("Number is  not Palindrome: "+ t);

       }
    }
}
49

Question- Write a java program for finding palindrome numbers between 10 to 1000 and also find how many numbers are palindrome.



Answer-
import java.util.Scanner;

public class FindPelidromNumber
{
    public static void main(String[] args)
    {
        
        int rev,t count=0;
        
        for(int n=10;n<=1000;n++)
       { 
         rev = 0;
         t=n; 
        //Enter number n is store in t variable becz n is lastly remain Zero(0) 
        while(n>0)
        {
        rev=rev*10+n%10;
        n=n/10;

         }
       if(t==rev)
       { count++;
        System.out.println("Number is Pelidrom: "+ t);

       }
      
     }
System.out.println("Total Numbers of Pelidrom is: "+ count);
    }
}
50

Question- Write a Java Program for checking input number is Armstrong or not. for example. input number is 153 then 1*1*1+5*5*5+3*3*3=153 then enter number is Armstrong.



Answer-
import java.util.Scanner;

public class CheckArmstrongNumber
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
     
        int sum = 0,rem;
        
        System.out.print("Enter a  number ");
        int n = sc.nextInt();
        int t=n; 
        //Enter number n is store in t variable becz n is lastly remain Zero(0) 
        while(n>0)
        {
        rem=n%10;
        sum=sum+rem*rem*rem;
        n=n/10;

         }
       if(t==sum)
       {
        System.out.println("Number is Armstrong: "+ t);

       }
     else
       {
       System.out.println("Number is  not Armstrong: "+ t);

       }
    }
}
51

Question- Write a java program for finding HCF of enter two numbers.



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner input=new Scanner(System.in);
        System.out.println("Enter 1st number");
int a= input.nextInt();
        System.out.println("Enter 2nd number");
     int b=input.nextInt();
int hcf=0;
     hcf=(a>b)?b:a;
     while(true)
       {
     if(a%hcf==0 && b%hcf==0)
       {
       System.out.println("HCF is "+hcf);
       break;
       }
       --hcf;     
       } 
    }
    
}
52

Question- Write a java program for finding HCF and LCM of enter two numbers.



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner input=new Scanner(System.in);
        System.out.println("Enter 1st number");
int a= input.nextInt();
        System.out.println("Enter 2nd number");
     int b=input.nextInt();
int x=a;
       int y=b;
       while(y!=0)
       {
        int t=y;
         y=x%y;
         x=t;
       }
       int hcf=x;
       int lcm=(a*b)/hcf;
       System.out.println("HCF is "+hcf);
       System.out.println("LCM is "+lcm);
       
    }
    
}
53

Question- Write a java program for finding sum of digit of an enter number.



Answer-
import java.util.Scanner;

public class FindSumOfDigits
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
     
        int sum = 0,rem;
        
        System.out.print("Enter a  number ");
        int n = sc.nextInt();
        
        while(n>0)
        {
        rem=n%10;
        sum=sum+rem;
        n=n/10;

         }
        System.out.println("Sum of digits is: "+ sum);
    }
}
54

Question- Pattern-1



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number of row");
       int a=in.nextInt();
        int j;
      for(int i = 1; i<=a; i++)
      {
         for(j=0; j<i; j++)
         System.out.print(i+" ");
           System.out.println();
       }
    }
    
}


Output:

Enter number of row 4

2 2 
3 3 3 
4 4 4 4 
55

Question- Pattern-2



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter number of row ");
       int n=sc.nextInt();
       for(int i=1;i<=n;i++){
         for(int j=1;j<=i;j++){
           System.out.print(j+" ");
         }
         System.out.println();
         }
    }
    
}


Output:

Enter number of row 4
1 2 
1 2 3 
1 2 3 4 

56

Question- Pattern-3



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter number of row ");
       int rows = sc.nextInt();
       for(int i =1;i<=rows;i++){
         for(int j=1;j<=i;j++){
           System.out.print(j+" ");
         }
         System.out.println();
       }
       for(int i=rows-1;i>=1;i--){
         for(int j = 1;j<=i;j++){
           System.out.print(j+" ");
         }
         System.out.println();
       }
    }
    
}


Output:

Enter number of row 4

1 2 
1 2 3 
1 2 3 4 
1 2 3 
1 2 

57

Question- Pattern-4



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter number of row ");
       int rows = sc.nextInt();
       for(int i = 1;i<=rows;i++)
       {
         for(int j= rows;j>=i;j--)
         {
           System.out.print(j+" ");
         }
         System.out.println();
       }
       
    }
    
}


Output:-

Enter number of row 4

4 3 2 1 
4 3 2 
4 3 
58

Question- Pattern-5



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter number of row ");
       int n = sc.nextInt();
       for(int i=n;i>=1;i--)
          {
         for(int j=n;j>=i;j--)
            {
          System.out.print(j+" ");
          }
       System.out.println();
       }
       
    }
    
}


Output:-

Enter number of row 5

5 4 
5 4 3 
5 4 3 2 
5 4 3 2 1 
59

Question- Pattern-6



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter number of row ");
       int n = sc.nextInt();
       for(int i = n; i > 0; i--)
        {
          for(int j = i; j > 0; j--)
       {
       System.out.print(j +" ");
       }
          System.out.println();
        }
       
    }
    
}

Output:

Enter number of row 4

4 3 2 1 
3 2 1 
2 1 

60

Question- Pattern-7



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter number of row ");
       int n = sc.nextInt();
       for(int i=n;i>=1;i--)
       {
         for(int j=1;j<=i;j++)
         {
           System.out.print(j+" ");
         }
         System.out.println();
       }
       
       for(int i=2;i<=n;i++)
       {
         for(int j=1;j<=i;j++)
           System.out.print(j+" ");
       System.out.println();
       }
    }
    
}

output:- 

Enter number of row 5
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

61

Question- Pattern-8



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter number of row ");
       int n = sc.nextInt();
        for(int i=1;i<=n;i++){
         int j=1;
         while(j<i)
           System.out.print(j+++" ");
         while(j>0)
           System.out.print(j--+" ");
         System.out.println();
       }
    }
    
}


Output:-

Enter number of row 5
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
62

Question- Pattern-9



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter number of row ");
       int n = sc.nextInt();
        
        for(int i=1;i<=n;i++)
        {
          for(int j=i;j>=1;j--)
          {
            System.out.print(j+" ");
          }
          System.out.println();
        }
    }
    
}

Output:-

Enter number of row 5

2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1 
63

Question- Pattern-10



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter number of row ");
       int a = sc.nextInt();
        
        for(int i=0;i<a+1;i++){
         for(int j=0;j<i;j++){
           if(j%2==0){
             System.out.print("1");
             }
           else
             {    
             System.out.print("0");
             }
           }
         System.out.println();
         }  
    }
    
}


Output:-

Enter number of row 5

1
10
101
1010
10101

64

Question- Pattern-11



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter number of row ");
      
        int rows = sc.nextInt();
        for (int i = 1; i <= rows; i++) 
        {
            for (int j = 1; j <= rows-i; j++)
            {
                System.out.print(1);
            }
             
            for (int j = 1; j <= i; j++)
            {
                System.out.print(i);
            }
             
            System.out.println();
        }
    }
    
}


Output:-

Enter number of row 4

1111
1122
1333
4444
65

Question- Pattern-12



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter number of row ");
      
        int rows = sc.nextInt();
        for (int i = 0; i < rows; i++) 
        {
            for (int j = 0; j < rows; j++)
            {
              if(i==j)
                System.out.print(i);
          else
            System.out.print(0);
        }
            System.out.println();
        }
    }
    
}


Output:

Enter number of row 5

00000
01000
00200
00030
00004
66

Question- Pattern-13



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter number of row ");
      
       int n = sc.nextInt();
      
       int i,j;
       int k=n;
    
       for(i=1;i<=n;i++)
       {
         int temp = i;
         System.out.print(i+" ");
       for(j=k-1;j>n-i;j--)
       {
        System.out.print((temp+j+" "));
         temp += j;
          
         
       }
         System.out.println();
       } 
    }
    
}


Output:-

Enter number of row 4

2 5 
3 6 8 
4 7 9 10 

67

Question- Pattern-14



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter number of row ");
      
       int n=sc.nextInt();
       for(int i=0;i<=n;i++){
       for(int j=0;j<((2*i)-1);j++){
            System.out.print("*");
            }
         System.out.println("");
       }
    }
    
}


Output:-

Enter number of row 5

*
***
*****
*******
*********
68

Question- Pattern-15



Answer-
package program;
import java.util.Scanner;

/**
 *
 * @author Ravi from CSDT Patna
 */
public class Program 
   {
 public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter number of row ");
      
       int n=sc.nextInt();
      for(int i=n;i>=0;--i){
       for(int j=1;j<2*i;j++){
         System.out.print("*");
           }
         System.out.println();
}
    }
    
}

Output:-

Enter number of row 5

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


69

Question- Write a Java program for print table from 1 to nth. where n is integer number.



Answer-

/**
 *
 * @author Ravi from CSDT
 */

 package table_program;
 import java.util.Scanner;

 public class Table_Program 
 {
    
    public static void main(String[] args) 
    {
        // Table Program in Java Language from 1 to nth.
        
        
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter nth term Number, which you want to display Table. ");
        int n = sc.nextInt();
        
        for(int num=1;num<=n;num++)
        {
           System.out.println("Table of  "+num); 
        for(int i = 1; i <=10; ++i)
        {
          System.out.println(num+" * "+i+" = "+(num*i));  
        }

        
            System.out.println(" ");
        
    }

    }
 }
70

Question- Write a Java program for check enter number is prime or not prime number.



Answer-

/**
 *
 * @author Ravi from CSDT
 * Prime number is a number that is greater than 1 and divided by 1 or itself only.
 * For example 2, 3, 5, 7, 11, 13, 17,19,23,29.... are the prime numbers.
 * Note:- 0 and 1 are not prime numbers.
 * 2 is the only even prime number because all the other even numbers can be divided by 2.
 */

package prime_number_java_program;
import java.util.Scanner;

public class Prime_Number_Java_Program 
{
    
    public static void main(String[] args) 
    {
        // Prime Number Program in Java Language.
        
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter an integer Number ");
        int num = sc.nextInt();
        boolean flag = false;
        for(int i = 2; i <= num/2; ++i)
        {
            // condition for checking non-prime number
            if(num % i == 0)
            {
                flag = true;
                break;
            }
        }

        if (!flag)
            System.out.println(num + " is a prime number.");
        else
            System.out.println(num + " is not a prime number.");
    }
}
        
71

Question- Write a Java program for check prime or not prime number from 2 to nth term, where n is natural number.



Answer-
/**
 *
 * @author Ravi from CSDT
 */

package prime_number_java_program;
import java.util.Scanner;

public class Prime_Number_Java_Program 
{
    
    public static void main(String[] args) 
    {
        // Prime Number Program in Java Language.
        
        
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter nth term Number, which you want to check. ");
        int n = sc.nextInt();
        
        for(int num=2;num<=n;num++)
        {
            boolean flag = false;
        for(int i = 2; i <= num/2; ++i)
        {
            // condition for checking non-prime number
            if(num % i == 0)
            {
                flag = true;
                break;
            }
        }

        if (!flag)
            System.out.println(num + " is a prime number.");
        
    }
    }
}
72

Question- Write a Java Program for Display Fibonacci series using for loop.



Answer-
/**
 * @author Ravi From CSDT
 * The Fibonacci series is a series where the next term is the sum of pervious two terms.
 * The first two terms of the Fibonacci sequence is 0 followed by 1.
 * The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
 */

package fibonacci_series;

public class Fibonacci_Series 
 {

    public static void main(String[] args) 
    {
        // Program for Display Fibonacci series
       int n = 20, a = 0, b = 1;
        System.out.println("First " + n + " terms: ");

        for (int i = 1; i <= n; ++i)
        {
            System.out.print(a + " + ");

            int sum = a + b;
            a = b;
            b = sum;
        }
    }
}
 
73

Question- Write a Java Program for Display Fibonacci series upto a given number (instead of terms).



Answer-
public class Fibonacci {

    public static void main(String[] args) 
    {

        int n = 100, a = 0, b = 1;
        
        System.out.print("Upto " + n + ": ");
        while (a <= n)
        {
            System.out.print(a + " + ");

            int sum = a + b;
            a = b;
            b = sum;
        }
    }
}

==============
When you run the program, the output will be:

Upto 100: 0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55 + 89 + 

Instead of displaying the series upto a specific number, this program displays it until a given number (100).
1 2