10th Record Programs
10th Record Programs
TH
PROGRAMS
Page 1 of 55
PROGRAM 1
Aim
Write a program to input a number. Check and display whether it is a Niven number or not.
import java.util.*;
int num,sum=0,d,p;
System.out.println("Enter a number:");
num=sc.nextInt();
p=num;
do
d=num%10;
sum=sum+d;
num=num/10;
}while(num!=0);
if(p%sum==0)
System.out.println("Niven number");
else
Page 2 of 55
OUTPUT 1
Enter a number:
126
Niven number
Page 3 of 55
PROGRAM 2
Aim
Write a program to accept a number and check whether the number is perfect or not.
(A number is said to be perfect if the sum of the factors (including 1 and excluding the number itself) is the
same as the original number)
Sample input: 6
import java.util.*;
int a,n,s=0;
System.out.println("Enter a number:");
n=sc.nextInt();
for(a=1;a<n;a++)
if(n%a==0)
s=s+a;
if(s==n)
else
Page 4 of 55
OUTPUT 2
Enter a number:
6 is a perfect number
Page 5 of 55
PROGRAM 3
Aim
Write a program to enter a number and check whether the number is an Armstrong number or not.
(A number is said to be Armstrong, if the sum of the cubes of its digits is equal to the original number)
Because, 13 + 53 + 33 = 153
import java.util.*;
int n,a,num,s=0;
System.out.println("Enter a number:");
n=sc.nextInt();
num=n;
while(n>0)
a=n%10;
s=s+a*a*a;
n=n/10;
if(num==s)
else
Page 6 of 55
}
Page 7 of 55
OUTPUT 3
Enter a number:
153
Page 8 of 55
PROGRAM 4
Aim
Write a program to display the Floyd’s Triangle.
2 3
4 5 6
7 8 9 10
11 12 13 14 15
int a,b,p=0;
for(a=1;a<=5;a++)
for(b=1;b<=a;b++)
p=p+1;
System.out.print(p+"\t");
System.out.println();
Page 9 of 55
OUTPUT 4
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Page 10 of 55
PROGRAM 5
Aim
Write a program to accept a string and displays:
Sample output:
// Program to accept a string and displays the number of lowercase, uppercase, digits and special character
import java.util.*;
int a,p,uc=0,lc=0,d=0,spl=0;
String st;
char ch;
st=sc.nextLine();
p=st.length();
for(a=0;a<p;a++)
Page 11 of 55
ch=st.charAt(a);
lc=lc+1;
uc=uc+1;
d=d+1;
else
spl=spl+1;
Page 12 of 55
OUTPUT 5
Enter the String:
My email ID is example123@gmail.com
Page 13 of 55
PROGRAM 6
Aim
Write a program to accept a word and displays the same in Pig Latin form.
int x,y;
String str1,str2;
char b;
x=str.length();
for(y=0;y<x;y++)
b=str.charAt(y);
if(b=='a' || b=='e' || b=='i' || b=='o' || b=='u' || b=='A' || b=='E' || b=='I' || b=='O' || b=='U' )
break;
str1=str.substring(y,x);
str2=str.substring(0,y);
System.out.println(str1+str2+"ay");
Page 14 of 55
OUTPUT 6
String str = “trouble”
oubletray
Page 15 of 55
PROGRAM 7
Aim
Write a program to find the sum of the given series to n terms by using the function name sum(int). write
the main program to display the sum of the series.
import java.io.*;
int sum(int a)
int i,s=0;
for(i=1;i<=a;i++)
s=s+(i*(i+1));
return s;
int n,p;
n=Integer.parseInt(in.readLine());
p=ob.sum(n);
Page 16 of 55
OUTPUT 7
Enter the value of n:
Page 17 of 55
PROGRAM 8
Aim
Write a program to accept a set of any 10 characters. Calculate and print the sum of ASCII codes of the
characters.
// Program to accept characters and print the sum of ASCII codes of the characters
import java.util.*;
char c;
int i,s=0;
for(i=1;i<=10;i++)
c=sc.next().charAt(0);
s=s+(int)c;
Page 18 of 55
OUTPUT 8
Enter any 10 characters:
Page 19 of 55
PROGRAM 9
The electricity board charges from their consumers according to the units consumed per month. The
amount is calculated as per the tariff given below.
Aim
Write a program to input consumer’s name, consumer number and the units consumed. The program
displays the following information after calculating the amount.
Money Receipt
import java.util.*;
int n,u;
String name;
double amt=0,total=0;
name=sc.nextLine();
n=sc.nextInt();
Page 20 of 55
System.out.println("Enter unit consumed:");
u=sc.nextInt();
if(u<=100)
amt=u*5.50;
amt=100*5.50+(u-100)*6.50;
amt=100*5.50+200*6.50+(u-300)*7.50;
if(u>600)
amt=100*5.50+200*6.50+300*7.50+(u-600)*8.50;
System.out.println("-------Money Receipt---------");
System.out.println("Consumer number:"+n);
System.out.println("Consumers name:"+name);
System.out.println("Units consumed:"+u);
System.out.println("Amount to be paid:Rs."+amt);
Page 21 of 55
OUTPUT 9
Enter consumer's name:
Abhijeet Mandal
435112
654
-------Money Receipt---------
Consumer number:435112
Units consumed:654
Amount to be paid:Rs.4559.0
Page 22 of 55
PROGRAM 10
Aim
Write a java program to display the sum of series,
𝑥^3 𝑥^5 𝑥^7 𝑥^𝑛
x=x- + - + …………………
3! 5! 7! 𝑛!
Where the user has to input the value of x and n. The value of n should be odd number.
import java.util.*;
int x,n,fact,c=1,i,j;
double sum=0.0;
n=sc.nextInt();
x=sc.nextInt();
for(i=1;i<=n;i=i+2)
fact=1;
for(j=1;j<=i;j++)
fact=fact*j;
if(c%2==0)
sum=sum-(Math.pow(x,i)/fact);
else
Page 23 of 55
sum=sum+(Math.pow(x,i)/fact);
c++;
Page 24 of 55
OUTPUT 10
Enter the value for n:
10
Page 25 of 55
PROGRAM 11
Aim
Write a program to accept 10 different numbers in a single dimensional array. Now enter a number and
search whether the number is present or not in the list of array elements by using the ‘linear search’
technique.
Sample Input:
m[0] m[1] m[2] m[3] m[4] m[5] m[6] m[7] m[8] m[9]
14 56 62 77 46 67 59 80 42 26
Sample Output:
import java.util.*;
int i,k=0,ns;
for(i=0;i<10;i++)
m[i]=sc.nextInt();
ns=sc.nextInt();
for(i=0;i<10;i++)
if(m[i]==ns)
Page 26 of 55
k=1;
break;
if(k==1)
else
Page 27 of 55
OUTPUT 11
Enter the numbers in the array:
12
21
34
62
72
81
98
65
73
68
34
Page 28 of 55
PROGRAM 12
Aim
Write a program to accept 10 integer number in a single dimensional array. Find and display the following:
3. Number of multiples of 4
import java.util.*;
int i,j,c1=0,c2=0,c3=0,n;
n=sc.nextInt();
for(i=0;i<n;i++)
a[i]=sc.nextInt();
for(i=0;i<n;i++)
System.out.print(a[i]+"\t");
for(i=0;i<n;i++)
if(a[i]%2==0)
c1++;
if(a[i]%2!=0)
c2++;
Page 29 of 55
if(a[i]%4==0)
c3++;
Page 30 of 55
OUTPUT 12
Enter the Number of array elements:
10
10
1 2 3 4 5 6 7 8 9 10
Page 31 of 55
PROGRAM 13
Aim
Write a program to accept 10 different numbers in a single dimensional array. Display the greatest and the
smallest numbers of the array elements
Sample Input:
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
43 45 92 87 64 76 81 65 12 31
Sample Output:
Greatest element: 92
Smallest element: 12
import java.util.*;
n=sc.nextInt();
for(i=0;i<n;i++)
a[i]=sc.nextInt();
for(i=0;i<n;i++)
System.out.print(a[i]+"\t");
max=a[0];
min=a[0];
for(i=0;i<n;i++)
Page 32 of 55
{
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
Page 33 of 55
OUTPUT 13
Enter the number of elements in an array:
10
57
21
34
64
41
84
71
72
81
82
57 21 34 64 41 84 71 72 81 82
Page 34 of 55
PROGRAM 14
Aim
Write a program to accept a number and check whether a number is prime or not. Use the function name
as check(int n). The function returns 1, if the number is prime otherwise it returns 0.
import java.util.*;
int i,c=0;
for(i=1;i<=n;i++)
if(n%i==0)
c=c+1;
if(c==2)
return 1;
else
return 0;
int num,res;
num=sc.nextInt();
res=ob.check(num);
Page 35 of 55
if(res==1)
else
Page 36 of 55
OUTPUT 14
Enter the number:
7 is a prime number
Page 37 of 55
PROGRAM 15
Aim
Write a program to store the numbers in a 3*3 matrix in a Double Dimensional array. Find the sum of the
numbers of each row and the sum of the numbers of each column of the matrix by using an input
statement.
import java.util.*;
int i,j,r,c,m,n;
m=sc.nextInt();
n=sc.nextInt();
for(i=0;i<m;i++)
for(j=0;j<n;j++)
a[i][j]=sc.nextInt();
for(i=0;i<m;i++)
System.out.println();
for(j=0;j<n;j++)
System.out.print(a[i][j]+"\t");
Page 38 of 55
System.out.println("\n\nThe sum of elements of each row:");
for(i=0;i<m;i++)
r=0;
for(j=0;j<n;j++)
r=r+a[i][j];
for(i=0;i<m;i++)
c=0;
for(j=0;j<n;j++)
c=c+a[j][i];
Page 39 of 55
OUTPUT 15
Enter the number of rows:
21
54
72
32
54
97
38
51
57
21 54 72
32 54 97
38 51 57
Page 40 of 55
PROGRAM 16
Aim
Write a program to store the numbers in 3*3 matrix in a Double Dimensional array. Find the sum of the
numbers of the left diagonal and the sum of the numbers of the right diagonal of the matrix by using an
input statement.
import java.util.*;
int i,j,n,k,ld=0,rd=0;
n=sc.nextInt();
for(i=0;i<n;i++)
for(j=0;j<n;j++)
a[i][j]=sc.nextInt();
for(i=0;i<n;i++)
System.out.println();
for(j=0;j<n;j++)
System.out.print(a[i][j]+"\t");
for(i=0;i<n;i++)
ld=ld+a[i][i];
Page 41 of 55
System.out.println("\nThe sum of left diagonal elements:"+ld);
k=n-1;
for(i=0;i<n;i++)
rd=rd+a[i][k];
k=k-1;
Page 42 of 55
OUTPUT 16
Enter the number of rows and columns:
23
51
87
83
65
39
61
79
57
23 51 87
83 65 39
61 79 57
Page 43 of 55
PROGRAM 17
Aim
Write a program to accept a number and check whether the number is palindrome or not by using the
function name reverse(int n), the function returns the reversed number to the main program that checks
palindrome number.
import java.util.*;
int rev=0,r;
do
r=n%10;
rev=rev*10+r;
n=n/10;
}while(n!=0);
return rev;
int num,rev_no;
num=sc.nextInt();
rev_no=ob.reverse(num);
if(rev_no==num)
Page 44 of 55
System.out.println(num+" is a palindrome number");
else
Page 45 of 55
OUTPUT 17
Enter the number:
313
Page 46 of 55
PROGRAM 18
Aim
Write a program with the following specifications:
Member Functions:
void calculate() : to find the area, perimeter and diagonal of the rectangle
import java.io.*;
int m,n,a,p;
double d;
m=Integer.parseInt(in.readLine());
n=Integer.parseInt(in.readLine());
Page 47 of 55
a=m*n;
p=2*(m+n);
d=Math.sqrt(m*m+n*n);
ob.inputdata();
ob.calculate();
ob.outputdata();
Page 48 of 55
OUTPUT 18
Enter the length of rectangle:
Page 49 of 55
PROGRAM 19
Aim
Write a class with the name volume using function overloading that computes the volume of a cube, a
sphere and a cuboid.
Formula:
// Program to find the volume of a cube, a sphere and a cuboid using overloading
import java.util.*;
double vc,vs,vcd;
void volume(int s)
vc=s*s*s;
void volume(float r)
vs=(4/3)*(22/7)*r*r*r;
vcd=l*b*h;
Page 50 of 55
{
int s,l,b,h;
float r;
s=sc.nextInt();
r=sc.nextInt();
l=sc.nextInt();
b=sc.nextInt();
h=sc.nextInt();
ob.volume(s);
ob.volume(r);
ob.volume(l,b,h);
Page 51 of 55
OUTPUT 19
Enter the value of side of a cube, radius of sphere, sides of a cuboid:
Page 52 of 55
PROGRAM 20
Aim
Write a program by using a class with the following specifications:
Member Functions:
void display() : to calculate and display either profit percent (pp) or loss percent (lp)
int cp,sp,pr,loss;
double pp,lp;
Profit_loss()
cp=0;
sp=0;
cp=m;
sp=n;
void display()
Page 53 of 55
if(sp>cp)
pr=sp-cp;
pp=(double)pr/cp*100;
if(cp>sp)
loss=cp-sp;
lp=(double)loss/cp*100;
Page 54 of 55
OUTPUT 20
int m = 50, int n = 60
Page 55 of 55