You are on page 1of 20

1|Page

Student Profile

Name: Pratyush Agrawal

Class: X
Section: L

Roll Number: 13453

Subject- Computer Applications


2|Page

Index
Sl. Programs Page
Number No.
1. Printing all the perfect numbers in a given 3-5
range
2. Printing the following pattern 6-7
*
**
***
****
3. Writing a menu driven program - 8-12
a) To generate and print the desired letters and
their Unicodes
b) To check whether an entered number is a lmao
number or not.
4. Designing a class, to overload function 13-
volume(), 16
1) to print the area of a sphere,
2) to print the area of a cuboid and 3) to print
area of cylinder, on the basis of user’s choice

5. Designing a class to overload function series(), to 17-


print the sum of the following series, based on user 20
inputs
1) =(1/a¹)+ (2/a²)+ (3/a³)+......n
2) =1+(1/2!)+(1/3!)+(1/4!)+...+(1/n!)
3|Page

Program 1:
import java.util.*; //importing packages
/* writing a program to print all the perfect numbers within a given range
by the user*/
class prog1
{
public static void main(String []args )
{
Scanner sc = new Scanner(System.in);
int low, high; //declaring low and high variables
System.out.println("Enter your range, first low then high ");
//taking low and high value
low = sc.nextInt();
high = sc.nextInt();
if(low>high) //checking if low is bigger than high
System.exit(0);
while(low<=high)
{
int sum = 1;
for(int i=1;i*i<=low;i++)
{
if(low%i==0) //checking if 'low' is divisible by i
{
sum+=(i + low/ i);
}

}
if (sum== low&&low!=1) //checking if sum is not 1 and is not
equal to low
{
System.out.print(low+" ");//printing the perfect numbers
4|Page

}
low++;
}

}
}

Outputs:
1)

2)
5|Page

Variable Table:
Name of Type of the Description of the variable
the variable
Variable
low int Stores the low value of the range given by the
user.

high int Stores the high value of the range given by the
user.

sum int Stores the sum of all the factors

i int Iterative variable;


counts the number of times the
loop will run
6|Page

Program 2:
/* writing a program to printing the pattern:
*
**
***
****
*/
class project2pyramid //class name
{
public static void main(String args[]) //declaring main method
{
System.out.println("The pattern:");
for(int r=1;r<=4;r++) //running loop for maximum numbers of row
{
for(int s=4;s>=r;s--)
{
System.out.print(" "); //printing spaces between the stars
}
for(int c=1;c<=r;c++)
{
System.out.print(" *"); //printing stars with a space
}
System.out.println();
}
}
7|Page

}
Output:

Variable Table:
Name of Type of the Description of the variable
the variable
Variable
r int Iterative variable;
counts the number of rows

s int Iterative variable;


counts the number of times the
inner loop will run

c int Iterative variable;


8|Page

Program 3:
import java.util.*;
/* writing a menu driven program to print the desired letters and their
unicodes; and check whether an entered number is a lmao number or
not. A lmao number is a number where the first digit of the number is
equal to the sum of the remaining digits of the number ---> for example,
5014 is a lmao number because the first digit: 5; is equal to the sum of
the remaining digits: 0+1+4=5 */
class prog3 //creating class
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1 for unicodes of your choice of letters
\nEnter 2 to check if a number is lmao "); //asking user to enter their
choice
int ch=sc.nextInt();
switch(ch)
{
case 1:
char ab;
System.out.println("Enter the first letter then the last letter in
capitals"); //asking user to enter the first and last letter
char f=sc.next().charAt(0);
char l= sc.next().charAt(0);
9|Page

System.out.println("Letters\tUnicodes");
for(ab=f;ab<=l;ab++)
{
System.out.println(ab+"\t"+(int)ab); //printing the letter
and its unicode
}
break;

case 2:
System.out.println("Enter a number"); //asking user to enter a
number
int n=sc.nextInt();
int s=0;
int ld=0;
while(n!=0)
{
ld= n%10;
s=s+ld;
n=n/10;
}
if((s-ld)==ld) //checking if the first digit of the number ==
the sum of the remaining digits of the number
System.out.println(m+" is a lmao number");
else
10 | P a g e

System.out.println(m+" is not a lmao number");


break;

default:
System.out.println("Please enter the correct choice!");
}
}
}

Variable Table
Name of the Type of the Description of the variable
Variable variable

n int stores the value entered by


user

ch int Stores the choice of the user

s int Stores the sum of digits of


the number

ld int Extracts and stores digits of


the number

ab char Used as an iterative variable.

f char Stores the first character


