0% found this document useful (0 votes)
16 views

10th Record Programs

Uploaded by

muhsinmuhammed34
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

10th Record Programs

Uploaded by

muhsinmuhammed34
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

X RECORD

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.

(A number is said to be Niven number if it is divisible by the sum of digits)

Sample input: 126

Sum of digits: 1 + 2 + 6 = 9 and 126 is divisible by 9

// Program to check whether a number is a Niven number or not

import java.util.*;

public class Niven

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

System.out.println("Not a niven number");

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

Sample output: it is a perfect number

The factors of 6 = 1, 2, 3 and 1 + 2 + 3 = 6

// Program to check whether a number is perfect number or not

import java.util.*;

public class Perfect

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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)

System.out.println(a+" is a perfect number");

else

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

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)

Sample input: 153

Sample output: 153 is an Armstrong number

Because, 13 + 53 + 33 = 153

// Program to check whether a number is Armstrong number or not

import java.util.*;

public class Armstrong

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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)

System.out.println("The number "+num+" is an Armstrong number");

else

System.out.println("The number "+num+" is not an Armstrong number");

Page 6 of 55
}

Page 7 of 55
OUTPUT 3
Enter a number:

153

The number 153 is an Armstrong number

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

// Program to display Floyd's triangle

public class Floyd

public static void main(String args[])

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:

1. The number of lower-case characters

2. The number of upper-case characters

3. The number of digits present in the string

4. The number of special characters in the string

Sample input: S.T.D code of New Delhi – 011

Sample output:

The number of lower-case characters = 12

The number of upper-case characters = 5

The number of digits present in the string = 3

The number of special characters = 9

// Program to accept a string and displays the number of lowercase, uppercase, digits and special character

import java.util.*;

public class Special

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int a,p,uc=0,lc=0,d=0,spl=0;

String st;

char ch;

System.out.println("Enter the String:");

st=sc.nextLine();

p=st.length();

for(a=0;a<p;a++)

Page 11 of 55
ch=st.charAt(a);

if(ch>='a' && ch<='z')

lc=lc+1;

else if(ch>='A' && ch<='Z')

uc=uc+1;

else if(ch>='0' && ch<='9')

d=d+1;

else

spl=spl+1;

System.out.println("The number of lowercase characrters:"+lc);

System.out.println("The number of uppercase charcters:"+uc);

System.out.println("The number of digits present in the string:"+d);

System.out.println("The number of special characters:"+spl);

Page 12 of 55
OUTPUT 5
Enter the String:

My email ID is example123@gmail.com

The number of lowercase characrters:23

The number of uppercase charcters:3

The number of digits present in the string:3

The number of special characters:6

Page 13 of 55
PROGRAM 6
Aim
Write a program to accept a word and displays the same in Pig Latin form.

Sample input: trouble

Sample output: oubletray

// Program to accept a word and display the same in Piglatin form

public class Piglatin

public static void main(String str)

int x,y;

String str1,str2;

char b;

x=str.length();

System.out.println("The Piglatin form of the given string is:");

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”

The Piglatin form of the given string is:

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.

S = (1*2) + (2*3) + (3*4) + ………… + (n*(n+1))

// Program to find the sum of the given series

import java.io.*;

public class Series

int sum(int a)

int i,s=0;

for(i=1;i<=a;i++)

s=s+(i*(i+1));

return s;

public static void main(String args[]) throws IOException

int n,p;

InputStreamReader read = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(read);

System.out.println("Enter the value of n:");

n=Integer.parseInt(in.readLine());

Series ob = new Series();

p=ob.sum(n);

System.out.println("The sum of the series is:"+p);

Page 16 of 55
OUTPUT 7
Enter the value of n:

The sum of the series is:70

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.*;

public class Ascii

public static void main(String args[])

Scanner sc = new Scanner(System.in);

char c;

int i,s=0;

System.out.println("Enter any 10 characters:");

for(i=1;i<=10;i++)

c=sc.next().charAt(0);

s=s+(int)c;

System.out.println("Sum of the ASCII codes:"+s);

Page 18 of 55
OUTPUT 8
Enter any 10 characters:

Sum of the ASCII codes:1031

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.

Units consumed Charges


Up to 100 units Rs. 5.50/unit
For next 200 units Rs. 6.50/unit
For next 300 units Rs. 7.50/unit
More than 600 units Rs. 8.50/unit

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

Consumer number : _______________

Consumer’s name : _______________

Units consumed : _______________

Amount to be paid : _______________

// Program to find the electricity bill

import java.util.*;

public class Electricity_bill

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int n,u;

String name;

double amt=0,total=0;

System.out.println("Enter consumer's name:");

name=sc.nextLine();

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

n=sc.nextInt();

Page 20 of 55
System.out.println("Enter unit consumed:");

u=sc.nextInt();

if(u<=100)

amt=u*5.50;

if((u>100) && (u<=300))

amt=100*5.50+(u-100)*6.50;

if((u>300) && (u<=600))

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

