Computer Collated Krishna 9A

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 95

Computer Assignment 1

By Krishna Thulasi
Class: 9A
Date:23-8/2020
Subject: Computer Application.
Question 1.
A. Program
public class Ques1 // Write a program to calculate area of a rectangle.
Input length and breadth of the rectangle from the user.
{
public static void main (String args [])
{
double length, breadth, area;
length=10;
breadth=5;
area=length*breadth;
System.out.println("The area is"+area);
}
}
B. Output : The area is50.0

C.Variable Description Table


Variable Name Data Type Description

Length Double To store the length of


the rectangle.

Breadth Double To store the breadth


of the rectangle.

Area Double To store the area of


the rectangle.

Question 2.
A. Program
public class Ques2 // Write a program to calculate area of a circle.
Input radius of the circle from the user.
{
public static void main (String arg[])
{
double pi,radius,area;
pi=3.14;
radius=5;
area=pi*radius*radius;
System.out.println("The radius of the circle is"+area);
}
}

B. Output : The radius of the circle is78.5


C. Variable Description Table

Variable Name Data Type Description

radius double To store the radius of


the circle.

pi double This is a constant


value pi, used to
calculate the area.

area double To store the value of


area of the circle.
Question 3.
A. Program
public class Ques3 // Write a program to calculate circumference
of the circle. Input radius of the circle from the user.
{
public static void main(String args [])
{
double pi,radius,circumference;
pi=3.14;
radius=8;
circumference=2*pi*radius;
System.out.println("The circumference is"+circumference);
}
}
B. Output : The circumference is50.24
C. Variable Description Table

Variable Name Data Type Description

pi double Used to store the


value of pi.

radius double To store the radius


of the circle.

Circumference double To store the


circumference of the
circle.
Question 4.
A. Program
public class Ques4 // Write program to calculate area of a
triangle. Input height and base of triangle from the user.
{
public static void main(String ards[])
{
double height,base,area;
height=5;
base=10;
area=0.5*height*base;
System.out.println("The area of the triangle is"+area);
}
}
B. Output: The area of the triangle is25.0
C. Variable Description Table

Variable Name Data Type Description

height double To store height of


the triangle.

base double To store base of the


triangle.

area double Used to store area


of the triangle.
Question 5.
A. Program
public class Ques5 // Write a program to calculate area of a
square. Input side of the square from the user.
{
public static void main(String args[])
{
double side,area;
side=10;
area=side*side;
System.out.println("The area of the square is"+area);
}
}
B. Output: The area of the square is100.0
C. Variable Description Table

Variable Name Data Type Description

side double Used to store the


side of a square.

area double Used to store the


area of a square.
Computer Assignment 2
Date:19/08/2020
Question 1.
A. Program

import java.util.Scanner;
public class Volume // Write a program to calculate volume of a sphere. Input
radius of the circle from the user.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
final float pi=2.14f;
float rad;
System.out.println("Enter the value:");
rad=sc.nextFloat();
double vol= 4.0/3.0*pi*rad*rad*rad;
System.out.println("The volume of the sphere is"+vol);
}
}

B. Output: Enter the value:

2
The volume of the sphere is22.82666778564453
C. Variable Description Table
Variable Name Data Type Description

pie float Used to calculate the


volume of a sphere.

rad float Used to store the radius


of the circle.

volume double To store the volume of a


sphere.

Question 2
A. Program

import java.util.Scanner;
public class Vol_cone // Write a program to calculate volume of a cone. Input
radius and height of the cone from the user.
{
public static void main (String args[])
{
Scanner sc=new Scanner(System.in);
final float pi=3.14f;
float rad;
System.out .println("Enter the value");
rad=sc.nextFloat();
float height;
System.out.println("Enter the value");
height=sc.nextFloat();
double vol=1.0/3.0*pi*rad*rad*height;
System.out.println("The volume of the cone is"+vol);
}
}

B. Output: Enter the value

3
Enter the value
6
The volume of the cone is56.52000188827515

C. Variable Description Table

Variable Name Data type Description

pi float Used to calculate the


volume of a cone.

rad float To store the radius of


a cone.

volume double To store the volume of


a cone.

height Float To store the height of


a cone.

Question 3
A. Program

import java.util.Scanner;
public class Vol_cylinder // Write a program to calculate volume of a cylinder
Input radius and height of the cylinder from the user.
{
public static void main (String args[])
{
Scanner sc=new Scanner (System.in);
final float pi=3.14f;
float rad;
System.out.println("Enter the value");
rad=sc.nextFloat();
float height;
System.out.println("Enter the value");
height=sc.nextFloat();
double vol= pi*rad*rad*height;
System.out.println("The volume of the cylinder is"+vol);
}
}
B. Output:

Enter the value


2
Enter the value
3
The volume of the cylinder is37.68000030517578
C. Variable Description Table
Variable Name Data Type Description

pi float Used to calculate the volume of


cylinder.

rad float To store the radius of the


cylinder.

height float To store height of the cylinder.

volume double To store volume of the cylinder.

Question 4
A. Program

import java.util.Scanner;
public class Vol_cube // Write a program to calculate volume of a cube. Input
side of the cube from the user.
{
public static void main (String args[])
{
Scanner sc=new Scanner(System.in);
float side;
System.out.println("Enter the value:");
side=sc.nextFloat();
double vol=side*side*side;
System.out.println("The volume of the cube is"+vol);
}
}
B. Output:

Enter the value:


6
The volume of the cube is216.0
C. Variable Description Table

Variable Name Data Type Description

side float Used to store the side


of the cube.

vol Double Used to store the


volume of a cube.
Question 5
A. Program

import java.util.Scanner;
public class Vol_cuboid // Write a program to calculate volume of a cuboid. Input
length,breadth and height from the user.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
float length;
System.out.println("Enter the value:");
length=sc.nextFloat();
float breadth;
System.out.println("Enter the value:");
breadth =sc.nextFloat();
float height;
System.out.println("Enter the value:");
height =sc.nextFloat();
double vol =length*breadth*height;
System.out.println("The volume of a cuboid is"+vol);
}
}
B. Output:

Enter the value:


8
Enter the value:
2
Enter the value:
6
The volume of a cuboid is96.0
C. Variable Description Table

Variable Name Data Type Description

length float Used to store length


of a cuboid.

breadth float Used to store


breadth of a cuboid.

height float Used to store height


of a cuboid.

volume double Used to store


volume of a cuboid.

D.
Computer Assignment 3
Date:19/08/2020
Question 1
A. Program
public class Saving // Write a program to compute the total amount
coleected by Rohit after saving 455rs every month for 7yrs.
{
public static void main (String arg[])
{
double saving,year,month,total_amount;
saving=455;
year=7;
month=12;
total_amount=saving*year*month;
System.out.println("Total amount collected by rohit is"+total_amount);
}
}
B. Output: Total amount collected by rohit is38220.0
C. Variable Description Table

