Some of the popular java Program:-


1. Factorial in JAVA

import java.util.Scanner;
public class factorial
{
    public static void main(String args[])
    {
        int i,n,fact=1;
        Scanner s1=new Scanner (System.in);
        System.out.print("Enter n=");
        n=s1.nextInt();
        for(i=1;i<=n;i++)
        {
            fact=fact*i;
        }
        System.out.print("Factorial n="+fact);
    }
}
output:-
Enter n=5
Factorial n=120
________________

*.Factorial using Recursion
import java.util.Scanner;
class pr24{
    public static void main(String args[])
    {
        int s,ans;
        Scanner s1=new Scanner(System.in);
        System.out.println("Enter n=");
        s=s1.nextInt();
        ans=fact(s);
        System.out.println("Factorial n="+ans);
    }
    static int fact(int n)
{
    if(n>=1){
        return n*fact(n-1);
    }
    else{
        return 1;
    }
}
}
_________________


2.Armstrong Number
import java.util.Scanner;
public class ars
{
    public static void main(String args[])
    {
       int n,temp,sum=0,r;
        Scanner s1=new Scanner (System.in);
        System.out.println("Enter n=");
        n=s1.nextInt();
        temp=n;
        while(n>0)
        {
            r=n%10;
            sum=sum+(r*r*r);
            n=n/10;
           
        }

         if(temp==sum)
        {
            System.out.println("The no.is an armstrong no.");
        }
        else
        {
            System.out.println("The no.is not an armstrong no.");
        }
       
    }
}
output:-
Enter n=153
The no. is an armstrong no.
_______________________________

3.Palindrome Number
import java.util.Scanner;
public class palindrome
{
    public static void main(String args[])
    {
        int n,sum=0,temp,r;
        Scanner s1= new Scanner(System.in);
        System.out.print("Enter n= ");
        n=s1.nextInt();
        temp=n;
        while(n>0)
        {
            r=n%10;
            sum=(sum*10)+r;
            n=n/10;
        }
        if(temp==sum)
        {
            System.out.print("Palindrome no.");
        }
        else{System.out.print("Not a Palindrome no.");
        }
    }
}    
output:-
Enter n=343
Palindrome no.
__________________

4.Prime Number
import java.util.Scanner;
public class prime_no
{
    public static void main (String args[])
    {
        int i,n,flag=0;
        Scanner s1=new Scanner (System.in);
        System.out.print("Enter the value of n=");
        n=s1.nextInt();
        for(i=2;i<n;i++)
        {
            if(n%i==0)
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
        {
            System.out.print(+n+" is not a prime no.");
        }
        else{
            System.out.print(+n+" is a prime no");
        }

    }
}
output:-
Enter the value of n=4
4 is not a prime no

5. Leap Year
import java.util.Scanner;
public class leapyear
{
    public static void main(String args[])
    {
        int year;
        boolean leap=false;
        Scanner s1=new Scanner(System.in);
        System.out.print("Enter year-");
        year=s1.nextInt();
        if(year%4==0 && year%100!=0 || year%400==0)
        {
            leap=true;
        }
        else
        {
            leap=false;
        }
        if(leap)
        {
            System.out.println(year+"is a leap year");
        }
        else
        {
            System.out.println(year+"is not a leap year");
        }
    }
}

output:-
Enter year-2004
2004 is a leap year

6.Write a program of restaurant system in which resttaurant
id,retaurant name and a data function displaymenu item.
Display a menu of 5 retaurants on a screen.

import java.util.Scanner;
class rest
{
    int restid,i,size;
    String rest_n;
    String m[]=new String[10];
    int p[]=new int[100];
    Scanner s1=new Scanner(System.in);
    void getdata()
    {
        System.out.print("Enter restaurant id-");
        restid=s1.nextInt();
        System.out.print("\nEnter restaurant name-");
        rest_n=s1.next();
    }
    void menu()
    {
        System.out.println("Tell the no. of item u want-");
        size=s1.nextInt();
        for(i=0;i<size;i++)
        {
            System.out.print("Enter Item"+(i+1)+"-");
            m[i]=s1.next();
            System.out.println("Enter Price-");
            p[i]=s1.nextInt();
        }
    }
    void displaymenu()
    {
        System.out.println("Restaurant Id-"+restid);
        System.out.println("Restaurant Name-"+rest_n);
        System.out.println("\n*MENU*\n");
        for(i=0;i<size;i++)
        {
            System.out.println("Item"+(i+1)+"-"+m[i]);
            System.out.println("Price-"+p[i]+"Rs");
        }
    }


};
class restaurant
{
    public static void main(String args[])
    {
        int i;
        Scanner sc=new Scanner(System.in);
        rest r[]=new rest[5];
        for(i=0;i<5;i++)
        {
            r[i]=new rest();
        }
        int ch;
        do{
            System.out.println("Restaurant Database System");
            System.out.println("1.Insert Information\n2.Display Menu\n3.Exit");
            System.out.println("Enter your choice-");
            ch=sc.nextInt();
            switch(ch)
            {
                case 1:
                for(i=0;i<5;i++)
                {
                    r[i].getdata();
                    r[i].menu();
                }
                break;
                case 2:
                for(i=0;i<5;i++)
                {
                    r[i].displaymenu();
                }
                break;
                case 3:
                System.out.println("Logout");
                break;
            }
        }while(ch<5);

    }
}
output:-
Restaurant Database System 1.Insert Information 2.Display Menu 3.Exit Enter your choice- 1 Enter restaurant id-1 Enter restaurant name-ss Tell the no. of item u want- 1 Enter Item1-khir Enter Price- 20 Enter restaurant id-2 Enter restaurant name-df Tell the no. of item u want- 1 Enter Item1-suji Enter Price- 22 Restaurant Database System 1.Insert Information 2.Display Menu 3.Exit Enter your choice- 2 Menu2 Item1-khir Price-20Rs Menu2 Item1-suji Price-22Rs

Comments

popular post

Design And Analysis Of Algorithm

Basic Programming in Python