Important java Program

Important java Program

Java programming Example


Question-1.

Write a program using a switch case to find the factor and factorial of a given number. 

If the choice is 1 find Factor and 2 find Factorial of the input number.
Solution:
import java.util.*;
public class Menu
{
    void main()
    {
        int ch,f,i,n;
        Scanner sc=new Scanner(System.in);
        System.out.println("1. Factor 2. Factorial:");
        ch=sc.nextInt();
        switch(ch)
        {
            case 1:
            System.out.println("Enter a number to print Factor:");
            n=sc.nextInt();
            for(i=1;i<=n;i++)
            {
                if(n%i==0)
                {
                    System.out.print(i+" ");
                }
            }
            break;
            case 2:
            System.out.println("Enter a number to print Factorial:");
            n=sc.nextInt();
            f=1;
            for(i=1;i<=n;i++)
            {
                f=f*i;
            }
            System.out.println("Factorial="+f);
            break;
            default:
            System.out.println("Wrong Choice");
        }
    }
}
Question-2.

Write a program to print the following pattern up to n lines.

A
AB
ABC
ABCD
Upto n number[1>=n<=26]
Solution:
import java.util.*;
public class LetterPattern
{
    void main()
    {
        int i,n,j;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter number 1-26 only");
        n=sc.nextInt();
        if(n>=1 && n<=26)
        {
            for(i=1;i<=n;i++)
            {
                for(j=65;j<=64+i;j++)
                {
                    System.out.print((char)j);
                }
                System.out.println();
            }
        }
        else
        {
            System.out.println("Pattern will not print");
        }
    }
}
Question-3.

Write a program to print the Fibonacci series up to n term.

Solution:
import java.util.*;
public class Fibonacci
{
    void main()
    {
        int f1,f2,fib,i,n;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a number:");
        n=sc.nextInt();
        f1=fib=0;
        f2=1;
        for(i=0;i<n;i++)
        {
            f1=f2;
            f2=fib;
            fib=f1+f2;
            System.out.print(fib+" ");
        }
    }
}
Question-4.

Write a program to input a number and find whether it is a Neon Number or not. 

A number is said to be a Neon Number. If it is equal to sum of digits of Square number.
92-> 81->8+1=9
Solution:
import java.util.*;
public class NeonNumber
{
    void main()
    {
        int n,m,a,s=0;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a number:");
        n=sc.nextInt();
        m=n*n;
        while(m!=0)
        {
            a=m%10;
            s=s+a;
            m=m/10;
        }
        if(s==n)
            System.out.println("It is a Neon Number");
        else
            System.out.println("It is not a Neon Number");
    }

}

Menu driven program in java with do-while loop

import java.util.*;
public class MenuDrivenWithDoWhileLoop
{
    static void menu()  // Method
    {        
        System.out.println("1. For Prime Number");
        System.out.println("2. For Sum of digits of a Givem number");
        System.out.println("3. For Factorial of a number");
        System.out.println("4. For Exit------->");
    }
    public static void main(String args[])
    {
        // Local variables
        int ch, num, m, sum=0, f, i, key=0;
        Scanner sc=new Scanner(System.in);
        // Do loop started
        do{
            menu();// call the menu method
            System.out.println("Enter your choice----->");
            ch=sc.nextInt();
            System.out.println("Enter a number:");
            num=sc.nextInt();
            switch(ch)
            {
                case 1:
                f=1;
                for(i=2;i<num;i++)
                {
                    if(num%i==0)
                    {
                        f=0; //Flag Variable
                        break;
                    }
                }
                if(f==1)
                    System.out.println("Prime Number");
                else
                    System.out.println("Not Prime Number");
                System.out.println("Press any key to continue");
                key=sc.nextInt();
                System.out.println("\f"); // form feed escap sequence
                break;
                case 2:
                while(num!=0)
                {
                    m=num%10;
                    sum=sum+m;
                    num=num/10;
                }
                System.out.println("Sum="+sum);
                System.out.println("Press (0-9) key to continue");
                key=sc.nextInt();
                System.out.println("\f");
                break;                
                case 3:
                f=1;
                for(i=1; i<=num; i++)
                {
                    f=f*i;
                }
                System.out.println("Factorial="+f);
                System.out.println("Press (0-9) key to continue");
                key=sc.nextInt();
                System.out.println("\f");
                break;
                case 4:
                System.exit(0);
                default:
                System.out.println("Wrong Choice");
                System.out.println("Press (0-9) key to continue");
                key=sc.nextInt();
                System.out.println("\f");
            }
        }while(ch != 4);
    }
}


SHARE THIS
Previous Post
Next Post