Basic Programs

Table of Contents

Program To Accept Name And Age And Then Print Them

				
					import java.util.Scanner;
class q1
{
    public static void main()
    {
        Scanner sc=new Scanner (System.in);
        String name;
        int age;
        System.out.println("Enter your name");
        name=sc.next();
        System.out.println("Enter your age");
        age=sc.nextInt();
        
        System.out.println("Name: "+name);
        System.out.println("Age: "+age);
    }
}
				
			

Program To Accept 2 Numbers And Find Their Sum And Product

				
					import java.util.Scanner;
class q2
{
    public static void main ()
    {
        Scanner sc=new Scanner (System.in);
        int num1;
        int num2;
        int sum;
        int product;
        
        System.out.println("Enter a number: ");
        num1=sc.nextInt();
        
        System.out.println("Enter another number: ");
        num2=sc.nextInt();
        
        sum=num1+num2;
        product=num1*num2;
        
        System.out.println("Sum is "+sum);
        System.out.println("Product is "+product);
    }
}
				
			

Program To Accept 3 Sides Of A Triangle And Find Its Perimeter

				
					import java.util.Scanner;
class q3
{
    public static void main()
    {
        Scanner sc=new Scanner(System.in);
        int a, b, c, p;
        
        System.out.println("Enter the length of one side: ");
        a=sc.nextInt();
        
        System.out.println("Enter the length of another side: ");
        b=sc.nextInt();
        
        System.out.println("Enter the length of the last side: ");
        c=sc.nextInt();
        
        p = a+b+c;
        
        System.out.println("Perimeter of the triangle is "+p);
    }
}
				
			

Program To Accept The Radius Of A Circle And Find Its Circumference

				
					import java.util.Scanner;
class q4
{
    public static void main()
    {
        Scanner sc=new Scanner (System.in);
        double r, c;
        System.out.println("Enter the radius: ");
        r=sc.nextDouble();
        
        c=2*3.14*r;
        
        System.out.println("Circumference of the circle is "+c);
    }
}
				
			

Program To Accept Principal, Rate, Time And Find Simple Interest

				
					import java.util.Scanner;
class q5
{
    public static void main()
    {
        Scanner sc=new Scanner (System.in);
        int p,r,t;
        double si;
        
        System.out.println("Enter the principal: ");
        p=sc.nextInt();
        
        System.out.println("Enter the rate: ");
        r=sc.nextInt();
        
        System.out.println("Enter the time: ");
        t=sc.nextInt();
        
        si=(p*r*t)/100;
        
        System.out.println("Simple Interest is "+si);
    }
}
				
			

Program To Accept The Temperature In Fahrenheit And Find The Celsius

				
					import java.util.Scanner;
class q6
{
    public static void main ()
    {
        Scanner sc=new Scanner (System.in);
        double f, c;
        System.out.println("Enter the temperature in fahrenheit: ");
        f=sc.nextDouble();
        
        c = 5/9*(f-32);
        
        System.out.println("The temperature in celcius is "+c);
    }
}
				
			

Program To Accept The Units, Tens And Hundreds Digit And Find The Number

				
					import java.util.Scanner;
class q7
{
    public static void main()
    {
        Scanner sc=new Scanner (System.in);
        int h,t,u,n;
        System.out.println("Enter a number in the hundereds place: ");
        h=sc.nextInt();
        
        System.out.println("Enter a number in the tens place: ");  
        t=sc.nextInt();
        
        System.out.println("Enter a number in the units place: ");
        u=sc.nextInt();
        
        n=h*100+t*10+u;
        
        System.out.println("The number is "+n);
    }
}
				
			

Program To Accept A 3 Digit Number And Find The Units, Tens And Hundreds Digit

				
					import java.util.Scanner;
class q8
{
    public static void main()
    {
        Scanner sc=new Scanner (System.in);
        int n,u,t,h;
        System.out.println("Enter a 3 digit number: ");             
        n=sc.nextInt();
        
        u=n%10;
        t=n%100/10;
        h=n/100;
        
        System.out.println("The number in the units place is "+u);
        System.out.println("The number in the tens place is "+t);
        System.out.println("The number in the hundreds place is "+h);
    }
}
				
			

Leave a Comment

Your email address will not be published. Required fields are marked *