0% found this document useful (0 votes)
26 views3 pages

Java Programs (Class 8)

The document contains multiple Java programs demonstrating basic programming concepts such as loops, simple interest calculation, age verification for voting, and arithmetic operations. It includes examples using for, while, and do-while loops, as well as user input handling with the Scanner class. Each program is structured with a main method and showcases different functionalities in Java.

Uploaded by

ughhsanvi
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)
26 views3 pages

Java Programs (Class 8)

The document contains multiple Java programs demonstrating basic programming concepts such as loops, simple interest calculation, age verification for voting, and arithmetic operations. It includes examples using for, while, and do-while loops, as well as user input handling with the Scanner class. Each program is structured with a main method and showcases different functionalities in Java.

Uploaded by

ughhsanvi
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

Java Programs.

1. Write a java program to print numbers from 1-10 using for, while, do while loop.

public class For {


public static void main(String[] args) {
for(int i=1;i<=10;i++){
System.out.println(i);
}
}
}

2. public class While {


public static void main(String[] args) {
int i=1;
while(i<=10){
System.out.println(i);
i++;
}
}
}

3. public class DoWhile {


public static void main(String[] args) {
int i=1;
do{
System.out.println(i);
i++;
}while(i<=10);
}
}

4. Simple Interest Program in Java.


import java.util.Scanner;
class Simple_Interest{
public static void main (String args[] )
{
int p;
float rot;
int t;
float si;
float amt;
Scanner input=new Scanner(System.in);
System.out.println(" Enter the Principal Amount: ");
p=input.nextInt();
System.out.println("Enter the Rate of Interest:");
rot=input.nextFloat();
System.out.println("Enter the Number of years:");
t=input.nextInt();
si=(p *rot*t)/100;
amt=si+p;
System.out.println("\n Simple Interest is: "+si);
System.out.println("\n Total Amount is: "+amt);
}
}
5. Java program to find out voter age.
import java.util.Scanner;
class Conditional
{
public static void main(String[]args)
{
{
System.out.println("Enter your age please.");
}
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
if(age>=18)
{
System.out.println("Welcome to the voter portal !" );
}
else
{
System.out.println("Sorry, you cannot proceed further. Thank you.");
}
}
}
6. Sum Of Natural Number in Java using For loop.

public class SumOfNaturalNumber1


{
public static void main(String[] args)
{
int i, num = 10, sum = 0;
//executes until the condition returns true
for(i = 1; i <= num; ++i)
{
//adding the value of i into sum variable
sum = sum + i;
}
//prints the sum
System.out.println("Sum of First 10 Natural Numbers is = " + sum);
}
}

7. Java Program to Find Sum of Natural Numbers.


public class SumOfNaturalNumber1
{
public static void main(String[] args)
{
int i, num = 10, sum = 0;
//executes until the condition returns true
for(i = 1; i <= num; ++i)
{
//adding the value of i into sum variable
sum = sum + i;
}
//prints the sum
System.out.println("Sum of First 10 Natural Numbers is = " + sum);
}
}

}
8. Sum of two numbers using Scanner
import java.util.Scanner;
public class AddTwoNumbers2 {

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();

sc.close();
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}

9. Java program for area of rectangle


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);
}
}

You might also like