0% found this document useful (0 votes)
17 views16 pages

Java Programs Without Scanner Class

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

Java Programs Without Scanner Class

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

Java Programs: Write Java program without using Scanner class:

1. To add two numbers.


public class p1
{
public static void main(String args[])
{
int a=10,b=20;
int sum=a+b;
[Link]("sum = "+sum);
}
}
2. To subtract one number from another.
public class p2
{
public static void main(String args[])
{
int a=10,b=20;
int diff=a-b;
[Link]("Difference= "+diff);
}
}
3. To multiply two numbers.
public class p3
{
public static void main(String args[])
{
int a=10,b=20;
int prod=a*b;
[Link]("product="+prod);
}
}
4. To divide one number by another and print the quotient.
public class p4
{
public static void main(String args[])
{
int a=10,b=2;
int quotient=a/b;
[Link]("Quotient = "+quotient);
}
}
5. To divide one number by another and print the remainder.
public class p5
{
public static void main(String args[])
{
int a=10,b=2;
int remainder=a%b;
[Link]("Remainder="+remainder);
}
}
6. To add three numbers and print the sum.
public class p6
{
public static void main(String args[])
{
int a=10,b=2,c=4;
int sum=a+b+c;
[Link]("sum ="+sum);
}
}
7. To multiply three numbers and print the product.
public class p7
{
public static void main(String args[])
{
int a=10,b=2,c=4;
int product=a*b*c;
[Link]("product ="+product);
}
}
8. To print area of a square.
public class p8
{
public static void main(String args[])
{
int side=10;
int area = side * side;
[Link]("Area of square ="+area);
}
}
9. To print perimeter of a square.
public class p9
{
public static void main(String args[])
{
int side=10;
int perimeter =4*side;
[Link]("perimeter of square ="+ perimeter);
}
}
[Link] print area of a rectangle.
public class p10
{
public static void main(String args[])
{
int l=10,b=20;
int area = l * b;
[Link]("Area of rectangle ="+area);
}
}
[Link] print perimeter of a rectangle.
public class p11
{
public static void main(String args[])
{
int l=10 , b=20;
int perimeter=2*(l+b);
[Link]("perimeter of rectangle ="+perimeter);
}
}
[Link] print area of a triangle.
public class p12
{
public static void main(String args[])
{
int b=10,h=5;
double area =(1.0/2.0)*b*h;
[Link]("Area of triangle ="+area);
}
}
[Link] print volume of a cuboid.
public class p13
{
public static void main(String args[])
{
int l=10,b=5,h=6;
int volume = l * b * h;
[Link]("Volume of cuboid= "+volume);
}
}
[Link] print area of a circle.
public class p14
{
public static void main(String args[])
{
int r=12;
double area = 3.14 * r *r;
[Link]("Area of circle ="+area);
}
}
[Link] print circumference of a circle.
public class p15
{
public static void main(String args[])
{
int r=12;
double circum = 2 * 3.14 * r;
[Link]("Circumference of circle ="+circum);
}
}
[Link] print average marks of three subjects.
public class p16
{
public static void main(String args[])
{
int m1,m2,m3,total;
double average;
m1=99;
m2=98;
m3=96;
total=m1+m2+m3;
average=total/3.0;
[Link]("Total ="+total);
[Link]("Average ="+average);
}
}
[Link] print your name, father’s name and surname in three different lines.
public class p17
{
public static void main(String args[])
{
[Link](" Diya ");
[Link](" Dilip ");
[Link](" Rathod ");
}
}
[Link] assign your roll no, name and section to a variable and print it.
//[Link] assign your roll no, name and section to a variable and print it.
public class p18
{
public static void main(String args[])
{
int rno=10;
String name = "Dev";
char section='M';
[Link]("Roll number = " +rno);
[Link](" Name = "+ name);
[Link](" Section = "+section);
}
}
[Link] add four numbers and print the average.
public class p19
{
public static void main(String args[])
{
int num1,num2,num3,num4;
num1=16;
num2=12;
num3=10;
num4=8;
double average=(num1+num2+num3+num4)/4.0;
[Link]("Average of four numbers = "+average);
}
}
[Link] print area of a Rhombus.
public class p20
{
public static void main(String args[])
{
int d1=10,d2=20;
double rhombus = (1.0/2.0)*d1*d2;
[Link]("Area of rhombus = "+ rhombus);
}}

[Link] print area of an isosceles triangle.

[Link] print time period of simple pendulum.

public class p22

public static void main(String args[])

{
int l=10,g=5;
double T= 2*3.14*[Link](l/g);
[Link]("time period of simple pendulum "+T);
}
}
[Link] print the value of S=1/2 at2 .
public class p23
{

public static void main(String args[])

int a=10,t=2;

double s=(1.0/2.0)*a*t*t;

[Link]("s = "+s);

[Link] print velocity.


public class p24

public static void main(String args[])

int distance=100;

int time=2;

double velocity=distance/time;

[Link]("Velocity = "+velocity);

[Link] print total surface area of a cuboids.

public class p25

public static void main(String args[])

int l=10,b=5,h=6;

int TSA =2*(l*h+l*b+h*b);

[Link]("Total surface area of cuboid= "+TSA);

}
Write Java programs using Scanner class:

