Professional Documents
Culture Documents
Record
YEAR: 2018-2019
GRADE : 10
ROLL NO : 30
TOPICS : PROGRAMS
1
COMPUTER RECORD
Siddhartha Mukherjee
Grade 10-
LIST OF PROGRAMS
5. Finding the sums of all odd numbers, and all even numbers, below a given number 15
17. Sorting the even numbers in a given array in ascending order using Bubble-Sort 47
2
Program 1: Checking if a number is a Magic Number or not
A magic number is a number whose continuous sum of digits equals 1. For example:
CODE:
importjava.util.*;
class P1_MagicNumber
if (isHappynumber(Number))
else
3
int Check1, Check2;
do
Check1 = numSquareSum(Check1);
Check2 = numSquareSum(numSquareSum(Check2));
if(Check1==1)
return true;
else
return false;
int Sum = 0;
while (n!= 0)
Sum += n%10;
n /= 10;
return Sum;
4
}
OUTPUT:
5
VARIABLE DESCRIPTION:
Variable Name Data Type Purpose
Number int To accept input from the user
Stores the value of one iteration
Check1 int of the addition of the digits of the
given number
Stores the value of two iterations
Check2 int of the addition of the digits of the
given number
Stores the sum of the digits of the
Sum int
number
Stores the value of the number in
n int
the units position in each iteration
CODE:
importjava.util.Scanner;
System.out.println("ENTER A NUMBER");
intnum=in.nextInt();
int choice=in.nextInt();
switch(choice)
case 1:
if(num%2==0)
else
break;
case 2:
if(num%17==0)
else
break;
7
case 3:
else
break;
default:
System.out.println("INVALID OPTION");
break;
OUTPUT:
8
9
Variable Description:
PROGRAM 2
VARIABLE DESCRIPTION
CODE:
import java.util.*;
class P3_PythagoreanTriplets
10
public static Scanner in = new Scanner(System.in);
N1= in.nextInt();
N2= in.nextInt();
N3= in.nextInt();
if((N1*N1)==(N2*N2)+(N3*N3))
System.out.println("This is because the square of "+N1+" equals the sum of squares of "+N2+"
and "+N3+".");
else if((N2*N2)==(N1*N1)+(N3*N3))
System.out.println("This is because the square of "+N2+" equals the sum of squares of "+N1+"
and "+N3+".");
else if((N3*N3)==(N1*N1)+(N2*N2))
System.out.println("This is because the square of "+N3+" equals the sum of squares of "+N1+"
and "+N2+".");
else
11
{
OUTPUT:
VARIABLE DESCRPITION:
12
Variable Name Data Type Purpose
N1 int To store the value of the first
digit entered by the user
N2 int To store the value of the second
digit entered by the user
N3 int To store the value of the third
digit entered by the user
CODE:
importjava.util.Scanner;
int x=in.nextInt();
int c=0,i;
for(i=2;i<x;i++)
if(x%i==0)
c++;
13
}
if(c!=0)
else
OUTPUT:
14
Variable Description:
PROGRAM 4
VARIABLE DESCRIPTION
CODE:
import java.util.*;
class P5_SumOfOddsAndEvens
System.out.println("Enter a number. The sum of all odd numbers and sum of all even numbers");
Limit = in.nextInt();
for(I=1;I<Limit;I++)
if(I%2!=0){SumOdds+=I;}
else if(I%2==0){SumEvens+=I;}
OUTPUT:
VARIABLE DESCRIPTION:
Variable Description Data Type Purpose
Stores the value of the number
entered by the user below which
Limit int
the sum of all odd and even
numbers have to be calculated
Stores the sum of all odd
SumOdds int numbers below the entered
number
Stores the sum of all even
SumEvens int numbers below the entered
number
I int Control variable
i) s=1*2+2*3+3*4+…+9*10
ii) s=1*2+3*4+5*6+…+9*10
iii) s=1*3+2*4+3*5+…+9*11
iv) s=1–2+3–4+5–6+…+19–20
17
v) s= 1+1/2+1/3+1/4+1/5+…+1/10
vi) s= 1/2+2/3+3/4+4/5+…+9/10
vii) s= 1-1/2+1/3-1/4+1/5-…-1/10
CODE:
inti,j, s=0;
for(i=1;i<10;i++)
j=i+1;
s+=i*j;
inti,j,s=0;
for(i=1;i<10;i+=2)
j=i+1;
s+=i*j;
18
}
inti,j,s=0;
for(i=1;i<10;i++)
j=i+2;
s+=i*j;
inti,j,s=0;
for(i=1;i<20;i+=2)
j=i+1;
s+=i-j;
inti,j,s=0;
for(i=1;i<10;i++)
19
j=1/i;
s+=j;
inti,j,s=0;
for(i=1;i<10;i+=2)
j=i+1;
s+=i/j;
inti,j,k,s=0;
for(i=1;i<10;i+=2)
j=1/i;
k=1/(i+1);
s+=j-k;
20
}
The program contains seven methods, each programmed to find the sum of a particular series. The name
of the method corresponds to the series as mentioned above. Each method uses the same 2 global
variables.
OUTPUT:
21
Variable Description:
PROGRAM 6
VARIABLE DESCRIPTION
int i it is a control variable
int j
it is used to calculate and store the individual elements of therespective series
int sum it stores the sum of the respective series
CODE:
import java.util.*;
import java.awt.*;
22
class P7_PellSeries
System.out.print(T1+", ");
System.out.print(T2+", ");
for(int I=1;I<=13;I++)
T3=(2*T2)+T1;
System.out.print(T3+", ");
T1=T2;
T2=T3;
OUTPUT:
23
VARIABLE DESCRIPTION:
Variable Name Data Type Purpose
Stores the value of the first term
T1 int in the Pell Series and every
following term
Stores the value of the second
term in the Pell Series and
T2 int
every following term just after
T1
Stores the value of the latest
T3 int term in the Pell Series which is
calculated on the spot
Control variable which runs the
I int
for-loop
CODE:
importjava.util.Scanner;
intx,y,sumx=0,sumy=0,i;
x=in.nextInt();
y=in.nextInt();
for(i=1;i<x;i++)
24
{
if(x%i==0)
sumx+=i;
for(i=1;i<y;i++)
if(y%i==0)
sumy+=i;
if(x==sumy)
else
25
WORKING OF THE PROGRAM:
The program first accepts two numbers from the user. Then, a for-loop finds the factors of one number
and another for-loop does the same to the other number. An if-statement then checks if the sum of factors
of each number equals the other number. If yes, then the pair is displayed as an amicable pair.
OUTPUT:
Variable Description:
PROGRAM 8
VARIABLE DESCRIPTION
int i it is a control variable
int x it is used to store the first number
int y it is used to store the second number
sum x it stores the sum of digits of the value stored in the variable x
sum y it stores the sum of digits of the value stored in the variable x
26
Program 9: Checking if a number is an Armstrong Number
A number is said to be an Armstrong Number when the sum of cubes of its digits is equal to the number
itself. This program accepts a number and checks if it is an Armstrong Number.
CODE:
import java.util.*;
class P9_ArmstrongNumber
Number = in.nextInt();
Copy=Number;
do
I=Number%10;
Sum+=I*I*I;
Number=Number/10;
while(Number!=0);
OUTPUT:
VARIABLE DESCRIPTION:
Variable Name Data Type Purpose
To store the value of the number
Number int
entered by the user
To duplicate the value entered by
Copy int the user for comparison in the
end
Stores the sum of cubes of the
Sum int
digits of the given number
Stores the value of each
I int individual number in the digit to
obtain its cubes
28
Program 10: Checking if a number is a Happy Number
A number is said to be a Happy Number when its continuous sum of squares of its digits eventually equals
1. For example:
97 92 + 72 130 12 + 32 + 02 10 12 + 02 1
CODE:
importjava.util.Scanner;
int x=in.nextInt();
boolean flag=false,loop=true;
int k=0,sum=0;
do
do
k=x%10;
x=x/10;
29
sum+=k*k;
}while(x>0);
x=sum;
sum=0;
}while(x>=10);
if(x==1)
else
OUTPUT:
30
Variable Description:
PROGRAM 10
VARIABLE DESCRIPTION
int x it is used to store the vaalue entered by the user
int k it is used to store the last digit of the number
int sum it used to store the calculated sum
CODE:
import java.util.*;
class P11_PalindromeNumber
Copy=Number;
while(Number>0)
I = Number%10;
Number = Number/10;
if(Sum==Copy){return true;}
else{return false;}
32
for(I=0;I<10;I++)
Numbers[I]=in.nextInt();
Copy=Numbers[I];
while(Numbers[I]>0)
A = Numbers[I]%10;
Numbers[I] = Numbers[I]/10;
if(Copy==Sum)
Found=true;
if(Copy>Largest)
Largest=Copy;
Sum=0;
if(Found==true)
else
33
System.out.println("None of the numbers entered is a palindrome number.");
In the method public static void main (String[] args), the user enters 10 numbers. The program checks if
each number is a Palindrome Number, and finally displays the largest Palindrome number. If no
Palindrome numbers were entered, the program states that no Palindrome numbers was entered.
OUTPUT:
34
VARIABLE DESCRIPTION:
Variable Name Data Type Purpose
To duplicate the number entered
Copy int
so that the tests can be compared
Stores the value of the unit’s digit
I int
in each iteration
Stores the value of the reversed
Sum int
number
Stores the value of the largest
Largest int
palindrome number
Stores the value of the unit’s digit
A int
in each iteration
Stores the ten numbers which are
Numbers int array
to be checked
To check if at least one
Found boolean palindrome number has been
entered
CODE:
importjava.util.Scanner;
publicint LCM(inta,int b)
inti=0,temp;
if(b<a)
temp=a;
a=b;
b=temp;
for(i=a;i<=a*b;i++)
if(i%a==0 &&i%b==0)
break;
returni;
{
36
Scanner in=new Scanner(System.in);
intx,y=1;
for(int j=0;j<10;j++)
x=in.nextInt();
y=LCM(x,y);
OUTPUT:
Result of Method 1
37
Result of Method 2
PROGRAM 12
VARIABLE DESCRIPTION
int a stores the first value passed onto the function
int b stores the second value passed onto the function
int i it is a control variable
int temp it is used in swapping values
int x it is used to store te value entered by the user
int y it is used to store the LCM
int j it is a control variable
The second method accepts one number (for example, n) and finds the sum of the following series:
CODE:
import java.math.*;
38
class P13_SeriesOverload
Sum=0;
I2=0;
for(I=1;I<=n;I++)
for(I2=I;I2>0;--I2)
FS=FS*I2;
Sum+=(Math.pow(x,I))/FS;
FS=1;
I2=0;
return Sum;
Sum=0;Sum2=1;D=0;
for(I=1;I<=n;++I)
for(D=I;D>0;--D)
39
Sum2=Sum2*D;
Sum=Sum+(1/Sum2);
Sum2=1;
D=0;
return Sum;
In the second method, the program accepts one number and enters a for-loop where a control variable’s
value increases for each iteration. Another variable then finds the factorial of the control variable at that
point to use as the denominator. The sum is then found by repeatedly dividing 1 by the factorial of that
number. The sum is then displayed.
OUTPUT:
40
VARIABLE DESCRIPTION:
Variable Name Data Type Purpose
Sum int Stores the sum of the first series
Stores the sum of the second
Sum2 int
series
I int Control variable for both series
Control variable for the first
I2 int
series exclusively
Stores the sum of the factorial of
FS int
each number
Value depending on which the
x int
sum of series is executed
Value depending on which the
n int
sum of series is executed
CODE:
importjava.util.Scanner;
41
public void display()
System.out.println("ENTER A SENTENCE");
String x=in.nextLine();
String rev="";
for(inti=0;i<x.length();i++)
ac=x.charAt(i);
ac=(char)((int)ac+32);
rev+=ac;
else
if(x.charAt(i)>='a' &&x.charAt(i)<='z')
ac=(char)((int)ac-32);
rev+=ac;
else
rev+=ac;
OUTPUT:
Variable Description:
PROGRAM 14
VARIABLE DESCRIPTION
String x it is used to store the entered sentence
String rev it is used to store the reversed case sentence
char ac it is used to store esch character of the sentence
int i it is a control variable
CODE:
import java.util.*;
class P15_PalindromeWord
{
43
public static Scanner in = new Scanner(System.in);
Sentence=in.nextLine();
for(I=0;I<Sentence.length();I++)
C=Sentence.charAt(I);
ReversedSentence=C+ReversedSentence;
if(Sentence.equals(ReversedSentence))
System.out.println(Sentence+" is a palindrome.");
else if(Sentence.equalsIgnoreCase(ReversedSentence)&&Sentence!=ReversedSentence)
else
44
}
OUTPUT:
VARIABLE DESCRIPTION:
Variable Name Data Type Purpose
Stores the sentence entered by the
Sentence String
word
Stores the value of the reversed
ReversedSentence String
word
Stores the value of each letter
C char individually, one at a time, to add
to the reverse of that word
I int Control variable
1. The letters which are present before the first vowel are removed and placed at the end of the word.
CODE:
importjava.util.Scanner;
System.out.println("ENTER A WORD");
String x=in.nextLine();
x=x.toUpperCase();
charch=' ';
String a,b;
for(inti=0;i<x.length();i++)
ch=x.charAt(i+1);
a=x.substring(0,i+1);
b=x.substring(i+1,x.length());
x=b+a+"AY";
break;
46
}
System.out.println(x);
OUTPUT:
Variable Description:
PROGRAM 16
VARIABLE DESCRIPTION
String x it stores the entered word
char ch stores the respective letter
String a it stores letters until the first consonant
String b it stores the letters other than that of the first consonant
47
Program 17: Accepting 10 numbers and arranging
only the even numbers in ascending order
The program accepts an array of 10 numbers. It arranges only the even numbers in ascending number and
prints only the even numbers.
CODE:
import java.util.*;
class P17_EvenBubbleSort
System.out.println("Enter 10 numbers and the even ones will be sorted in ascending order.");
for(I=0;I<10;I++)
Array[I]=in.nextInt();
for(I=0;I<10;I++)
if(Array[I]%2==0)
EC=EC+1;
}
48
System.out.println();
for(I=0;I<Array.length;I++)
for(J=1;J<Array.length;J++)
if(Array[J]%2==0)
Temp = Array[J-1];
Array[J-1] = Array[J];
Array[J] = Temp;
if(Array[J-1]> Array[J])
if(Array[J]%2==0)
Temp = Array[J-1];
Array[J-1] = Array[J];
Array[J] = Temp;
for(I=0;I<EC;I++)
49
{
if(I+1 != EC)
System.out.print(Array[I]+", ");
else
System.out.print("and "+Array[I]+".");
OUTPUT:
50
VARIABLE DESCRIPTION:
Variable Name Data Type Purpose
Array int array Stores ten numbers
Control variable, runs the outer
I int
for-loop
Control variable, runs the inner
J int
for-loop
Store the number of even
EC int numbers and prints only that
amount of numbers
Helps in exchanging the position
Temp int
of two variables
CODE:
importjava.util.Scanner;
System.out.println("ENTER A SENTENCE");
String x =in.nextLine();
inti,j=0,k,words=0;
String temp="";
for(i=0;i<x.length()-1;i++)
words++;
for(i=0;i<x.length()-1;i++)
k=i;
do
{
52
k++;
}while(x.charAt(k+1)!=' ');
str[j]=x.substring(i+1,k+1);
j++;
for(i=str.length-1;i>=0;i--)
for(j=0;j<i;j++)
if(str[j].compareTo(str[j+1])>0)
temp=str[j];
str[j]=str[j+1];
str[j+1]=temp;
for(i=0;i<str.length;i++)
System.out.println(str[i]);
53
WORKING OF THE PROGRAM:
The program accepts a word from the user. It then enters a for-loop to count the number of words in the
sentence. A String array to store all the words is then created. Next, another for-loop takes each word
separately and stores each word in the array. Then, using Bubble Sort, each word is sorted in alphabetical
order. All words are converted to upper case before doing so. This is because any word that begins with a
capital letter will be arranged before all other words irrespective of the letter (for example, “Why” will be
arranged before “do” as the ASCII code of capital letters are lower than that of lower case letters) . After
all the words have been converted to upper case and arranged in alphabetical order, the words are printed
in alphabetical order.
OUTPUT:
Variable Description:
PROGRAM 18
VARIABLE DESCRIPTION
String x stores the entered words
int i it is a control variable
int j it is a control variabele
int k it is used toreach the position of last letter of the word
54
Program 19: Arranging the contents of a
given String array in alphabetical order
A String array is given with the names of 5 cities – Delhi, Bangalore, Agra, Mumbai and Calcutta. The
contents of this array are then arranged in alphabetical order and displayed.
CODE:
import java.util.*;
class P19_AlphabetizingCity
String Temp;
int I,J;
for(I=0;I<CityNames.length;I++)
if(I>=0&&I<4)
System.out.print(CityNames[I]+", ");
else
System.out.print("and "+CityNames[I]+".");
55
System.out.println();
System.out.println();
for(I=0;I<CityNames.length;I++)
for(J=0;J<CityNames.length-1;J++)
if(CityNames[J].compareTo(CityNames[J+1])>=0)
Temp=CityNames[J];
CityNames[J]=CityNames[J+1];
CityNames[J+1]=Temp;
System.out.println();
for(I=0;I<CityNames.length;I++)
if(I>=0&&I<4)
System.out.print(CityNames[I]+", ");
else
System.out.print("and "+CityNames[I]+".");
56
}
OUTPUT:
VARIABLE DESCRIPTION:
Variable Name Data Type Purpose
To store the name of the cities to
CityNames String array
be sorted
I int Control variable
J int Control variable
Helps in exchanging the positions
Temp String
of two variables
57
Program 20: Printing various patterns on the screen
The program has two methods. The first method prints the following pattern on the screen:
* *
* *
* *
* *
543212345
4321234
32123
212
CODE:
public class twenty
inti,j;
for(i=6;i>0;i--)
for(j=6;j>i;j--)
System.out.print(" ");
58
for(j=i;j>0;j--)
System.out.print(j);
for(j=2;j<=i;j++)
System.out.print(j);
System.out.println();
inti,j;
for(i=0;i<3;i++)
for(j=0;j<i;j++)
System.out.print(" ");
System.out.print("*");
for(j=i+2;j<5-i;j++)
System.out.print(" ");
if(j+1>=5-i)
59
{
System.out.print("*");
break;
System.out.println();
for(i=2;i>0;i--)
for(j=1;j<i;j++)
System.out.print(" ");
System.out.print("*");
for(j=i;j<5-i;j++)
System.out.print(" ");
if(j+1>=5-i)
System.out.print("*");
break;
System.out.println();
60
}
In the second method, there is one for-loop with three for-loops nested within it. The first for-loop prints
spaces as an inverted pyramid is to be achieved. The pyramid is then divided into two parts – each being
printed by a separate for-loop. In the end, the result is achieved and an inverted pyramid is displayed on
the screen.
OUTPUT:
Variable Description:
PROGRAM 20
VARIABLE DESCRIPTION
61
TEACHER’S REMARKS
1. Examiner I:
2. Examiner II:
3. Total Marks:
Principal’s Signature
62
63
64