0% found this document useful (0 votes)
13 views7 pages

Class 8 Java Codes

The document contains a series of Java programs that demonstrate various programming concepts such as calculating the product of numbers, finding the maximum of three values, displaying multiples, checking age for adulthood, calculating average marks, displaying numbers backward, generating series, summing cubes of odd numbers, calculating computer bills based on quantity, grading based on marks, and computing discounts on purchases. Each program includes code snippets and sample outputs. The examples serve as practical exercises for learning Java programming.

Uploaded by

mrinmoyghosh517
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)
13 views7 pages

Class 8 Java Codes

The document contains a series of Java programs that demonstrate various programming concepts such as calculating the product of numbers, finding the maximum of three values, displaying multiples, checking age for adulthood, calculating average marks, displaying numbers backward, generating series, summing cubes of odd numbers, calculating computer bills based on quantity, grading based on marks, and computing discounts on purchases. Each program includes code snippets and sample outputs. The examples serve as practical exercises for learning Java programming.

Uploaded by

mrinmoyghosh517
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

1.

write a program in java to find the product of the number

Code

public class Main {


public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5}; // You can change these numbers
int product = 1;

for (int number : numbers) {


product *= number;
}

[Link]("The product of the numbers is: " + product);


}
}

Output
The product of the numbers is: 120

2. Write a program in java to ask the value of 3 variables and it will show the maximum value
among the numbers.

Code

import [Link];

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter the first number:");


int num1 = [Link]();

[Link]("Enter the second number:");


int num2 = [Link]();

[Link]("Enter the third number:");


int num3 = [Link]();

int max = num1;


if (num2 > max) {
max = num2;
}
if (num3 > max) {
max = num3;
}

[Link]("The maximum value is: " + max);


}
}
Output
Enter the first number:
5
Enter the second number:
6
Enter the third number:
7
The maximum value is: 7

3. Write a program in Java to display the first 10 multiple of 11.

Code
public class Main {
public static void main(String[] args) {
int num = 11;

for (int i = 1; i <= 10; i++) {


[Link](num + " x " + i + " = " + num * i);
}
}
}
Output
11 x 1 = 11
11 x 2 = 22
11 x 3 = 33
11 x 4 = 44
11 x 5 = 55
11 x 6 = 66
11 x 7 = 77
11 x 8 = 88
11 x 9 = 99
11 x 10 = 110
4. Write a program in java that will ask the user is his age and if the age is above 18 or 18 the
programme will show you are an adult as a message on screen.
Code
import [Link];

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter your age:");
int age = [Link]();

if (age >= 18) {


[Link]("You are an adult.");
} else {
[Link]("You are not an adult.");
}
}
}
Output
Enter your age:20
You are an adult.

Or
Enter your age:
15
You are not an adult.
5. Write a program in java to calculate the average marks of the student among the English,
Maths, Science and computer after taking the marks on the user and displaying the average
marks.
Code
import [Link];

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter your marks in English:");


int englishMarks = [Link]();

[Link]("Enter your marks in Maths:");


int mathsMarks = [Link]();

[Link]("Enter your marks in Science:");


int scienceMarks = [Link]();

[Link]("Enter your marks in Computer:");


int computerMarks = [Link]();

double averageMarks = (englishMarks + mathsMarks + scienceMarks + computerMarks)


/ 4.0;

[Link]("Your average marks are: " + averageMarks);


}
}
Output
Enter your marks in English:
78
Enter your marks in Maths:65
Enter your marks in Science:
85
Enter your marks in Computer:
54
Your average marks are: 70.5
6. Write a program in java to display the number backward like 100, 81, 64..........1,0.
Code
public class Main {
public static void main(String[] args) {
for (int i = 10; i >= 0; i--) {
[Link](i * i);
}
}
}
Output
10081
64
49
36
25
16
9
4
1
0
7. Write a program to generate the following series 3,12,48,192,768..... up to 10 forms.
Code
public class Main {
public static void main(String[] args) {
int num = 3;
for (int i = 1; i <= 10; i++) {
[Link](num);
num *= 4;
}
}
}
Output
312
48
192
768
3072
12288
49152
196608
786432

8. Write a program to find the sum of the cube of first 10 odd numbers.
Code
public class Main {
public static void main(String[] args) {
int sum = 0;

for (int i = 1; i <= 20; i += 2) {


sum += [Link](i, 3);
}

[Link]("The sum of the cubes of the first 10 odd numbers is: " + sum);
}
}
Output
The sum of the cubes of the first 10 odd numbers is: 19900

9. A computer dealer sell computers at the following rates


rs 28000 per computer for less than20 computers,
rs25000for computer for 20 to39 computers
rs 20000per computer for 40 or more computers.
write a program to input the numbers of computer and calculate and print the bill?
Code
import [Link];

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter the number of computers:");


int numComputers = [Link]();

int pricePerComputer;
if (numComputers < 20) {
pricePerComputer = 28000;
} else if (numComputers < 40) {
pricePerComputer = 25000;
} else {
pricePerComputer = 20000;
}

int totalBill = numComputers * pricePerComputer;


[Link]("The total bill is: Rs " + totalBill);
}
}
Output
Enter the number of computers:
30
The total bill is: Rs 750000

10. wap to enter the marks of the student in science. Print the grade based on marks for 90and
above -A,
75 to 89-B
60to74-C
40 to 59 D
Below 40 –F
Code
import [Link];

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter the marks in Science:");


int marks = [Link]();

if (marks >= 90) {


[Link]("Grade: A");
} else if (marks >= 75) {
[Link]("Grade: B");
} else if (marks >= 60) {
[Link]("Grade: C");
} else if (marks >= 40) {
[Link]("Grade: D");
} else {
[Link]("Grade: F");
}
}
}
Output
Enter the marks in Science:
50
Grade: D

11. in puja shopping center has anounced festival discount on the purchase amount on the
following uptp
rs 2500 -5%,
rs2501 to 5000-10%,
5001 to 10000-15%,
above 10,000 - 20%.
wap to input the total amount of putchase and compute and display the discount and the
amount to paid after dicount.
Code
import [Link];

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter the total purchase amount:");


double purchaseAmount = [Link]();

double discount;
if (purchaseAmount <= 2500) {
discount = purchaseAmount * 0.05;
} else if (purchaseAmount <= 5000) {
discount = purchaseAmount * 0.10;
} else if (purchaseAmount <= 10000) {
discount = purchaseAmount * 0.15;
} else {
discount = purchaseAmount * 0.20;
}

double amountToPay = purchaseAmount - discount;

[Link]("The discount is: Rs " + discount);


[Link]("The amount to be paid after discount is: Rs " + amountToPay);
}
}
Output
Enter the total purchase amount:46582
The discount is: Rs 9316.4
The amount to be paid after discount is: Rs 37265.6

You might also like