Delhi Public School – Ruby Park, Kolkata
CLASS IX
Computer Science(2022-23)
Study Material (JAVA Programs)
1. Check the number is Positive Or Negative in Java using scanner class.
import java.util.Scanner;
public class Positive_Negative
{
public static void main(String[] args)
{
int num;
//object of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
//reading a number from the user
num = sc.nextInt();
//checks the number is greater than 0 or not
if(num>0)
{
System.out.println("The number is positive.");
}
//checks the number is less than 0 or not
else if(num<0)
{
System.out.println("The number is negative.");
}
//executes when the above two conditions return false
else
{
System.out.println("The number is zero.");
}
}
}
2. Java program to check a year is leap, if it is divisible by 4 and 400. But, not by 100.
import java.util.Scanner;
public class Leap_Year {
public static void main(String[] args){
int year;
System.out.println("Enter an Year :: ");
Scanner sc = new Scanner(System.in);
year = sc.nextInt();
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
System.out.println("Specified year is a leap year");
else
System.out.println("Specified year is not a leap year");
}
}
3. Java
program of grading system for fail, D grade, C grade, B grade, A grade and A+
.
import java.util.Scanner;
public class Grade {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter percentage marks");
double percentage = scan.nextDouble();
if(percentage >= 90){
System.out.println("Excellent: Grade A");
}else if(percentage < 90 && percentage >= 80){
System.out.println("Very Good: Grade B");
}else if(percentage < 80 && percentage >= 70){
System.out.println("Good: Grade C");
}else if(percentage < 70 && percentage >= 60){
System.out.println("Satisfactory: Grade D");
}else if(percentage < 60 && percentage >= 50){
System.out.println("Work Hard: Grade E");
}else if(percentage < 50 && percentage >= 40){
System.out.println("Just Passed: Grade F");
}else {
System.out.println("Failed!");
}
}
}
4. calculate 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);
5. Write a program to find voting age and show the output.
import java.util.Scanner;
public class Voting {
public static void main(String[] args)
{
// Declaring variables
int age,shrt;
// taking values from user at run time
Scanner scan = new Scanner(System.in);
System.out.println(" Please enter your age");
age = scan.nextInt();
// Checking condition for voting..
if(age>=18)
{
System.out.println("Welcome to voting system Yo can Vote");
}
else
{
shrt= (18 - age);
System.out.println("Sorry,You can vote after :"+ shrt + " years");
}
6. Java Program to Print 1 to 100 without Loop.
import java.util.*;//Importing the packages of java
public class Print1to100 {
//Body of the main function
public static void main(String[] args) {
int number = 1;
printNumbers(number);
}
//Body of the user defined function
public static void printNumbers(int num)
{
//Checkinf the condition for the funtion processing
if(num <= 100)
{
//Printing the output number to the user by the program.
System.out.print(num +" ");
//Increasing the value of number by 1
printNumbers(num + 1);
}
}
}
7. Java program prints the first 10 natural numbers using a While loop.
class While {
public static void main(String[] args) {
int n = 1;
while (n <= 10) {
System.out.println(n);
n++;
}
}
}
8. Java program prints the first 10 natural numbers using a For loop.
class For {
public static void main(String[] args) {
int n;
for (n = 1; n <= 10; n++) {
System.out.println(n);
}
}
}
9. Java Program to find the sum of 5 numbers entered by the user is printed.
import java.util.Scanner;
class SumOfNumbers {
public static void main(String[] args) {
int sum = 0;
for (int n = 1; n <= 5; n++) {
System.out.println("Enter a number");
Scanner s = new Scanner(System.in);
int num = s.nextInt();
sum += num; // same as sum = sum + num
}
System.out.println("Sum is " + sum);
}
}
10. The first 10 natural numbers are printed using a Do While Loop in Java.
class DoWhile {
public static void main(String[] args) {
int n = 1;
do {
System.out.println(n);
n++;
} while (n <= 10);
}
}