FA24-BCS-045 PF_2
FA24-BCS-045 PF_2
SECTION: BCS-2-A
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter student marks: ");
int marks = scanner.nextInt();
int grade;
if( marks>=85 && marks<100){
System.out.println("GRADE IS A");
} else if (marks>=70 && marks<=84) {
System.out.println("GRADE IS B");
} else if (marks>=55 && marks<=69) {
System.out.println("GRADE IS C");
}
else if (marks>=40 && marks<=54) {
System.out.println("GRADE IS D");
}
else if (marks>=0 && marks<=40) {
System.out.println("YOU ARE FAIL");
}
else { System.out.println("ENTER VALID MARKS") }
}
Write a Java program that checks whether a given integer is even or odd using an IF-ELSE statement.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int a= 44;
if (a%2==0){
System.out.println(a+" is an even number");
}
else {
System.out.println(a+" is an odd number");
}
}
}
Create a Java program that takes three numbers as input and determines the largest number using
nested IF-ELSE statements
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("enter number 1: ");
int num1=scanner.nextInt();
System.out.println("enter number 2: ");
int num2=scanner.nextInt();
System.out.println("enter number 3: ");
int num3=scanner.nextInt();
if(num1>num2)
if(num1>num3){
System.out.println(num1+" is the largest number ");
}
else {
System.out.println(num3+" is the largest number");
}
else
if(num2>num3){
System.out.println(num2+" is the largest number ");
}
else {
System.out.println(num3+" is the largest number");
}}
Write a Java program that takes an integer N (1-10) as input and uses a FOR loop to print the
multiplication table of N (from 1 to 10).
1. import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("ENTER AN INTEGER: ");
int n= scanner.nextInt();
System.out.println("Multiplication table of "+n );
for (int i=1;i<=10;i++){
System.out.println(n+" x "+i +" = " +(n * i));
}
}
}
Develop a Java program that takes an integer X and prints all numbers from 1 to X using a WHILE loop.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("Enter an integer: ");
int X= scanner.nextInt();
int i=1;
while (i<=X){
System.out.println(i);
i++;
}
}
}
Write a Java program that calculates the sum of all even numbers from 1 to 100 using a FOR loop.
int sum=0;
for(int i=2;i<=100;i+=2){
sum +=i;
System.out.println("Sum of even numbers between 1 to 100 is "+sum);
}
}
}
Construct a program that displays the following shape using nested for loops.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int rows = 4;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= (2 * i - 1); k++) {
System.out.print("*");
}
System.out.println();
}
}
}
Construct a program that displays the following shape using nested for loops.
2. public class Main {
public static void main(String[] args) {
int n = 5;