Enter consumer number:

435112

Enter unit consumed:

654

-------Money Receipt---------

Consumer number:435112

Consumers name:Abhijeet Mandal

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.

// program to find the sum of the series

import java.util.*;

public class Sum

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int x,n,fact,c=1,i,j;

double sum=0.0;

System.out.println("Enter the value for n:");

n=sc.nextInt();

System.out.println("Enter the value for x:");

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++;

System.out.println("Sum of the series="+sum);

Page 24 of 55
OUTPUT 10
Enter the value for n:

10

Enter the value for x:

Sum of the series=0.08963018077601692

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:

Enter a number to be searched: 42

The number is present at position 9

// Program to search whether a number is in the array list or not

import java.util.*;

public class Search

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int i,k=0,ns;

int m[] = new int[10];

System.out.println("Enter the numbers in the array:");

for(i=0;i<10;i++)

m[i]=sc.nextInt();

System.out.println("Enter the number to be searched:");

ns=sc.nextInt();

for(i=0;i<10;i++)

if(m[i]==ns)

Page 26 of 55
k=1;

break;

if(k==1)

System.out.println("The number is present at position "+(i+1));

else

System.out.println("The number is not present");

Page 27 of 55
OUTPUT 11
Enter the numbers in the array:

12

21

34

62

72

81

98

65

73

68

Enter the number to be searched:

34

The number is present at position 3

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:

1. Number of even numbers

2. Number of odd numbers

3. Number of multiples of 4

// Program to find the number of different category of leements in a Array

import java.util.*;

public class Number

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int i,j,c1=0,c2=0,c3=0,n;

int a[]=new int[10];

System.out.println("Enter the Number of array elements:");

n=sc.nextInt();

System.out.println("Enter the array elements:");

for(i=0;i<n;i++)

a[i]=sc.nextInt();

System.out.println("The array elements are:");

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++;

System.out.println("\nNumber of even numbers:"+c1);

System.out.println("Number of odd numbers:"+c2);

System.out.println("Number of multiples of 4:"+c3);

Page 30 of 55
OUTPUT 12
Enter the Number of array elements:

10

Enter the array elements:

10

The array elements are:

1 2 3 4 5 6 7 8 9 10

Number of even numbers:5

Number of odd numbers:5

Number of multiples of 4:2

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

// Program to find the greatest and smallest number in an array

import java.util.*;

public class Max_min

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int a[]=new int[10],i,n,min,max;

System.out.println("Enter the number of elements in an array:");

n=sc.nextInt();

System.out.println("Enter the array elements:");

for(i=0;i<n;i++)

a[i]=sc.nextInt();

System.out.println("The array elements are:");

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];

System.out.println("\nThe greatest element in the array is:"+max);

System.out.println("The smallest element in the array is:"+min);

Page 33 of 55
OUTPUT 13
Enter the number of elements in an array:

10

Enter the array elements:

57

21

34

64

41

84

71

72

81

82

The array elements are:

57 21 34 64 41 84 71 72 81 82

The greatest element in the array is:84

The smallest element in the array is:21

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.

// program to check whether a number is prime or not

import java.util.*;

public class Prime

public int check(int n)

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;

public static void main(String args[])

int num,res;

Scanner sc = new Scanner(System.in);

Prime ob=new Prime();

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

num=sc.nextInt();

res=ob.check(num);

Page 35 of 55
if(res==1)

System.out.println(num+" is a prime number");

else

System.out.println(num+" is not a prime number");

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.

// Program to find the sum of row elements in a 2D array

import java.util.*;

public class Row_col_sum

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int i,j,r,c,m,n;

int a[][]=new int[4][4];

System.out.println("Enter the number of rows:");

m=sc.nextInt();

System.out.println("Enter the number of columns:");

n=sc.nextInt();

System.out.println("Enter the elements of the Matrix:");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

a[i][j]=sc.nextInt();

System.out.print("The elements of the matrix are:");

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];

System.out.println("The sum of elements of row "+(i+1)+":"+r);

System.out.println("\nThe sum of elements of each column:");

for(i=0;i<m;i++)

c=0;

for(j=0;j<n;j++)

c=c+a[j][i];

System.out.println("The sum of elements of column "+(i+1)+":"+c);

Page 39 of 55
OUTPUT 15
Enter the number of rows:

Enter the number of columns:

Enter the elements of the Matrix:

21

54

72

32

54

97

38

51

57

The elements of the matrix are:

21 54 72

32 54 97

38 51 57

The sum of elements of each row:

The sum of elements of row 1:147

The sum of elements of row 2:183

The sum of elements of row 3:146

The sum of elements of each column:

The sum of elements of column 1:91

The sum of elements of column 2:159

The sum of elements of column 3:226

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.

// Program to find the sum of Diagonal elements in a 2D array

import java.util.*;

public class Diag_sum

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int i,j,n,k,ld=0,rd=0;

int a[][]=new int[4][4];

System.out.println("Enter the number of rows and columns:");