Variable Name Data type Description

saving Double To store the saving.

years double Used to store the number of


years.

month double Used to store the number of


month.

Total amount double Used to store the amount.

Question 2
A. Program
public class Distance // Write a program to determine the total distance
covered by an athlete who takes 16 rounds of a rectangle field with
sides 25 and 12.
{
public static void main(String arg[])
{
int total_distance,length,breadth,rounds,total_length_of_field;
length=25;
breadth=12;
rounds=16;
total_length_of_field=2*(length+breadth);
total_distance=rounds*total_length_of_field;
System.out.println("Total distance is"+total_distance);
}
}
B. Output: Total distance is1184
C. Variable Description Table

Variable name Data Type Description

length int Used to store the


length.

breadth int Uesd to store the


breadth.

No_of_rounds int Used to store the


rounds.

Total_length_of_field int Used to store the


length of the field.

Total_distance int Used to store the


distance of the field.
Question 3
A. Program
public class Expenditure // John and Sara spend 6250rs and 5225rs
every month on their day to day needs. Write a program to compute
thier total expenditure for last 6 yrs.
{
public static void main(String args[])
{
double
expenditure_on_john,expenditure_on_sara,total_expenditure,month,ye
ar;
expenditure_on_john=6250;
expenditure_on_sara=5225;
month=12;
year=6;

total_expenditure=expenditure_on_john+expenditure_on_sara*month*
year;
System.out.println("Expenditure is"+total_expenditure);
}
}
B. Output: Expenditure is382450.0
C. Variable Description Table

Variable Name Data Type Description

Expenditure_on_john double To store the


expenditure on john.

Expenditure_on_sara double To store the


expenditure on sara.

month double To store the value of


month.
year double To store the value of
year.

Total_expenditure double To store the value of


total expenditure.

Question 4
A. Program
import java.util.Scanner;
public class Bill_Amount // Arestaurant charges 5% of G.S.T and 2%
of service tax to its customers. Write a program to calculate the tax
amount on a dining bill.Accept the bill_amount from the keyboard.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Bill Amount:");
double Bill_Amount=sc.nextDouble();
double gst=0.05*Bill_Amount;
double stax=0.02*Bill_Amount;
double total_tax=gst+stax;
System.out.println("Total tax amount on"+total_tax);
}
}

B. Output : Enter the Bill Amount:


456
Total tax amount on31.92
C. Variable Description Table

Variable Name Data Type Description

gst double Used to store gst.


tax double Used to store the
value of tax.

Total_tax double Used to store the


value of tax.

Bill_amount double Used to store the


value of bill.

Question 5
A. Program
public class Simple_interest // Write a program to compute
simple interest for the Principal Amount of 10780/- rs with the
rate as 8.1% for 9yrs.
{
public static void main(String args[])
{
double p=10780;
double r=8.1;
double t=9;
double si=0;
double n=100.0;
si=(p*r*t)/n;
System.out.println("Simple Interest is:"+si);

}
}
B. Output : Simple Interest is:7858.62
C. Variable description table

Variable name Data type Description

p double Used to store the


principle.

r double Used to store the


rate.

t double Used to store the


time.

Si double Used to store the


simple interest
Computer Assignment 4
Date:19/08/2020
Question 1
A. Program

import java.util.Scanner ;
public class Swap // write a program to swap two values. Accept the value from
the user.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a,b,temp;
System.out.println("Enter the value of integer a:");
a=sc.nextInt();
System.out.println("Enter the value of integer b:");
b=sc.nextInt();
System.out.println("Before Swapping/na :"+a+"/nb="+b);
temp=a;
a=b;
b=temp;
System.out.println("After swapping/na:"+a+"/nb="+b);
}
}
B. Output: Enter the value of integer a:
4
Enter the value of integer b:
5
Before Swapping/na :4/nb=5
After swapping/na:5/nb=4
C. Variable description table

Variable name Data type Description

a int Used to store the value


of a.

b int Used to store the value


of b.

temp int Used to store the value


of temp.

Question 2
A. Program
import java.util.Scanner;
public class profit // write a program to compute the profit percentage
after reading cost_price and selling_price.
{
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
double cost_price,selling_price;
System.out.println("Enter the value of cost_price:");
cost_price=sc.nextDouble();
System.out.println("Enter the value of selling_price:");
selling_price=sc.nextDouble();
double profit_percent= selling_price-cost_price;
System.out.println("Profit is"+profit_percent);
}
}
B. Output : Enter the value of cost_price:
5225
Enter the value of selling_price:
4678
Profit is-547.0
C. Variable description table

Variable name Data type Description

Profit_percent double Used to store the


profit

Selling_percent double Used to store the


selling price

Cost_price double Used to store the value


of cost price.

Question 3
A. Program
import java.util.Scanner;
public class speed // write a program to compute speed of a vehicle.
Accept distance and time.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double distance,time;
System.out.println("Enter the value of distance:");
distance=sc.nextDouble();
System.out.println("Enter the value of time:");
time=sc.nextDouble();
double speed=distance/time;
System.out.println("Speed is"+speed);
}
}
B. Output: Enter the value of distance:
1500
Enter the value of time:
2
Speed is750.0
C. Variable description table

Variable name Data type Description

distance double Used to store the value


of distance.

time double Used to store the value


of time.

Speed double Used to store the value


of speed.

Question 4
A. Program
public class expression // write program to evaluate the following
expression:m-(n++)*(--m)*n
{
public static void main(String args[])
{
int m=2,n=5,solution;
solution=m-(n++)*(--m)*n;
System.out.println("Solution is"+solution);
}
}
B. Output : Solution is-28
C. Variable description table

Variable name Data type Description

m int Used to store the value


of an integer.

n int To store the value of an


integer.

Solution int To store the solution.

Question 5
A. Program
public class baskets // write a program to compute total no. of baskets
maya can fill with 1037 apples; each should contain 8 apples. Also compute
the no. of remaining apples which she couldn't fill int the basket.
{
public static void main(String args [])
{
int apples=1037;
int baskets=apples/8;
System.out.println("Total number of baskets are:"+baskets);
System.out.println("The apples remaining are 5");
}
}
B. Output : Total number of baskets are:129
The apples remaining are 5
C. Variable description table

Variable name Data type Description

apples int To store the total


numbers of apples.

baskets int To store the total


numbers of baskets.