entered by the user
11 | P a g e

l char Stores the last character


entered by the user

Output:

1)

2)
12 | P a g e

3)
13 | P a g e

Program 4:
import java.util.*;
/* writing a program, where we design a class, to overload function
volume(), 1) to print the volume of a sphere, 2) to print the volume of a
cuboid and 3) to print volume of cylinder, on the basis of user’s choice*/
class shapes
{
double volume(double r)
{
double v= (4.0/3.0)*(22/7.0)*r*r*r; //calculating volume of sphere
return v;
}
double volume(double l, double b, double h)
{
double v= l*b*h; //calculating volume of cuboid
return v;
}
double volume(double h, double r)
{
double v= (22.0/7.0)*r*r*h; //calculating volume of cylinder
return v;
}
void main()
{
Scanner sc=new Scanner(System.in);
14 | P a g e

System.out.println("Enter 1 for volume of sphere; 2 for cylinder; 3 for


cuboid"); //asking user to input its choice
int ch= sc.nextInt();
if(ch==1) //checking if number is 1
{
System.out.println("Enter radius");
double r=sc.nextDouble();
double g=volume(r); //the actual parameters
System.out.println("volume of sphere: "+g); //printing the volume
}
else if(ch==2) //checking condition
{
System.out.println("Enter height and radius");
double h=sc.nextDouble();
double r=sc.nextDouble();
double g=volume(h,r); //the actual parameters
System.out.println("volume of cylinder: "+g); //printing the volume
}
else if(ch==3) //checking condition
{
System.out.println("Enter length, breadth and height");
double l=sc. nextDouble();
double b=sc. nextDouble();
double h=sc.nextDouble();
15 | P a g e

double g=volume(l,b,h); //the actual parameters


System.out.println("volume of cuboid: "+g); //printing the volume

}
}
}
Output:

1)

2)
16 | P a g e

Variable Table
Name of the Type of the Description of the
Variable variable variable

v double Calculates and


stores volume of the
different shapes

ch int Takes user’s choice as


input

h double Stores the height of the


cylinder and cuboid

r double Stores the radius of


cylinder and sphere

l double Stores the length of cuboid

b double Stores the breadth of


cuboid

g double Calls the function


17 | P a g e

Program 5:
import java.util.*;
/* Designing a class to overload function series(), to print the sum of the
following series, based on user’s choice-
1) =(1/a¹)+ (2/a²)+ (3/a³)+......n
2) =1+(1/2!)+(1/3!)+(1/4!)+...+(1/n!) */
class Prog5 //class name
{
//SERIES 1
double series(double a, double n)
{
double s=0.0;
System.out.println("Series 1: (1/a¹)+ (2/a²)+ (3/a³)+......n");
for(int i=1;i<=n;i++) //loop for calculating the sum till the limit
{
s=s+(double)(i/(Math.pow(a,i))); //calculating the sum
}
return s;
}
//SERIES 2
double series(double n)
{
double s=0.0;
System.out.println("Series 2: 1+(1/2!)+(1/3!)+(1/4!)+...+(1/n!)");
for(int i=1;i<=n;i++) //running the loop for number of times the
fraction will be added
{
int f=1;
for(int j=1;j<=i;j++) //taking out the factorial of all digits one by one till
the limit
{
f*=j;
18 | P a g e

}
s=s+(double)1/f; //calculating the sum
}
return s;
}
void main()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter your choice of series: \nSeries 1: (1/a¹)+
(2/a²)+ (3/a³)+......n\nSeries 2: 1+(1/2!)+(1/3!)+(1/4!)+...+(1/n!)");
int ch=sc.nextInt();
if(ch==1) //checking the condition
{
System.out.println("Enter value of a and n");
double a=sc.nextDouble();
double n=sc.nextDouble();
double f1= series(a,n);
System.out.println("Sum of first series: "+f1);
}
else if(ch==2)//checking the condition
{
System.out.println("Enter the limit of the series...n");
double n= sc.nextDouble();
double f1=series(n);
System.out.println("Sum: "+f1);
}
else
System.out.println("Wrong choice");
}
}
19 | P a g e

Output:
1)

2)

3)
20 | P a g e

Variable Table
Name of the Type of the Description of
Variable variable the variable
f int Stores the factorial of digits

a double Takes the value


of denominator in
series 1.
n double Stores the limit of both
the series

i int Make sures the loop runs


within the
limit; iterative
variable
j int Make sures the loop runs
within the
limit; iterative
variable
s double Stores the sum of series

ch int Stores the choice of user

f1 double Calls the functions

You might also like