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 [Link];
public class Positive_Negative
{
public static void main(String[] args)
{
int num;
//object of the Scanner class
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
//reading a number from the user
num = [Link]();
//checks the number is greater than 0 or not
if(num>0)
{
[Link]("The number is positive.");
}
//checks the number is less than 0 or not
else if(num<0)
{
[Link]("The number is negative.");
}
//executes when the above two conditions return false
else
{
[Link]("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 [Link];
public class Leap_Year {
public static void main(String[] args){
int year;
[Link]("Enter an Year :: ");
Scanner sc = new Scanner([Link]);
year = [Link]();
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
[Link]("Specified year is a leap year");
else
[Link]("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 [Link];
public class Grade {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter percentage marks");
double percentage = [Link]();
if(percentage >= 90){
[Link]("Excellent: Grade A");
}else if(percentage < 90 && percentage >= 80){
[Link]("Very Good: Grade B");
}else if(percentage < 80 && percentage >= 70){
[Link]("Good: Grade C");
}else if(percentage < 70 && percentage >= 60){
[Link]("Satisfactory: Grade D");
}else if(percentage < 60 && percentage >= 50){
[Link]("Work Hard: Grade E");
}else if(percentage < 50 && percentage >= 40){
[Link]("Just Passed: Grade F");
}else {
[Link]("Failed!");
}
}
}
4. calculate Area of Rectangle.
import [Link];
class AreaOfRectangle {
public static void main (String[] args)
{
Scanner scanner = new Scanner([Link]);
[Link]("Enter the length of Rectangle:");
double length = [Link]();
[Link]("Enter the width of Rectangle:");
double width = [Link]();
//Area = length*width;
double area = length*width;
[Link]("Area of Rectangle is:"+area);
5. Write a program to find voting age and show the output.
import [Link];
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([Link]);
[Link](" Please enter your age");
age = [Link]();
// Checking condition for voting..
if(age>=18)
{
[Link]("Welcome to voting system Yo can Vote");
}
else
{
shrt= (18 - age);
[Link]("Sorry,You can vote after :"+ shrt + " years");
}
6. Java Program to Print 1 to 100 without Loop.
import [Link].*;//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.
[Link](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) {
[Link](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++) {
[Link](n);
}
}
}
9. Java Program to find the sum of 5 numbers entered by the user is printed.
import [Link];
class SumOfNumbers {
public static void main(String[] args) {
int sum = 0;
for (int n = 1; n <= 5; n++) {
[Link]("Enter a number");
Scanner s = new Scanner([Link]);
int num = [Link]();
sum += num; // same as sum = sum + num
}
[Link]("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 {
[Link](n);
n++;
} while (n <= 10);
}
}