Computer Assignment 5
Implicit and Explicit type conversions
Date:30/09/2020
Question 1
A. Program
import java.util.Scanner;
public class int_double // Write a java program to illustrate conversion of
int and double to byte.Accept the number form the user.
{
{
public static void main (String args[])
{
Scanner sc=new Scanner(System.in);
int a;double c;
System.out.println("Value of a is");
a = sc.nextInt();
System.out.println("Value of c is");
c = sc.nextDouble();
byte ab = (byte)a;
byte cb = (byte)c;
System.out.println("Int value "+a);
System.out.println("Double value "+c);
System.out.println("After conversion of int value to byte is "+ab);
System.out.println("After conversion of double value to byte is "+cb);
}
}
B. Output :
Value of a is
6
Value of c is
9
Int value 6
Double value 9.0
After conversion of int value to byte is 6
After conversion of double value to byte is 9

C. Variable description table

Variable name Data type Description

A int Used to store the value


of a.

C double Used to store the value


of c.

ab byte Used to store the value


of ab.

cb byte Used to store the value


of cb.

Question 2
A. Program
import java.util.Scanner;
public class int_byte ; // Java program to illustrate type casting int to
byte.Accept the numbers from the user.

{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a;
System.out.println("The value of a is = ");
a = sc.nextInt();
byte ab = (byte)a;
System.out.println("Int value of a is "+a);
System.out.println("After type casting int to byte the value is "+ab);
}
}

B. Output :
The value of a is =
5
Int value of a is 5
After type casting int to byte the value is 5

C. Variable description table

Variable name Data type Description

a int Used to store the


value of a.

ab byte Used to store the


value of ab.

Question 3
A. Program
import java.util.Scanner;
public class float_int // Java program to illustrate explicit type
conversion of float to int.Accept the numbers from the user.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
float a;
System.out.println("Value of a is ");
a = sc.nextFloat();
int ai = (int)a;
System.out.println("Float value of a is "+a);
System.out.println("After the conversion of float to int the value of
a is "+ai);
}
}

B. Output :
Value of a is
8
Float value of a is 8.0
After the conversion of float to int the value of a is 8

C. Variable description table

Variable name Data type Description

a float Used to store the


value of a.

ai int Used to store the


value of ai.

Question 4
A. Program
import java.util.Scanner;
public class int_char // Java program to illustrate explicit type
conversion of int to char.Accept the numbers from the user.
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
int a;
System.out.println("Value of a is ");
a = sc.nextInt();
char ac = (char)a;
System.out.println("Int value of a is "+a);
System.out.println("After conversion of int to char the value of a is
"+ac);
}
}

B. Output :
Value of a is
78
Int value of a is 78
After conversion of int to char the value of a is N
C. Variable description table

Variable name Data type Description

a int Used to store the


value of a.
ac char Used to store the
value of ac.

Question 5
A. Program
import java.util.Scanner;
public class int_char // Java program to illustrate explicit type conversion
of int to char.Accept the numbers from the user.
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
int a;
System.out.println("Value of a is ");
a = sc.nextInt();
char ac = (char)a;
System.out.println("Int value of a is "+a);
System.out.println("After conversion of int to char the value of a is
"+ac);
}
}
B. Output: Value of a is
5
Int value of a is 5
After conversion of int to char the value of a is
C. Variable description table

Variable name Data type Description

A int Used to store the value


of a.
Computer Assignment 6
Math class methods
Date:30/09/2020
Question 1
A. Program
import java.util.Scanner;
public class maximum_minimum // Write java program to illustrate use of
math class functions max() and min(), Accept the value from the
user.Display the max and min values with appropriate message.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int num1,num2;
System.out.println("num1 is=");
num1=sc.nextInt();
System.out.println("num2 is=");
num2=sc.nextInt();
System.out.println("Max
between"+num1+"and"+num2+"is"+Math.max(num1,num2));
System.out.println("Min
between"+num1+"and"+num2+"is"+Math.min(num1,num2));
}
}

B. Output :
num1 is=
56
num2 is=
78
Max between56and78is78
Min between56and78is56

C. Variable description table

Variable name Data type Description

Num1 int Used to store the value


of num1.

Num2 int Used to store the value


of num2.

Question 2
A. Program
public class sqrt_cbrt
{
public static void main(String args[])
{
int num1 = 4;
int num2 = 27;
int num3 = 25;
int num4 = 8;
int num5 = 9;
System.out.println("Square root of num1 is "+Math.sqrt(num1));
System.out.println("Cube root of num1 is "+Math.cbrt(num1));
System.out.println("Square root of num2 is "+Math.sqrt(num2));
System.out.println("Cube root of num2 is "+Math.cbrt(num2));
System.out.println("Square root of num3 is "+Math.sqrt(num3));
System.out.println("Cube root of num3 is "+Math.cbrt(num3));
System.out.println("Square root of num4 is "+Math.sqrt(num4));
System.out.println("Cube root of num4 is "+Math.cbrt(num4));
System.out.println("Square root of num5 is "+Math.sqrt(num5));
System.out.println("Cube root of num5 is "+Math.cbrt(num5));
}
}

B. Output :
Square root of num1 is 2.0
Cube root of num1 is 1.5874010519681996
Square root of num2 is 5.196152422706632
Cube root of num2 is 3.0
Square root of num3 is 5.0
Cube root of num3 is 2.924017738212866
Square root of num4 is 2.8284271247461903
Cube root of num4 is 2.0
Square root of num5 is 3.0
Cube root of num5 is 2.080083823051904

C. Variable description table

Variable name Data type Description

Num1 int Used to store the


value of num1.

Num2 int Used to store the


value of num2.

Num3 int Used to store the


value of num3.

Num4 int Used to store the


value of num4.

Num5 int Used to store the


value of num5.

Question 3
A. Program
import java.util.Scanner;
public class ceil_floor // Write a java program to find ceil and floor values
of a number accepted by the user.Display the ceil and floor values.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double num1;
System.out.println("num1 = ");
num1=sc.nextDouble();
System.out.println("The ceiling of" +num1+ "is" +Math.ceil(num1));
System.out.println("The floor of "+num1+ "is"+Math.floor(num1));
}
}

B. Output :
num1 =
123.89
The ceiling of123.89is124.0
The floor of 123.89is123.0

C. Variable description table

Variable name Data type Description

Num1 double Used to store the value


of num1.

Question 4
A. Program
import java.util.Scanner;
public class round_absolute // Write a program in java to accept 3
numbers in variables x,y and z.Display it's rounded value,absolute
value.Also display the value of xy.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double x,y,z;
System.out.println("x = ");
x=sc.nextDouble();
System.out.println("y = ");
y=sc.nextDouble();
System.out.println("z = ");
z=sc.nextDouble();
System.out.println("Rounded form of" +x+ "is"+Math.round(x));
System.out.println("Absolute form of "+x+ "is"+Math.abs(x));
System.out.println("Rounded form of "+y+ "is"+Math.round(y));
System.out.println("Absolute form of "+y+ "is"+Math.abs(y));
System.out.println("Rounded form of "+z+ "is"+Math.round(z));
System.out.println("Absolute form of "+z+ "is"+Math.abs(z));
}
}