1. To print area of a square.


import [Link].*;
public class p1
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter side");
double side=[Link]();
double area= side*side;
[Link]("Area of square ="+area);
}}
2. To print perimeter of a square.
import [Link].*;
public class p2
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter side");
double side=[Link]();
double perimeter=4*side;
[Link]("perimeter of square ="+ perimeter);
}
}
3. To print area of a rectangle.
import [Link].*;
public class p3
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter l");
double l=[Link]();
[Link]("Enter b");
double b=[Link]();
double area=l*b;
[Link]("Area of rectangle = "+area);
}
}
4. To print perimeter of a rectangle.
import [Link].*;
public class p4
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter l");
double l=[Link]();
[Link]("Enter b");
double b=[Link]();
double perimeter=2*(l+b);
[Link]("Perimeter of rectangle = "+perimeter);
}
}
5. To print area of a triangle.
import [Link].*;
public class p5
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter b");
double b=[Link]();
[Link]("Enter h");
double h=[Link]();
double area=(1.0/2.0)*b*h;
[Link]("Area of triangle = "+area);
}}
6. To print area of a circle.
import [Link].*;
public class p6
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter r");
double r=[Link]();
double area= 3.14*r*r;
[Link]("Area of Circle = "+area);
}
}
7. To print circumference of a circle.
import [Link].*;
public class p7
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter r");
double r=[Link]();
double circum=2*3.14*r;
[Link]("Circumference of circle = "+circum);
}
}
8. To print volume of a cuboid.
import [Link].*;
public class p8
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter l");
double l=[Link]();
[Link]("Enter b");
double b=[Link]();
[Link]("Enter h");
double h=[Link]();
double volume=l*b*h;
[Link]("Volume of cuboid ="+volume);
}
}
9. To print area of a semi-circle.
import [Link].*;
public class p9
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter r");
double r=[Link]();
double area= (1.0/2.0)*3.14*r*r;
[Link]("Area of semiCircle = "+area);
}}
[Link] print area of an equilateral triangle.
import [Link].*;
public class p10
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a");
double a=[Link]();
double area=([Link](3)/4)*a*a;
[Link]("Area of Equilateral triangle = "+area);
}}
[Link] print area of a triangle with Heron’s formula.
import [Link].*;
public class p11
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a");
double a=[Link]();
[Link]("Enter b");
double b=[Link]();
[Link]("Enter c");
double c=[Link]();
double s=(a+b+c)/2.0;
double area=[Link](s*(s-a)*(s-b)*(s-c));
[Link]("Area of triangle = "+area);
}}
[Link] print time period of a simple pendulum.
import [Link].*;
public class p12
{ public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter l");
double l=[Link]();
[Link]("Enter g");
double g=[Link]();
double T=2*3.14*[Link](l/g);
[Link]("time period of a simple pendulum "+T);
}}
[Link] print diagonal of a cuboid.
import [Link].*;
public class p13
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter l");
double l=[Link]();
[Link]("Enter b");
double b=[Link]();
[Link]("Enter h");
double h=[Link]();
double diagonal=[Link](l*l+b*b+h*h);
[Link]("diagonal of a cuboid= "+diagonal);
}}
[Link] print volume of a cylinder.
import [Link].*;
public class p14
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter r");
double r=[Link]();
[Link]("Enter h");
double h=[Link]();
double volume=3.14*r*r*h;
[Link]("Volume of cylinder= "+volume);
}}
[Link] print volume of a sphere.
import [Link].*;
public class p15
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter r");
double r=[Link]();
double volume=(4.0/3.0)*3.14*r*r*r;
[Link]("Volume of Sphere= "+volume);
}}
[Link] print Simple Interest and Amount.
import [Link].*;
public class p16
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter p");
double p=[Link]();
[Link]("Enter r");
double r=[Link]();
[Link]("Enter t");
double t=[Link]();
double si=(p*r*t)/100.0;
double A=si+p;
[Link]("Simple Interest= "+si);
[Link]("Amount = "+A);
}
}
[Link] print value of c. where c=a2 +b2 +2ab.
import [Link].*;
public class p17
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a");
double a=[Link]();
[Link]("Enter b");
double b=[Link]();
double c=a*a+b*b+2*a*b;
[Link]("c="+c);
}
}
[Link] print value of c. where c=a3 + b3 + 3ab(a+b)
import [Link].*;
public class p18
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a");
double a=[Link]();
[Link]("Enter b");
double b=[Link]();
double c=a*a*a+b*b*b+3*a*b*(a+b);
[Link]("c="+c);
}}
[Link] print value of c. where c=a2 + b2 + c2 + 2(ab + bc +ca)
import [Link].*;
public class p19
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a");
double a=[Link]();
[Link]("Enter b");
double b=[Link]();
[Link]("Enter c");
double c=[Link]();
double d=a*a+b*b+c*c+2*(a*b+b*c+c*a);
[Link]("d ="+d);
}}
[Link] print temperature from Fahrenheit to Celcius.
import [Link].*;
public class p20
{
public static void main (String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a");
double Fahrenheit=[Link]();
double Celsius = ((Fahrenheit-32)*5)/9;
[Link]("Temperature in celsius is: "+Celsius);
}}
[Link] print third angle of a triangle when two are given.
import [Link].*;
public class p21
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a");
double a=[Link]();
[Link]("Enter b");
double b=[Link]();
double c=180-(a+b);
[Link]("third angle of a triangle = "+c);
}
}
[Link] print area of an isosceles triangle.
[Link] print height of the triangle.
import [Link].*;
public class p23
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter b");
double b=[Link]();
[Link]("Enter area");
double area=[Link]();
double h=2*(area/b);
[Link]("Height of the Triangle= "+h);
}
}
[Link] print perimeter of a semi circle.
import [Link].*;
public class p24
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter r");
double r=[Link]();
double perimeter=3.14*r+2*r;
[Link]("perimeter of a semi circle = "+perimeter);
}
}
[Link] print area of a circle when diameter is given.
import [Link].*;
public class p25
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter d");
double d=[Link]();
double r=d/2.0;
double area=3.14*r*r;
[Link]("Area of circle = "+area);
}
}

