Samiksh - Week 2 - Level 3 - 11 Practice Problems(Control Flow)
Samiksh - Week 2 - Level 3 - 11 Practice Problems(Control Flow)
Write a LeapYear program that takes a year as input and outputs the Year is a Leap Year or
not a Leap Year.
//importing modules
import java.util.*;
//importing modules
import java.util.*;
Q3. Write a program to input marks and 3 subjects physics, chemistry and maths. Compute the
percentage and then calculate the grade as per the following guidelines
//importing modules
import java.util.*;
import java.util.Scanner;
//initializing variables
int num;
//user input
System.out.print("Enter a number: ");
num = input.nextInt();
// Output result
if (isPrime) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
//closing scanner object
input.close();
}
}
Q5. Create a program to check if a number is armstrong or not. Use the hints to show the steps
clearly in the code
import java.util.Scanner;
//initializing variables
int num, numberOfDigits=0,sum=0,originalNum;
//user input
System.out.print("Enter a number: ");
num = input.nextInt();
originalNum = num;
// Output result
if (sum == num) {
System.out.println(num + " is an armstrong number.");
} else {
System.out.println(num + " is not an armstrong number.");
}
//closing scanner object
input.close();
}
}
import java.util.Scanner;
//initializing variables
int num, numberOfDigits=0, originalNum;
//user input
System.out.print("Enter a number: ");
num = input.nextInt();
originalNum = num;
import java.util.Scanner;
//intializing varibales
double weight,height,bmi = 1;
String status;
// Taking user input
System.out.print("Enter your weight in kg: ");
weight = scanner.nextDouble();
System.out.print("Enter your height in m: ");
height = scanner.nextDouble();
// Calculating BMI
bmi = weight / (height * height);
// Displaying output
System.out.printf("Your BMI is: %.2f\n", bmi);
System.out.println("Weight status: " + status);
//closing scanner object
scanner.close();
}
}
Q8. Create a program to check if a number taken from the user is a Harshad Number.
import java.util.Scanner;
//initializing variable
int num, sum = 0, temp;
// Taking user input
System.out.print("Enter a number: ");
num = scanner.nextInt();
temp = num;
import java.util.Scanner;
//initializing variables
int num,sum=0;
//user input
System.out.print("Enter a number: ");
num = input.nextInt();
import java.util.Scanner;
scanner.close();
}
}
Q11. Write a program DayOfWeek that takes a date as input and prints the day of the week
that the date falls on. Your program should take three command-line arguments: m (month),
d (day), and y (year). For m use 1 for January, 2 for February, and so forth. For output print
0 for Sunday, 1 for Monday, 2 for Tuesday, and so forth. Use the following formulas, for the
Gregorian calendar (where / denotes integer division):
y0 = y − (14 − m) / 12
x = y0 + y0/4 − y0/100 + y0/400
m0 = m + 12 × ((14 − m) / 12) − 2
d0 = (d + x + 31m0 / 12) mod 7
import java.util.*;
//calculating
int y0 = y - (14 - m) / 12;
int x = y0 + y0/4 - y0/100 + y0/ 400;
int m0 = m + 12 * ((14 - m) / 12) - 2;
int d0 = (d + x + (31 * m0) / 12) % 7;
//printing output
System.out.println(d0);
}
}