B. Output :
x=
4.5
y=
5.9
z=
-9.9
Rounded form of4.5is5
Absolute form of 4.5is4.5
Rounded form of 5.9is6
Absolute form of 5.9is5.9
Rounded form of -9.9is-10
Absolute form of -9.9is9.9

C. Variable description table

Variable name Data type Description

x double Used to store the


value of x.

y double Used to store the


value of y.

z double Used to store the


value of z.

Question 5
A. Program
public class random // Write a program to generate a random number
between 40 to 45.
{
public static void main(String args[])
{
double max=45,min=40,rangev,randv;
rangev=max-min+1;
randv=Math.random()*rangev+min;
System.out.println("The generated random number is:"+randv);
}
}

B. Output :
The generated random number is:42.35447216980545
The second output is :
The generated random number is:40.60845239117431

C. Variable description table

Variable name Data type Description

min double Used to store the


value of min.

max double Used to store the


value of max.

randv double Used to store the


value of randv.

rangev double Used to store the


value of rangev.

Computer Assignment 7
If-else statements
Question 1
A. Program
public class number_positive_or_negative // Write a Java program to
check if the number is positive or negative.
{
public static void main(String args[])
{
int number=10;
if(number > 0)
{
System.out.println("Number is positive");
}
else
{
System.out.println("Number is negative");
}
}
}

B. Output : Number is positive

C. Variable description table

Variable name Data type Description


number int Used to store the
value of number.

Question 2
A. Program
public class even_odd // write a java program to check if number is
even or odd.
{
public static void main(String args[])
{
int number=30;
if(number%2==0)
{
System.out.println("The number is even");
}
else
{
System.out.println("The number is odd");
}
}
}
B. Output : The number is even

C. Variable description table

Variable name Data type Description


number int Used to store the
value of number.

Question 3
A. Program
public class divisible_5 // write a java program to check if number is
divisible by 5 or not.
{
public static void main(String args[])
{
int number=53;
if(number%5==0)
{
System.out.println("Number is divisible by 5");
}
else
{
System.out.println("Number is not divisible by 5");
}
}
}
B. Output : Number is not divisible by 5

C. Variable description table

Variable name Data type Description


number int Used to store the
value of number.
Question 4
A. Program
public class voting // write a java program to enter the age of an
indiviual and check whether he is eligible of voting.
{
public static void main(String args[])
{
double age=17;
if(age>=18)
{
System.out.println("The person of age 17 is eligible of voting");
}
else
{
System.out.println("The person of age 17 is not eligible of
voting");
}
}
}

B. Output : The person of age 17 is not eligible of voting

C. Variable description table

Variable name Data type Description


age double Used to store the
value of age.
Question 5
A. Program
public class temperature // write a java program to accept
temperature of the day from the user and print if it is a hot wheather
or cool wheather on the following condition:
Temp<=28 : cool wheather
Temp>28 : hot wheather
{
public static void main(String args[])
{
double temp =27;
if(temp<=28)
{
System.out.println("The temperature is Cool");
}
else
{
System.out.println("The temperature is Hot");
}
}
}

B. Output : The temperature is Cool


C. Variable description table

Variable name Data type Description


temp double Used to store the
value of temp.
Computer Assignment 8
If-else ladder
Question 1
A. Program
import java.util.Scanner;
public class positivenegative // write a java program to print if the number
entered by the user is positive, negative or zero.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int num;
System.out.println("num =");
num =sc.nextInt();
if(num>0)
{
System.out.println("Number is positive");
}
else if(num<0)
{
System.out.println("Number is negative");
}
else if(num==0)
{
System.out.println("Numer is zero");
}
else
{
System.out.println("Number is negative");
}
}
}
B. Output : num =
25
Number is positive

C. Variable description table

Variable name Data type Description


num int Used to store the value
of num.

Question 2
A. Program
import java.util.Scanner;
public class smallcapital // write java program to print if the character
entered by the user is small or capital.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
char ch=sc.next().charAt(0);
int num=ch;
if(num>=65&&num<=90)
{
System.out.println("Capital");
}
else if(num>=97&&num<=122)
{
System.out.println("Small");
}
else
{
System.out.println("Capital");
}
}
}
B. Output: 68
Capital

C. Variable description table

Variable name Data type Description


num int Used to store the value
of num.

Question 3
A. Program
import java.util.Scanner;
public class week // write a java program to display the week day according
to the number entered.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n =sc.nextInt();
if(n==1)
{
System.out.println("Monday");
}
else if(n==2)
{
System.out.println("Tuesday");
}
else if(n==3)
{
System.out.println("Wednesday");
}
else if(n==4)
{
System.out.println("Thursday");
}
else if(n==5)
{
System.out.println("Friday");
}
else if(n==6)
{
System.out.println("Saturday");
}
else if(n==7)
{
System.out.println("Sunday");
}
else
{
System.out.println("Invalid");
}
}
}

B. Output : 9
Invalid

C. Variable description table

Variable name Data type Description


N int Used to store the value
of n.

Question 4 : // write a java program that inputs total marks of a student ,


calculate the percentage and display grade according to the folowing rules:
Percentage Grade
More than or equal to 80 A+
Between 70 and 80 A
Between 60 and 70 B
Between 50 and 60 C
Between 40 and 50 D
Between 33 and 40 E
Less than 33 F

A. Program
import java.util.Scanner;
public class totalmarks
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int marks=sc.nextInt();
System.out.println("The total marks of the student are"+marks);

if(marks>=80)
{
System.out.println("The grade of the student is A+");
}
else if(marks>70&&marks<=80)
{
System.out.println("The grade of the student is A");
}
else if(marks>60&&marks<=70)
{
System.out.println("The grade of the student is B");
}
else if(marks>50&&marks<=60)
{
System.out.println("The grade of the student is C");
}
else if(marks>40&&marks<=50)
{
System.out.println("The grade of the student is D");
}
else if(marks>33&&marks<=40)
{
System.out.println("The grade of the student is E");
}
else if(marks<33)
{
System.out.println("The grade of the student is F");
}
}
}

B. Output : 70
The total marks of the student are70
The grade of the student is B

C. Variable description table

Variable name Data type Description


