0% found this document useful (0 votes)
11 views

Java Programs

Uploaded by

nivedhg2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java Programs

Uploaded by

nivedhg2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Write a program to print “National Public School , East” in one line and Your name & Your age in next line

import java.util.Scanner; public class Prg7


{
public static void main()
{
System.out.println("National Public School East");
System.out.print("Sreedevi R");
System.out.print("12");
}
}

2. Declare three variables to store your name , class and section and print the same as a proper sentence .
import java.util.Scanner;
public class Prg8
{
public static void main()
{
String name="James Gosling";
int c=7;
char s='B';
System.out.println("My name is "+name+" I study in "+c+s);
}
}

3. WAP to store your name and age. Display the same and the age after 5 years in proper sentence .

import java.util.Scanner;
public class Prg3
{
public static void main(String[] args)
{
String name="James Gosling";
int age=12;
int newage;
newage=age+5;
System.out.println("My name is "+name+" I am "+age+" years old");
System.out.println(" I will be "+newage+"years old after 5 years");
}
}

4. Write a program to store two numbers, display the sum, difference, product and quotient .

import java.util.Scanner;
public class Prg4
{
public static void main(String[] args)
{
int num1=7;
int num2=2;
int sum,diff,pro,quo;
sum = num1+num2;
diff=num1-num2;
pro=num1*num2;
quo=num1/num2;
System.out.println("The two numbers are "+num1+" and "+num2);
System.out.println(" The result of addition"+ sum);
System.out.println(" The result of subtraction"+ diff);
System.out.println(" The result of multiplication"+ pro);
System.out.println(" The result of division"+ quo);
}
}

5. Write a program to store a number, display the square of given number.

import java.util.Scanner;
public class Prg5
{
public static void main(String[] args)
{
int num1=7;
int sqr;
sqr=num1*num1;
System.out.println("The square of the number "+num1+" is "+sqr);
}
}

6. WAP to assign three numbers and print the sum of three numbers.

import java.util.Scanner;
public class Qtn3
{
public static void main(String[] args)
{
int a,b,c,d;
a=10;
b=18;
c=15;
d=a+b+c;
System.out.println(“The numbers are”+a+ “ ,”+b+ “ ,” +c);
System.out.print(“The sum is” + d);
}
}

7. WAP to accept three numbers and print the sum of three numbers
import java.util.Scanner;
public class prg10
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in); //System.in is a standard input stream
System.out.print("Enter first number- ");
int a= sc.nextInt();
System.out.print("Enter second number- ");
int b= sc.nextInt();
System.out.print("Enter third number- ");
int c= sc.nextInt();
int d=a+b+c;
System.out.println("Total= " +d);
}
}

8. WAP to accept a String value and print the same.

import java.util.Scanner;
public class prg11
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in); //System.in is a standard input stream
System.out.print("Enter a string: ");
String str= sc.nextLine(); //reads string
System.out.print("You have entered: "+str);
}
}
9. Program to accept

import java.util.Scanner;
public class prg11
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
// Reading a string
System.out.print("Enter your name: ");
String name = sc.nextLine();
// Reading an integer
System.out.print("Enter your age: ");
int age = sc.nextInt();
// Reading a double
System.out.print("Enter your height: ");
double height = sc.nextDouble ();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Height: " + height);
}
}

You might also like