0% found this document useful (0 votes)
20 views5 pages

Java Programs

Uploaded by

ssyblus690
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)
20 views5 pages

Java Programs

Uploaded by

ssyblus690
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

Java Programs

Q1. Write a program to calculate percentage of 5 subject by


taking input for each subject.

Ans 1.

import [Link];

public class Percentage {


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

[Link]("Enter Subject 1 Marks : ");


int sub1 = [Link]();
[Link]("Enter Subject 2 Marks : ");
int sub2 = [Link]();
[Link]("Enter Subject 3 Marks : ");
int sub3 = [Link]();
[Link]("Enter Subject 4 Marks : ");
int sub4 = [Link]();
[Link]("Enter Subject 5 Marks : ");
int sub5 = [Link]();

int totalGivenMarks = sub1+sub2+sub3+sub4+sub5;


float totalMarks = 500;

float percentage = (totalGivenMarks/totalMarks)*100;

[Link]("Percentage = "+percentage);
}
}
Output -

Q2. Write a program to check age for various age groups.

Ans 2.

import [Link];

public class Age {


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

[Link]("Enter you Age : ");


int age = [Link]();

if(age>0 && age<=2){


[Link]("Infant");
}
else if (age>2 && age<12) {
[Link]("Childhood");
}
else if (age>=12 && age<16) {
[Link]("Adolescent");
}
else if (age>=16 && age<18) {
[Link]("Teenage");
}
else if (age>=18) {
[Link]("Adult");
}
else{
[Link]("Wrong Input");
}
}
}

Output -

Q3. Write a program for taking different types of input from


the user.

Ans 3.

import [Link];

public class InputExample {


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

// Taking String input


[Link]("Enter a string: ");
String str = [Link]();

// Taking Integer input


[Link]("Enter an integer: ");
int num = [Link]();

// Taking Float input


[Link]("Enter a float: ");
float f = [Link]();

// Taking Double input


[Link]("Enter a double: ");
double d = [Link]();

// Taking Boolean input


[Link]("Enter a boolean (true/false): ");
boolean b = [Link]();

// Taking Character input


[Link]("Enter a character: ");
char ch = [Link]().charAt(0);

// Displaying all inputs


[Link]("\n--- You Entered ---");
[Link]("String: " + str);
[Link]("Integer: " + num);
[Link]("Float: " + f);
[Link]("Double: " + d);
[Link]("Boolean: " + b);
[Link]("Character: " + ch);
}
}
Output :

You might also like