marks int Used to store the
value of the marks.
Computer Assignment 9
Nested if()-else
Question 1
A. Program
import java.util.Scanner;
public class maxmin // Write a program in java print the maximum of the 3
numbers. The program should check if the numbers are greater than zero.
If yes proceed with finding the maximum of three else print an appropriate
message and terminate the program.
{
public static void main (String args[])
{
Scanner sc=new Scanner(System.in);
int num1,num2,num3,max;
System.out.println("Enter the numbers :");
num1=sc.nextInt();
num2=sc.nextInt();
num3=sc.nextInt();
System.out.println("The three numbers entered
are :"+num1+","+num2+"and"+num3);
if(num1>0 && num2>0 && num3>0)
{
if(num1>=num2 && num1>=num3)
{
System.out.println(num1+" is the maximum number");
}
else if(num2>=num1 && num2>=num3)
{
System.out.println(num2+" is the maximum number");
}
else if(num3>=num1)
{
System.out.println(num3+" is the maximum number");
}
else
{
System.out.println("The number entered is invalid.");
}

}
}
}
B. Output : Enter the numbers :
56
78
89
The three numbers entered are :56,78and89
89 s the maximum number
C. Variable description table
Variable name Data type Description
Num1 int Used to store the value
of num1.
Num2 int Used to store the value
of num2.
Num2 int Used to store the value
of num3.

Question 2
A. Program
import java.util.Scanner;
public class positive_negative // write a java program if the number
entered by the user is even positive or odd positive number, the
program should print an appropriate message and terminate if the
number entered is negative or zero.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int num;
System.out.println("Enter the value of num :");
num=sc.nextInt();
if(num>0)
{
System.out.println("The number entered is positive");
}
else if(num<0)
{
System.out.println("The number entered is negative");
}
else
{
System.out.println("The number is zero");
}
if(num%2==0)
{
System.out.println("The number entered is even");
}
else
{
System.out.println("The number entered is odd");
}
}
}

B. Output : Enter the value of num :


26
The number entered is positive
The number entered is even

C. Variable description table

Variable name Data type Description


num int Used to store
the value of
num.

Question 3
A. Program
import java.util.Scanner;
public class week_day // write a java program using nested if() to
display the week day according to the number entered eg 1=monday,
2= tuesday etc.The program should not compute numbers other than
1-7 that is appropriate message for inputs other than 1-7 and
terminate else find out the corresponding weekday for the number
entered.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n =sc.nextInt();
if(n==1)
{
System.out.println("Monday");
}
else if(n==2)
{
System.out.println("Tuesday");
}
else if(n==3)
{
System.out.println("Wednesday");
}
else if(n==4)
{
System.out.println("Thursday");
}
else if(n==5)
{
System.out.println("Friday");
}
else if(n==6)
{
System.out.println("Saturday");
}
else if(n==7)
{
System.out.println("Sunday");
}
else
{
System.out.println("Invalid");
}
}
}
B. Output : 6
Saturday

C. Variable description table

Variable name Data type Description


n int Used to store the
value of n.

Question 4
A. Program
import java.util.Scanner;
public class months // write a java program using nested if() to display the
month according to the number entered eg 1=January, 2=February etc. The
program should not compute on numbers other than 1-12 that is display
appropriate message for the inputs other than 1-12 and terminate else find
out the corresponding week day for the number entered.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
if(m==1)
{
System.out.println("January");
}
else if(m==2)
{
System.out.println("February");
}
else if(m==3)
{
System.out.println("March");
}
else if(m==4)
{
System.out.println("April");
}
else if(m==5)
{
System.out.println("May");
}
else if(m==6)
{
System.out.println("June");
}
else if(m==7)
{
System.out.println("July");
}
else if(m==8)
{
System.out.println("August");
}
else if(m==9)
{
System.out.println("September");
}
else if(m==10)
{
System.out.println("October");
}
else if(m==11)
{
System.out.println("November");
}
else if(m==12)
{
System.out.println("December");
}
else
{
System.out.println("Invalid statement");
}
}
}

B. Output : 9
September

C. Variable description table

Variable name Data type Description


m int Used to store the value
of m.

Question 5
A. Program
import java.util.Scanner;
public class angles // write a java program that takes 3 angles and states
whether these angles form an acute angle, obtuse angle or right angle.
{
public static void main (String args[])
{
Scanner sc=new Scanner(System.in);
double angle1;
System.out.println("Enter value of angle1 :");
angle1=sc.nextDouble();
double angle2;
System.out.println("Enter value of angle2 :");
angle2=sc.nextDouble();
double angle3;
System.out.println("Enter value of angle3 :");
angle3=sc.nextDouble();
if(angle1<90 && angle2<90 && angle3<90)
{
System.out.println("Acute angle triangle");
}
else if(angle1>90 || angle2>90 || angle3<90)
{
System.out.println("Obtuse angle triangle");
}
else if(angle1==90 || angle2==90 || angle3==90)
{
System.out.println("Right angled triangle");
}
else
{
System.out.println("Given angles cannot make a triangle");
}
}
}
B. Output : Enter value of angle1 :
89
Enter value of angle2 :
98
Enter value of angle3 :
88
Obtuse angle triangle

C. Variable description table

Variable name Data type Description


Angle1 double Used to store the
value of angle1.
Angle2 double Used to store the
value of angle2.
Angle3 double Used to store the
value of angle3.

Question 6
A. Program
import java.util.Scanner;
public class leap_year // write a program that inputs a 4-digit year
and tests whether it is a leap year or not.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int year;
System.out.println("Enter the value of year");
year=sc.nextInt();

if(year%100==0)
{
if(year%400==0)
System.out.println("Year"+year+"is a leap year");
else
System.out.println("Year"+year+"is not a leap year");
}

else
{
if(year%4==0)
System.out.println("Year"+year+"is a leap year");
else
System.out.println("Year"+year+"is not a leap year");
}
}
}

B. Output : Enter the value of year


2020
Year2020is a leap year

C. Variable description table

Variable name Data type Description


year int Used to store the
value of year.

Computer Assignment 10
Switch Statement
Date:25/10/2020
Question 1
A. Program
import java.util.Scanner;
public class weekday // Write a java program using switch statement to
display the week day according to the number entered. The program
should not compute on numbers other than 1-7 that is display appropriate
message for the inputs other than 1-7 and terminate else find out the
corresponding weekday for the number entered.
{
public static void main(String args [])
{
Scanner sc=new Scanner(System.in);
int weekday;
System.out.println("Enter the number of week: ");
weekday=sc.nextInt();
switch(weekday)
{
case 1: System.out.println("Sunday") ;
break ;
case 2: System.out.println("Monday") ;
break ;
case 3: System.out.println("Tuesday") ;
break ;
case 4: System.out.println("Wednesday") ;
break ;
case 5: System.out.println("Thursday") ;
break ;
case 6: System.out.println("Friday") ;
break ;
case 7: System.out.println("Saturday") ;
break ;
default : System.out.println("Invalid day number.") ;
}
}
}
B. Output: Enter the number of week:
5
Thursday
C. Variable description table

