Computer Collated Krishna 9A
Computer Collated Krishna 9A
Computer Collated Krishna 9A
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
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);
}
}
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);
}
}
2
The volume of the sphere is22.82666778564453
C. Variable Description Table
Variable Name Data Type Description
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);
}
}
3
Enter the value
6
The volume of the cone is56.52000188827515
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:
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:
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:
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
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
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
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);
}
}
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
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
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
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
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
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
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
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
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
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
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
B. Output :
num1 is=
56
num2 is=
78
Max between56and78is78
Min between56and78is56
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
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
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
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
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");
}
}
}
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
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
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
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
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
}
}
}
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");
}
}
}
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
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
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
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");
}
}
}
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
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
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
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
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
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
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);
}
}
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;
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();
}
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
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
System.out.println("Sum = "+sum);
}
}
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
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
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("Sum = "+sum);
}
}
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
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);
}
}
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
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("Sum = "+sum);
}
}
B. Output : Enter a number:
56
Enter a number
89
Enter a number
-9
Sum = 145
C. Variable description table
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
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
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
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
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
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();
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