Common questions

Powered by AI

Without using the Scanner class, values for mathematical operations are hardcoded directly in the program. This approach limits flexibility as user input is not dynamically accepted. For example, calculating the sum of two numbers includes assigned constant integers directly in code . With the Scanner class, the program can prompt the user to input values at runtime, enhancing flexibility and usability, as seen in programs that calculate areas of geometric shapes .

For hardcoded values, Java calculates the average by summing the values and dividing by the count. In your program, this involves initial integer assignments followed by arithmetic operations such as 'average = total / count', explicitly defined in the code . Using Scanner input, user-provided values are read at runtime, dynamically computing the average with 'average = (num1 + num2 + num3 + ...)' aimed at user inputs .

Java programs implement formulas for shape properties by coding mathematical expressions directly into the program. For example, the volume of a cuboid is computed with 'volume = l * b * h' and the area of a triangle with 'area = (1/2) * b * h'. These calculations utilize basic arithmetic operations embedded within method bodies. Using these pre-defined formulas ensures accuracy and simplicity in the logic while reducing complexity by avoiding iterative or conditional logic .

In hardcoded Java programs, dimensions, such as side length for squares or length and breadth for rectangles, are assigned directly in the program. Consequently, the perimeter and area are calculated with fixed values, resulting in static outputs . When employing Scanner inputs, the user provides the dimensions during runtime, allowing dynamic calculations of perimeter and area—such as inputting side values to dynamically compute 'side * side' for area and '4 * side' for perimeter .

When using constants, the radius is predefined, and the area is calculated by substituting this value into the formula 'area = π * r * r', assuming π as 3.14 . With dynamic input using the Scanner class, the user is prompted to enter the radius value, which then applies to the same formula, providing more adaptable usage .

The Scanner class in Java facilitates user input at runtime, which allows programs to handle dynamic data entries. This is advantageous as it enables programs to be more flexible and interactive, accommodating a variety of inputs from users without needing to change the source code. For example, calculating areas or perimeters of shapes can adapt to different user-specified dimensions .

To compute the diagonal of a cuboid, Java uses the formula that involves the dimensions of the cuboid: d = √(l² + b² + h²). Java programs implement this by accepting input values for length, breadth, and height using the Scanner class. These values are then squared, summed, and the square root of the total is calculated using Math.sqrt, delivering the diagonal's length .

Java calculates the period (T) of a simple pendulum using the formula T = 2π√(l/g), where l is the length and g is the gravity constant. Using dynamic input facilities like the Scanner class, users input l and g, which are plugged into the formula within a program. By utilizing Math.sqrt for the square root calculation, Java evaluates T, outputting the pendulum's period based on varied physical values .

Java programs can calculate geometric areas without the Scanner class by directly assigning values to variables representing dimensions. For example, to calculate the area of a rectangle, you would assign values to the length and breadth, and then compute the area using the formula 'area = l * b' .

Java handles complex arithmetic operations by first obtaining user input using the Scanner class, then performing the operations using standard arithmetic operators. The expressions are placed within program methods where the input variables are combined in arithmetic calculations. For instance, calculating simple interest involves obtaining principal, rate, and time, then applying '(p * r * t) / 100', instancing such calculations as concise chunks of executable logic .

You might also like