Variable name Data type Description


weekday int Used to store the value
of the weekday.

Question 2
A. Program
import java.util.Scanner;
public class month // Write a java program using switch statement to
display month according to the number entered. The program should not
compute on numbers other than 1-12 that is display appropiate message
for the inputs other than 1-12 and terminate else find out the
corresponding weekday for the number entered.
{
public static void main(String args [])
{
Scanner sc=new Scanner(System.in);
int month;
System.out.println("Enter the number of week: ");
month=sc.nextInt();
switch(month)
{
case 1: System.out.println("January") ;
break ;
case 2: System.out.println("February") ;
break ;
case 3: System.out.println("March") ;
break ;
case 4: System.out.println("April") ;
break ;
case 5: System.out.println("May") ;
break ;
case 6: System.out.println("June") ;
break ;
case 7: System.out.println("July") ;
break ;
case 8: System.out.println("August") ;
break ;
case 9: System.out.println("September") ;
break ;
case 10: System.out.println("October") ;
break ;
case 11: System.out.println("November") ;
break ;
case 12: System.out.println("December") ;
break ;
default : System.out.println("Invalid month number.") ;
}
}
}
B. Output: Enter the number of month:
9
September
C. Variable description table

Variable name Data type Description


month int Used to store the value
of the month.

Question 3
A. Program
import java.util.Scanner;
public class Days // Write a java program to display the number of days in a
particular month using switch statement.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int month,year,numDays=0;
System.out.println("Enter the month between 1 to 12: ");
month=sc.nextInt();
System.out.println("Enter the year : ");
year=sc.nextInt();
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays=31;
break;
case 4:
case 6:
case 9:
case 11:
numDays=30;
break;
case 2:
if(((year%4 == 0) && !(year%100 == 0)) || (year %400 == 0))
numDays=29;
else
numDays=28;
break;
default :
System.out.println("Invalid month.");
break;
}
System.out.println("Number of days = "+numDays);
}
}
B. Output: Enter the month between 1 to 12:
9
Enter the year :
2021
Number of days = 30
C. Variable description table

Variable name Data type Description


month int Used to store the value
of month.
numdays int Used to store the value
of numdays.
year int Used to store the value
of thr year.

Question 4
A. Program
import java.util.Scanner;
public class square // Write a program to display a menu for areas of
different shapes and then calculate the area of selected shape by asking for
required information.
{
public static void main(String args[])
{
float a,b,area=0;
int choice;
Scanner sc=new Scanner(System.in);
System.out.println("Shapes Area Menu:");
System.out.println("1. Circle.");
System.out.println("2. Square.");
System.out.println("3. Rectangle.");
System.out.println("4. Triangle.");
System.out.println("Enter your choice 1 to 4");
choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.println("Area of circle calculation");
System.out.println("Enter the radius: ");
a=sc.nextFloat();
area=3.14f*a*a;
break;
case 2:
System.out.println("Area of the square calculation");
System.out.println("Enter the side: ");
a=sc.nextFloat();
area=a*a;
break;
case 3:
System.out.println("Area of the rectangle calculation");
System.out.println("Enter the length and breadth: ");
a=sc.nextFloat();
b=sc.nextInt();
area=a*b;
break;
case 4:
System.out.println("Area of the triangle calculation");
System.out.println("Enter the base and height: ");
a=sc.nextFloat();
b=sc.nextInt();
area=0.5f*a*b;
break;
default :
System.out.println("Valid choices are 1 to 4.");
}
System.out.println("Area= "+area);
}
}
B. Output: Shapes Area Menu:
1. Circle.
2. Square.
3. Rectangle.
4. Triangle.
Enter your choice 1 to 4
3
Area of the rectangle calculation
Enter the length and breadth:
34
45
Area= 1530.0
C. Variable description table

Variable name Data type Description


A int Used to store the value
of a.
B int Used to store the value
of b.
area int Used to store the value
of b.
choice int Used to store the value
Of choice.

Question 5
A. Program
public class words // Write a program that receives a single digit as an
input and displays it in words.
{
public static void main(int num)
{
System.out.println(num+"in words is: ");
switch(num)
{
case 0: System.out.println("Zero");
break;
case 1: System.out.println("One");
break;
case 2: System.out.println("Two");
break;
case 3: System.out.println("Three");
break;
case 4: System.out.println("Four");
break;
case 5: System.out.println("Five");
break;
case 6: System.out.println("Six");
break;
case 7: System.out.println("Seven");
break;
case 8: System.out.println("Nine");
break;
case 9: System.out.println("Ten");
break;
}
}
}
B. Output : 5in words is:
Five
C. Variable description table

Variable name Data type Description


num int Used to store the value
of num.

Computer Assignment 11
For loop
Date:23/01/2021
Question 1
A. Program
public class print_num_forloop // Write a program to print numbers from
1 to 10.
{
public static void main(String args[])
{
for(int i=1; i<=10; i++)
{
System.out.println(i);
}
}
}

B. Output : 1
2
3
4
5
6
7
8
9
10

C. Variable description table

Variable name Data type Description


I int Used to store the value
of i.

Question 2
A. Program
public class sum_naturalnum_forloop // Write a program to calculate
the sum of first 10 natural number.
{
public static void main (String args[])
{
int sum=0;
for(int i=1; i<=10; i++)
{
sum += i;
}
System.out.println("Sum of the 1st 10 natural number is "+sum);
}
}

B. Output : Sum of the 1st 10 natural number is 55

C. Variable description table

Variable description Data type Description


sum int Used to store the
value of sum.
i int Used to store the
value of i.

Question 3
A. Program
import java.util.Scanner;
public class print_positivenum // Write a program that prompts the
user to input a positive integer. It should then print the multiplication
table of that user.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int num;

System.out.println("Enter the value any positive number: ");


num=sc.nextInt();

System.out.println("The multiplication table of " +num);


for(int i=1; i<=20; i++)
{
System.out.println(num+"x"+ i +"="+(num*i));
}
}
}

B. Output : Enter the value any positive number:


5
The multiplication table of 5
5x1=5
5x2=10
5x3=15
5x4=20
5x5=25
5x6=30
5x7=35
5x8=40
5x9=45
5x10=50
5x11=55
5x12=60
5x13=65
5x14=70
5x15=75
5x16=80
5x17=85
5x18=90
5x19=95
5x20=100

C. Variable description table

Variable name Data type Description


i int Used to store the
value of i.
num int Used to store the
value of num.

Question 4
A. Program
public class Two_loop // Write a Java program by using two for loops to
produce the output shown below:
{
public static void main(String args[])
{
printStars();
}

static void printStars()


{

int i,j;
for(i=0;i<=6;i++)
{
for(j=1;j<=7-i;j++)
System.out.print("*");
System.out.println("");
}
}
}

B. Output : *******
******
*****
****
***
**
*
C. variable description table
Variable name Data type Description
I int Used to store the value
of i.
J int Used to store the value
of j.

Computer Assignment 12
While- loop
Date:25/01/2021
Question 1
A. Program
import java.util.Scanner;
public class square_whileloop // Write a program to print square of number
from 1 to 10 using a while loop.
{
public static void main(String args[])
{
int i=1;
while(i<=10)
{
System.out.println(i*i);
i++;
}
}
}
B. Output: 1
4
9
16
25
36
49
64
81
100
C. Variable description table

Variable name Data type Description


I int Used to store the value
of i.

Question 2
A. Program
public class reverse // Write a program to reverse an already initialized
number.
{
public static void main(String args[])
{
int num=12345;
int reversenum=0;
while(num != 0)
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
System.out.println("Reverse of the initialized number is
"+reversenum);
}
}
B. Output: Reverse of the initialized number is 54321
C. Variable description table

Variable name Data type Description


num int Used to store the value
of num.
reversenum int Used to store the value
of reversenum.
Question 3
A. Program
public class naturalnum // Write a program to print sum of 1t 100
natural numbers using while loop.
{
public static void main(String args[])
{
int num=100;
int i=1;
int sum=0;
while(1<=num)
{
sum += 1;
i++;
}
System.out.println("Sum = "+sum);
}
}
B. Output: Sum=5050
C. Variable description table

Variable name Data type Description


num int Used to store the
value of num.
i int Used to store the
value of i.
sum int Used to store the
value of sum.
Question 4
A. Program
import java.util.Scanner;
public class sum_positive_num // Write a java program to calculate the
sum of positive numbers only.
{
public static void main(String args[])
{
int sum=0;
Scanner sc=new Scanner(System.in);

System.out.println("Enter a number: ");


int number=sc.nextInt();
while(number >= 0)
{
sum +=number;
System.out.println("Enter a number");
number=sc.nextInt();
}

System.out.println("Sum = "+sum);
}
}

B. Output : Enter a number


-78
Sum = 8
C. Variable description table

Variable name Data type Description


sum int Used to store the value
of sum.
number int Used to store the value
of number.
Computer Assignment 12
While- loop
Date:25/01/2021
Question 1
A. Program
import java.util.Scanner;
public class square_whileloop // Write a program to print square of number
from 1 to 10 using a while loop.
{
public static void main(String args[])
{
int i=1;
while(i<=10)
{
System.out.println(i*i);
i++;
}
}
}
B. Output: 1
4
9
16
25
36
49
64
81
100
C. Variable description table
Variable name Data type Description
I int Used to store the value
of i.

Question 2
A. Program
public class reverse // Write a program to reverse an already initialized
number.
{
public static void main(String args[])
{
int num=12345;
int reversenum=0;
while(num != 0)
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
System.out.println("Reverse of the initialized number is
"+reversenum);
}
}
B. Output: Reverse of the initialized number is 54321
C. Variable description table

Variable name Data type Description


num int Used to store the value
of num.
reversenum int Used to store the value
of reversenum.

Question 3
D. Program
public class naturalnum // Write a program to print sum of 1t 100
natural numbers using while loop.
{
public static void main(String args[])
{
int num=100;
int i=1;
int sum=0;
while(1<=num)
{
sum += 1;
i++;
}
System.out.println("Sum = "+sum);
}
}
E. Output: Sum=5050
F. Variable description table

Variable name Data type Description


num int Used to store the
value of num.
i int Used to store the
value of i.
sum int Used to store the
value of sum.

Question 4
A. Program
import java.util.Scanner;
public class sum_positive_num // Write a java program to calculate the
sum of positive numbers only.
{
public static void main(String args[])
{
int sum=0;
Scanner sc=new Scanner(System.in);

System.out.println("Enter a number: ");


int number=sc.nextInt();
while(number >= 0)
{
sum +=number;
System.out.println("Enter a number");
number=sc.nextInt();
}

System.out.println("Sum = "+sum);
}
}

B. Output : Enter a number


-78
Sum = 8
C. Variable description table

Variable name Data type Description


sum int Used to store the value
of sum.
number int Used to store the value
of number.

Computer Assignment 13
While loop to for loop conversion
Date:06/02/2021
Question 1
A. Program
import java.util.Scanner;
public class square_whiletofor // Convert the following program to print
square of number from 1 to 10 using for loop.
{
public static void main(String args[])
{
int i=1;
for(i=1;i<=10;i++)
{
System.out.println(i*i);

}
}
}
B. Output : 1
4
9
16
25
36
49
64
81
100

C. Variable description table

Variable name Data type Description


I int Used to store the value
of i.

Question 2
A. Program
public class reverse_whiletofor // Convert the following program to reverse
an already initialized number using for loop.
{
public static void main(String args[])
{
int num=12345;
int reversenum=0;
for(;num !=0; )
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
System.out.println("Reverse of the initialized number is
"+reversenum);
}
}

B. Output : Reverse of the initialized number is 54321


C. Variable description table

Variable name Data type Description


num int Used to store the value
of num.
reversenum int Used to store the value
of reversenum.

Question 3
A. Program
public class naturalnum_whiletofor // Convert the following program to
print sum of 1st 100 natural numbers using for loop.
{
public static void main(String args[])
{
int num=100;
int count;
int sum=0;
for(count=1;count<=num;count++)
{
sum = sum + count;

}
System.out.println("Sum = "+sum);
}
}
B. Output : Sum = 5050
C. Variable description table

Variable name Data type Description


num int Used to store the value
of num.
count int Used to store the value
of count.
sum int Used to store the value
of sum.

Question 4
A. Program
import java.util.Scanner;
public class sum_positive_num_whiletofor // Convert the following java
program to calculate the sum of positive numbers only using a for loop.
{
public static void main(String args[])
{
int sum=0;
Scanner sc=new Scanner(System.in);

System.out.println("Enter a number: ");


int number=sc.nextInt();
for( ;number>=0; )
{
sum += number;
System.out.println("Enter a number");
number=sc.nextInt();
}

System.out.println("Sum = "+sum);
}
}
B. Output : Enter a number:
56
Enter a number
89
Enter a number
-9
Sum = 145
C. Variable description table

Variable name Data type Description


Number int Used to store the value
of number.
sum int Used to store the value
of sum.
Computer Assignment 14
For loop to while loop conversion
Date:06/02/2021
Question 1
A. Program
public class printnum_fortowhile // Convert the following program to print
numbers from 1 to 10 using while loop.
{
public static void main(String args[])
{
int num=0;
while(num<=10)
{
System.out.println(num);
num++;
}
}
}
B. Output : 0
1
2
3
4
5
6
7
8
9
10

C. Variable description table

Variable name Data type Description


num int Used to store the value
of num.

Question 2
A. Program
public class naturalnum_fortowhile // Convert the following program to
calculate the sum of the 1st 100 natural number using while loop.
{
public static void main(String args[])
{
int num=1;
int sum=0;
while(num<=100)
{
sum = sum + num;
num = num + 1;
}
System.out.println("Sum = "+sum);
}
}
B. Output : Sum = 5050
C. Variable description table

Variable name Data type Description


num int Used to store the value
of num.
sum int Used to store the value
of sum.

Question 3
A. Program
import java.util.Scanner;
public class positiveint_fortowhile // Convert the following program that
prompts the user to input a positive integer.It should then print the
multiplication table of that number using while loop.
{
public static void main(String args[])
{
Scanner console=new Scanner(System.in);
int num;
System.out.println("Enter any positive integer:");
num=console.nextInt();
System.out.println("Multiplication table of"+num);
int i=1;
while(i<=20)
{
System.out.println(num+"x"+i+"="+(num*i));
i++;
}
}
}
B. Output : Enter any positive integer:
5
Multiplication table of5
5x1=5
5x2=10
5x3=15
5x4=20
5x5=25
5x6=30
5x7=35
5x8=40
5x9=45
5x10=50
5x11=55
5x12=60
5x13=65
5x14=70
5x15=75
5x16=80
5x17=85
5x18=90
5x19=95
5x20=100
C. Variable description table

Variable name Data type Description


num int Used to store the value
of num.
I int Used to store the value
of i.
Question 4
A. Program
public class Twoloop_fortowhile // Convert the following java program by
using two while loops to produce the output shown below:
{
public static void main(String args[])
{
printStars();
}

static void printStars()


{

int i,j;
i=1;
while(i<=7)
{
j=i;
while(j<=7)
{
System.out.print("*");
j++;
}
i++;
System.out.println("");
}
}
}
B. Output : *******
******
*****
****
***
**
*
C. Variable description table

Variable name Data type Description


I int Used to store the value
of i.
J int Used to store the value
of j.

Computer Assignment 15
For and While with If and Else/Switch
Date:14/02/2021
Question 1
A. Program
import java.util.Scanner;
public class Mult_positivenum // Write a program that prompts a user to
input a integer. If the number is positive it should then print the
multiplication table of that number else it should prompt invlaid entry a
positive number.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Please enter a number:");
int num=sc.nextInt();
if(num>=0)
{
int i=1;
while(i<=10)
{
int mul=i*num;
System.out.println(num+"*"+i+"="+mul);
i++;
}
}
else
System.out.println("You have to enter a negative number. Please enter
a positive number:");
}
}
B. Output : Please enter a number:
6
6*1=6
6*2=12
6*3=18
6*4=24
6*5=30
6*6=36
6*7=42
6*8=48
6*9=54
6*10=60
C. Variable description table

Variable name Data type Description


I int Used to store the value
of i.
num int Used to store the value
of num.
mul int Used to store the value
of mul.

Question 2
A. Program
public class patterns // Write a program to generate the following patterns
using iterations statements use a while loop.
{
public static void main(String args[])
{
int i=1,j=1;
{
while(i<=5)
{
while(j<=1)
{
if(j % 2 == 0)
System.out.println("# ");
else
System.out.println("* ");
j++;
}
System.out.println();
i++;
j=1;
}
}
}
}
B. Output : *
* #
*#*
*#*#
*#*#*
C. Variable description table

Variable name Data type Description


I int Used to store the value
of i.
J int Used to store the value
of j.
Question 3
A. Program

import java.util.Scanner;
public class factors // Write a java program t calculate and display the
factorials of all the numbers between 'm' and 'n' use for loop for iteration.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter m: ");
int m = sc.nextInt();
System.out.println("Enter n: ");
int n = sc.nextInt();

if(m < n && m > 0 && n > 0)


{
for(int i = m; i <= n; i++)
{
long fact=1;
for(int j = 1; j<=1; j++)
fact*=j;
System.out.println("Factorial of"+i+"="+fact);
}
}
else
{
System.out.println("Invalid input");
}
}
}
B. Output: Enter m:
55
Enter n:
77
Factorial of55=1
Factorial of56=1
Factorial of57=1
Factorial of58=1
Factorial of59=1
Factorial of60=1
Factorial of61=1
Factorial of62=1
Factorial of63=1
Factorial of64=1
Factorial of65=1
Factorial of66=1
Factorial of67=1
Factorial of68=1
Factorial of69=1
Factorial of70=1
Factorial of71=1
Factorial of72=1
Factorial of73=1
Factorial of74=1
Factorial of75=1
Factorial of76=1
Factorial of77=1
C. Variable description table