n=sc.nextInt();

System.out.println("Enter the elements of the Matrix:");

for(i=0;i<n;i++)

for(j=0;j<n;j++)

a[i][j]=sc.nextInt();

System.out.print("The elements of the matrix are:");

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;

System.out.println("The sum of right diagonal elements:"+rd);

Page 42 of 55
OUTPUT 16
Enter the number of rows and columns:

Enter the elements of the Matrix:

23

51

87

83

65

39

61

79

57

The elements of the matrix are:

23 51 87

83 65 39

61 79 57

The sum of left diagonal elements:145

The sum of right diagonal elements:213

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.

// Program to check whether a number is palindrome or not

import java.util.*;

public class Palindrome

public int reverse(int n)

int rev=0,r;

do

r=n%10;

rev=rev*10+r;

n=n/10;

}while(n!=0);

return rev;

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int num,rev_no;

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

num=sc.nextInt();

Palindrome ob=new Palindrome();

rev_no=ob.reverse(num);

if(rev_no==num)

Page 44 of 55
System.out.println(num+" is a palindrome number");

else

System.out.println(num+" is not a palindrome number");

Page 45 of 55
OUTPUT 17
Enter the number:

313

313 is a palindrome number

Page 46 of 55
PROGRAM 18
Aim
Write a program with the following specifications:

Class name : Rectangle

Data Members / Instance variables:

int length : to store the length of the rectangle

int breadth : to store the breadth of the rectangle

Member Functions:

void inputdata() : to accept the length and breadth of the rectangle

void calculate() : to find the area, perimeter and diagonal of the rectangle

void outputdata() : to display area, perimeter and diagonal of the rectangle

// Program to calculate area, perimeter and diagonal of a rectangle

import java.io.*;

public class Rectangle

int m,n,a,p;

double d;

public void inputdata() throws IOException

InputStreamReader read = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(read);

System.out.println("Enter the length of rectangle:");

m=Integer.parseInt(in.readLine());

System.out.println("Enter the breadth of rectangle:");

n=Integer.parseInt(in.readLine());

public void calculate()

Page 47 of 55
a=m*n;

p=2*(m+n);

d=Math.sqrt(m*m+n*n);

public void outputdata()

System.out.println("The area of the rectangle is:"+a);

System.out.println("The perimeter of the rectangle is:"+p);

System.out.println("The diagonal of the rectangle is:"+d);

public static void main(String args[]) throws IOException

Rectangle ob = new Rectangle();

ob.inputdata();

ob.calculate();

ob.outputdata();

Page 48 of 55
OUTPUT 18
Enter the length of rectangle:

Enter the breadth of rectangle:

The area of the rectangle is:15

The perimeter of the rectangle is:16

The diagonal of the rectangle is:5.830951894845301

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:

Volume of a cube (vc) :s*s*s


4
Volume of a sphere (vs) :3*π*r*r*r

Volume of a cuboid (vcd) :l*b*h

// Program to find the volume of a cube, a sphere and a cuboid using overloading

import java.util.*;

public class Compute

double vc,vs,vcd;

void volume(int s)

vc=s*s*s;

System.out.println("The volume of a cube="+vc);

void volume(float r)

vs=(4/3)*(22/7)*r*r*r;

System.out.println("The volume of a sphere="+vs);

void volume(int l,int b,int h)

vcd=l*b*h;

System.out.println("The volume of a cuboid="+vcd);

public static void main(String args[])

Page 50 of 55
{

Scanner sc = new Scanner(System.in);

int s,l,b,h;

float r;

System.out.println("Enter the value of side of a cube, radius of sphere, sides of a cuboid:");

s=sc.nextInt();

r=sc.nextInt();

l=sc.nextInt();

b=sc.nextInt();

h=sc.nextInt();

Compute ob = new Compute();

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:

The volume of a cube=64.0

The volume of a sphere=648.0

The volume of a cuboid=210.0

Page 52 of 55
PROGRAM 20
Aim
Write a program by using a class with the following specifications:

Class name : Profit_loss

Data members / Instance variables:

int cp : to store the cost price

int sp : to store the selling price

Member Functions:

Profit_loss : default constructor to initialize cp, sp

void input(int m, int n) : to assign cp with m and sp with n

void display() : to calculate and display either profit percent (pp) or loss percent (lp)

// Program to calculate and display either profit or loss percent

public class Profit_loss

int cp,sp,pr,loss;

double pp,lp;

Profit_loss()

cp=0;

sp=0;

void input(int m,int n)

cp=m;

sp=n;

void display()

Page 53 of 55
if(sp>cp)

pr=sp-cp;

pp=(double)pr/cp*100;

System.out.println("The profit percent:"+pp);

if(cp>sp)

loss=cp-sp;

lp=(double)loss/cp*100;

System.out.println("The loss percent:"+lp);

Page 54 of 55
OUTPUT 20
int m = 50, int n = 60

The profit percent:20.0

Page 55 of 55

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy