//Document section – we write a description of the program
//Package statement – we add packages here
//Interface statement – we declare interface here
class Test // class definition
{
public static void main(String args[]) // main function definition
{
// program code
}
}
Example : Sum of two numbers
class AddTwoNumbers
{
public static void main(String[] args)
{
int num1 = 5, num2 = 15, sum;
sum = num1 + num2;
System.out.println("Sum of two numbers: "+sum);
}
}
Output:
Sum of these numbers: 20
Example: Sum of two numbers using Command line arguments
class AddTwoNumbers
{
public static void main(String[] args)
{
int num1, num2, sum;
num1 = Integer.parseInt(args[0]);
num2 = Integer.parseInt(args[1]);
sum = num1 + num2;
System.out.println("Sum of two numbers: "+sum);
}
}
Output:
Enter First Number:
12
Enter Second Number:
19
Sum of these numbers: 31
Example: Sum of two numbers using Scanner
import java.util.Scanner;
class AddTwoNumbers
{
public static void main(String[] args)
{
int num1, num2, sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter First Number: ");
num1 = sc.nextInt();
System.out.println("Enter Second Number: ");
num2 = sc.nextInt();
sum = num1 + num2;
System.out.println("Sum of two numbers: "+sum);
}
}
Output:
Enter First Number:
121
Enter Second Number:
19
Sum of these numbers: 140
//Package statement – we add packages here
//Interface statement – we declare interface here
class Test // class definition
{
public static void main(String args[]) // main function definition
{
// program code
}
}
Example : Sum of two numbers
class AddTwoNumbers
{
public static void main(String[] args)
{
int num1 = 5, num2 = 15, sum;
sum = num1 + num2;
System.out.println("Sum of two numbers: "+sum);
}
}
Output:
Sum of these numbers: 20
Example: Sum of two numbers using Command line arguments
class AddTwoNumbers
{
public static void main(String[] args)
{
int num1, num2, sum;
num1 = Integer.parseInt(args[0]);
num2 = Integer.parseInt(args[1]);
sum = num1 + num2;
System.out.println("Sum of two numbers: "+sum);
}
}
Output:
Enter First Number:
12
Enter Second Number:
19
Sum of these numbers: 31
Example: Sum of two numbers using Scanner
import java.util.Scanner;
class AddTwoNumbers
{
public static void main(String[] args)
{
int num1, num2, sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter First Number: ");
num1 = sc.nextInt();
System.out.println("Enter Second Number: ");
num2 = sc.nextInt();
sum = num1 + num2;
System.out.println("Sum of two numbers: "+sum);
}
}
Output:
Enter First Number:
121
Enter Second Number:
19
Sum of these numbers: 140
Example 1: convert temperature given in foreign heat to celsius
Solution:
import java.util.Scanner;
class FahrenheitToCelsius
{
public static void main(String[] args)
{
float temperature;
Scanner in = new Scanner(System.in);
System.out.println("Enter temperature in Fahrenheit");
temperature = in.nextInt();
temperature = ((temperature - 32)*5)/9;
System.out.println("Temperature in Celsius = " + temperature);
}
}
Output:
H:\>java FahrenheitToCelsius
Enter temperature in Fahrenheit
98
Temperature in Celsius = 36.666668
Solution:
import java.util.Scanner;
class FahrenheitToCelsius
{
public static void main(String[] args)
{
float temperature;
Scanner in = new Scanner(System.in);
System.out.println("Enter temperature in Fahrenheit");
temperature = in.nextInt();
temperature = ((temperature - 32)*5)/9;
System.out.println("Temperature in Celsius = " + temperature);
}
}
Output:
H:\>java FahrenheitToCelsius
Enter temperature in Fahrenheit
98
Temperature in Celsius = 36.666668
Example 2. program to calculate Area Of Rectangle.
Solution:
import java.util.Scanner;
class AreaOfRectangle
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of Rectangle:");
double length = scanner.nextDouble();
System.out.println("Enter the width of Rectangle:");
double width = scanner.nextDouble();
//Area = length*width;
double area = length*width;
System.out.println("Area of Rectangle is:"+area);
}}
Output:
Enter the length of Rectangle:
2
Enter the width of Rectangle:
8
Area of Rectangle is:16.0
Solution:
import java.util.Scanner;
class AreaOfRectangle
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of Rectangle:");
double length = scanner.nextDouble();
System.out.println("Enter the width of Rectangle:");
double width = scanner.nextDouble();
//Area = length*width;
double area = length*width;
System.out.println("Area of Rectangle is:"+area);
}}
Output:
Enter the length of Rectangle:
2
Enter the width of Rectangle:
8
Area of Rectangle is:16.0
Example 3:Program to calculate compound interest
Solution:
import java.util.Scanner;
class CompoundInterest
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter Principal :");
double p=s.nextInt();
System.out.println("Enter Rate of Interest:");
double r=s.nextInt();
System.out.println("Enter Time Period:");
double n=s.nextInt();
double ci=p *( Math.pow(1+0.01*r,n)-1);
double amt=p+ci;
System.out.println("Compound Interest after " + n + " years: "+ci);
System.out.println("Amount after " + n + " years: "+amt);
}
}
Output:
H:\>java CompoundInterest
Enter Principal :
25000
Enter Rate of Interest:
4
Enter Time Period:
5
Compound Interest after 5.0 years: 5416.322560000003
Amount after 5.0 years: 30416.322560000004
Solution:
import java.util.Scanner;
class CompoundInterest
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter Principal :");
double p=s.nextInt();
System.out.println("Enter Rate of Interest:");
double r=s.nextInt();
System.out.println("Enter Time Period:");
double n=s.nextInt();
double ci=p *( Math.pow(1+0.01*r,n)-1);
double amt=p+ci;
System.out.println("Compound Interest after " + n + " years: "+ci);
System.out.println("Amount after " + n + " years: "+amt);
}
}
Output:
H:\>java CompoundInterest
Enter Principal :
25000
Enter Rate of Interest:
4
Enter Time Period:
5
Compound Interest after 5.0 years: 5416.322560000003
Amount after 5.0 years: 30416.322560000004
No comments:
Post a Comment