Variable name Data type Description


m int Used to store the value
of m.
N int Used to store the value
of n.
I int Used to store the value
of i.
J int Used to store the value
of j.

Question 4
A. Program
import java.util.Scanner;
public class prime_nonprime // Write a menu driven propram to display all
prime and non-prime numbers from 1 to 100. Enter 1:to display all prime
numbers. Enter 2:to display all non-prime numbers.
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1 : to display all prime numbers");
System.out.println("Enter 2 : to display all non-prime numbers");
System.out.println("Enter your choice: ");
int choice = sc.nextInt();
switch(choice)
{
case 1:
for(int i = 2; i <= 100; i++)
{
boolean isprime = true;
for(int j = 2; j <= i / 2; j++)
{
if(i % j == 0)
{
isprime = false;
break;
}
}
if(isprime)
System.out.println(i);
}
break;

case 2:
System.out.println(1);
for(int i = 2; i <= 100; i++)
{
boolean isprime = true;
for(int j = 2; j<=i / 2; j++)
{
if(i % j ==0)
{
isprime = false;
break;
}
}
if(!isprime)
System.out.println(i);
}
break;
default: System.out.println("Incorrect Choice");
break;
}
}
}
B. Output: Enter 1 : to display all prime numbers
Enter 2 : to display all non-prime numbers
Enter your choice:
1
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
C. Variable description table

Variable name Data type Description


choice int Used to store the value
of choice.
isprime int Used to store the value
of isprime.
I int Used to store the value
of i.
J int Used to sotre the value
of j.

You might also like