Comp Programs
Comp Programs
AIM:
To calculate the gross salary and net salary of an employee
ALGORITHM:
[double basic: Basic salary from the user
double da: Dearness allowance
double hra: House rent allowance
double pf: Provident fund
double epf: Employee provident fund
double cta:
double gross: Gross salary
double net: Net salary]
STEPS DESCRIPTION
1. Start the program.
2. Import the package java.util.*
3. Name the class GrossNetSalary
4. Declare the variables for the program.
5. Receive the user input in the input method and initialize
the input variable.
6. Calculate the da, hra, pf, epf, cta, gross salary and net
salary using suitable percentages and formulae.
7. Display the gross salary and net salary using a display
method.
8. Create an object and call the methods inside it.
9. Compile the program.
10. End the program.
PROGRAM:
//To calculate the gross salary and net salary of an employee
import java.util.*;
class GrossNetSalary
{
//Instance variables
double basic,da,hra,pf,epf,cta,gross,net; //Declaration of
variables
//Input method
void basic()
{
Scanner jk=new Scanner(System.in);
System.out.println("Enter basic salary"); //user input
basic=jk.nextDouble(); //initialization
}
//Logic method
void calc()
{
//Calculation
da=basic*(0.4);
hra=basic*(0.12);
pf=basic*(0.0833);
epf=basic*(0.0167);
cta=basic*(0.08);
gross=basic+da+hra+cta;
net=gross-(pf+epf);
}
//Display method
void net()
{
System.out.println("Gross salary:Rs."+gross); //To display
gross salary
System.out.println("Net salary:Rs."+net); //To display net
salary
}
public static void main(String args[])
{
GrossNetSalary txt=new GrossNetSalary(); //object
creation
//calling the methods
txt.basic();
txt.calc();
txt.net();
}
}
SAMPLE INPUT:
Enter basic salary
50000
SAMPLE OUTPUT:
Gross salary:Rs.80000.0
Net salary:Rs.75000.0
PROGRAM 2: BONUS
AIM:
To display the annual bonus for a company's employers
ALGORITHM:
[double basic: Basic salary from user
double bonus, bonus1: To store bonus value
double per: Percentage of bonus from basic salary
char ch, A, B, C: To choose category- Officers or Supervisors or
Workers]
STEPS DESCRIPTION
1. Start the program.
2. Import the package java.util.*
3. Name the class Bonus.
4. Declare the variables for the program.
5. Receive the category which the user is going to choose
and initialize the variable.
6. Use switch cases to separate into Officers, Supervisors
and Workers.
7. In each case, Get the user input for basic salary,
initialize the input variable and calculate the bonus
using suitable percentages. For Officers, if the bonus
exceeds Rs.50000, let the bonus be Rs.50000. For
Supervisors, if the bonus exceeds Rs.40000, let the
bonus be Rs.40000. For Workers, if the bonus exceeds
Rs.30000, let the bonus be Rs.30000.
8. Print the bonus value.
9. Compile the program.
10. End the program.
PROGRAM:
//To display the annual bonus for a company's employers
import java.util.*;
class Bonus
{
public static void main(String args[])
{
//Declaration of variables
double basic,bonus,bonus1,per;
char ch,A,B,C;
Scanner rm=new Scanner(System.in);
System.out.println("Enter A for Officers, Enter B for
Supervisors and Enter C for Workers"); //category
System.out.println("Enter your choice"); //user input for
category
ch=rm.next().charAt(0); //initialization
switch(ch)
{
case 'A':
System.out.println("For Officers:");
System.out.println("Enter basic value"); //user input for
basic salary
basic=rm.nextDouble(); //initialization
per=12; //percentage of bonus
bonus=(per/100)*basic; //calculation
if(bonus>50000) //if the bonus exceeds Rs.50000
bonus1=50000;
else
bonus1=bonus;
System.out.println("Bonus:Rs."+bonus1); //printing the
bonus
break;
case 'B':
System.out.println("For Supervisors");
System.out.println("Enter basic value"); //user input for
basic salary
basic=rm.nextDouble(); //initialization
per=10; //percentage of bonus
bonus=(per/100)*basic; //calculation
if(bonus>40000) //if the bonus exceeds Rs.40000
bonus1=40000;
else
bonus1=bonus;
System.out.println("Bonus:Rs."+bonus1); //printing the
bonus
break;
case 'C':
System.out.println("For Workers");
System.out.println("Enter basic value"); //user input for
basic salary
basic=rm.nextDouble(); //initialization
per=8.33; //percentage of bonus
bonus=(per/100)*basic; //calculation
if(bonus>30000) //if the bonus exceeds Rs.30000
bonus1=30000;
else
bonus1=bonus;
System.out.println("Bonus:Rs."+bonus1); //printing the
bonus
break;
default:
System.out.println("Wrong choice!!!");
}
}
}
SAMPLE INPUT:
Enter A for Officers, Enter B for Supervisors and Enter C for
Workers
Enter your choice
A
For Officers:
Enter basic value
500000
SAMPLE OUTPUT:
Bonus:Rs.50000.0
PROGRAM 3: PRIME PERFECT
AIM:
To check and display whether the given number is prime
perfect or not
ALGORITHM:
[int n: Number from user
int s: Sum of factors of the number
int c: Number of factors of the factor of the number given by the
user
int m: Variable to store the original number
int i: Factors of number given by user
int j: Factors of the factor of number given by user]
STEPS DESCRIPTION
1. Start the program
2. Import the package java.util.*
3. Name the class PrimePerfect
4. Declare the variables for the program
5. Get the number from the user and initialize the input
variable.
6. Store the original number in another variable. This is to
check whether the sum of the prime factors of the
number is equal to the original number.
7. Open a for loop to check the factors of the number
given by the user.
8. Open another for loop inside the outer loop to check
the factors of the factor of the user’s number.
9. If the number of factors is 2(i.e., 1 and the number
itself to check whether it is prime) for the factors of the
user’s number, sum up the factors of the original
number.
10. If the original number is equal to the sum of the prime
factors of the number, print the number is prime
perfect, otherwise print it is not prime perfect.
11. Compile the program.
12. End the program.
PROGRAM:
//To check and display whether the given number is prime perfect or
not
import java.util.*;
class PrimePerfect
{
public static void main(String args[])
{
int n,s=1,c=0,m; //Declaration of variables
Scanner kr=new Scanner(System.in);
System.out.println("Enter a number"); //user input
n=kr.nextInt(); //initialization
m=n; //storing original value
for(int i=1;i<n;i++)
{
c=0;
if(n%i==0) //To check the factors of the numbers
{
for(int j=1;j<=i;j++) //To check if the factors are prime
numbers
{
if(i%j==0)
c++;
}
if(c==2) //if the factor is prime
s=s+i;
}
}
if(m==s) //if the original number is equal to the sum of the
prime factors of the number
System.out.println("The number is prime perfect");
else
System.out.println("The number ain't prime perfect");
}
}
SAMPLE INPUT:
Enter a number
6
SAMPLE OUTPUT:
[Factors of 6:1,2,3(all are prime factors); Sum of the
factors=1+2+3=6]
The number is prime perfect
PROGRAM 4: TECH NUMBER
AIM:
To generate and print all the 4 digit tech numbers
ALGORITHM:
[int i: loop variable for 4 digit numbers
int fh: first half of the number, i.e.,the first 2 digits of the number
int sh: second half of the number, i.e., the last 2 digits of the number
int sum: The sum of first 2 and last 2 digits]
STEPS DESCRIPTION
1. Start the program
2. Import the package java.util.*
3. Name the class Tech
4. Declare all the variable for the program
5. Open the for loop for all the 4 digit numbers
6. Divide each number into the first half by dividing by
100 and the second half by getting the remainder
when divided by 100.
7. Sum up the first half and second half and square it.
8. If the square of the sum of the first half and second
half of the number is equal to the original number
itself, all the 4 digit tech numbers will be printed.
9. Compile the program.
10. End the program
PROGRAM:
//To generate and print all the 4 digit tech numbers
import java.util.*;
class Tech
{
public static void main(String args[])
{
int i,fh,sh,sum=0; //Declaration
Scanner kr=new Scanner(System.in);
System.out.println("The Tech numbers are:");
for(i=1000;i<=9999;i++) //4 digit numbers
{
fh=i/100; //first 2 digits of the number
sh=i%100; //last 2 digits of the number
sum=fh+sh; //Summing up the first half and second half
if(i==sum*sum) //if the square of the sum of first half and
second half is equal to original number
System.out.println(i); //printing the original number
}
}
}
OUTPUT:
The Tech numbers are:
2025
3025
9801
PROGRAM 5: PATTERN
AIM:
To display a pattern
ALGORITHM:
[int i, j, m, n: Loop variables]
STEPS DESCRIPTION
1. Start the program
2. Import the package java.util.*
3. Name the class Pattern
4. Declare the loop variables
5. First, for the upper half of the pattern, open a for loop
for the number of rows. Increment the loop variable by
1
6. Open another for loop inside the outer loop for the
numbers that are to be printed.
7. If the inner loop variable is lesser than the outer
variable, print spaces, otherwise, print the numbers
with spaces.
8. Finally for the lower half of the pattern, open a for loop
for the number of rows. This time, decrement the loop
variable by 1.
9. Open another for loop inside the second outer loop for
the numbers to be printed.
10. If the second inner loop variable is lesser than the
second outer loop variable, print spaces, otherwise,
print the numbers with spaces.
11. Compile the program.
12. End the program.
PROGRAM:
//To display a pattern
import java.util.*;
class Pattern
{
public static void main(String args[])
{
int i,j,m,n; //Declaration
System.out.println("The Pattern:");
//For upper half of the pattern
for(i=1;i<=5;i++) //no. of rows
{
for(j=1;j<=5;j++) //for printing the numbers
{
if (j<i )
{
System.out.print(" "); //to print spaces before the
numbers
}
else
{
System.out.print(j+" ");
}
}
System.out.println();
}
//for the lower half of the pattern
for(m=4;m>=1;m--) //no. of rows
{
for(n=1;n<=5;n++) //to print the numbers
{
if (n<m )
{
System.out.print(" "); //to print spaces before the
numbers
}
else
{
System.out.print(n+" ");
}
}
System.out.println();
}
}
}
OUTPUT:
The pattern:
12345
2345
345
45
5
45
345
2345
12345
PROGRAM 6: PIGLATIN
AIM:
To accept a sentence and display each word of the sentence
in piglatin form
ALGORITHM:
[String str: String from the user
String str1: Each word in the string
String str2: Piglatin words
int len: Size of the string
int len1: Size of each word
int p: First letter of each word
int c: To store the character of the first vowel in each word
Int i, j: loop variables
char ch: Each character of the string
char: ch1: Each character of a word]
STEPS DESCRIPTION
1. Start the program
2. Import the package java.util.*
3. Name the class PigLatin
4. Declare the variables for the program
5. Get the user input for a string and initialize the input
variable. Add a space at the last of the string to include
the last word in the string.
6. Find the size of the length of the string.
7. Open a for loop and find each character in the string
8. If the character is a space, find each word in the string
using a substring() function having its first index
number as p and to get the word’s last letter, second
variable in the function as ‘i’ which is the space. To get
the next word have p=i+1.
9. Find the size of the word.
10. Open another for loop for the word and find the
characters of the word.
11. If there is a vowel in the word, take the first occurrence
of the vowel and store it in a variable and give a break
statement so that the value of the index number of the
first occurrence of the vowel does not change.
12. Using a substring() function, join the part of the word
from the vowel to the last letter of the word and the
remaining part of the word along with “ay” at the last.
13. Print the words with space .
14. Compile the program.
15. End the program
PROGRAM:
//To accept a sentence and display each word of the sentence in
piglatin form
import java.util.*;
class PigLatin
{
public static void main(String args[])
{
//Declaration of variables
String str,str1="",str2="";
int len,len1,p=0,c=0;
char ch,ch1;
Scanner kr=new Scanner(System.in);
System.out.println("Enter a string"); //user input
str=kr.nextLine(); //initialization
str=str+" "; //to include the last word
len=str.length(); //size of the string
for(int i=0;i<len;i++)
{
ch=str.charAt(i); //to take each character
if(ch==' ') //to take each word
{
str1=str.substring(p,i); //for a word in the string
p=i+1; //to take the next word
len1=str1.length(); //size of the word
for(int j=0;j<len1;j++)
{
ch1=str1.charAt(j); //each letter of the word
if("AEIOUaeiou".indexOf(ch1)>=0) //checking
whether a letter is a vowel
{
c=j; //storing the index no. of vowel
break;
}
}
str2=str1.substring(c)+str1.substring(0,c)+"ay"; //joining
the last half and first half along with "ay"
System.out.print(str2+" "); //printing the words to make
sentence
}
}
}
}
SAMPLE INPUT:
Enter a string
The capital of India is New Delhi
SAMPLE OUTPUT:
eThay apitalcay ofay Indiaay isay ewNay elhiDay
PROGRAM 7: REVERSING A SENTENCE
WITHOUT PUNCTUATION MARKS
AIM:
To accept a sentence with punctuation marks and display it in
reverse order without punctuation marks
ALGORITHM:
[String str: String from user
String str1: Each word in the string
String str2: Sentence of new string
Int len: Size of the string
Int i: Loop variable
Char ch: Each character of the string]
STEPS DESCRIPTION
1. Start the program.
2. Import the package java.util.*
3. Name the class SpecialReverse
4. Declare the variables for the program
5. Get the input string from the user and initialize the
input variable
6. Add a space in the last of the string to include the last
word
7. Find the size of the string
8. Open a for loop and find each character in the string.
9. If the character is not a space, add the characters to
make a word
10. Otherwise, use a replaceAll() function to remove all the
special characters in the string.
11. To reverse the words, add the each word in front of the
new string str2
12. Reinitialize the string used for each word
13. Print the new sentence
14. Compile the program
15. End the program
PROGRAM:
//To accept a sentence with punctuation marks and display it in
reverse order without punctuation marks
import java.util.*;
class SpecialReverse
{
public static void main(String args[])
{
//Declaration of variables
String str,str1="",str2="";
int len;
char ch;
Scanner kr=new Scanner(System.in);
System.out.println("Enter a string"); //user input
str=kr.nextLine(); //initialization
str=str+" "; //to include the last word
len=str.length(); //size of the string
for(int i=0;i<len;i++)
{
ch=str.charAt(i); //to take each character of the string
if(ch!=' ')
{
str1=str1+ch; //to make a word
}
else
{
str1=str1.replaceAll("['.,;]",""); //to delete all the
punctuation marks
str2=str1+" "+str2; //to make a sentence
str1=""; //reinitializing the word
}
}
System.out.println(str2); //printing the sentence
}
}
SAMPLE INPUT:
Enter a string
The capital of India, New Delhi, is very beautiful.;
SAMPLE OUTPUT:
beautiful very is Delhi New India of capital The
PROGRAM 8: WORDS THAT START WITH A
VOWEL AND END WITH A VOWEL
AIM:
To accept a sentence with blank spaces and display the
number of words beginning and ending with a vowel and place the
words which begin and end with a vowel at the beginning, followed
by the remaining words as they occur in the sentence
ALGORITHM:
[String str: Input from user
String str1: Each word in the string
String vow: Set of words that start and end with a vowel
String cons: Set of remaining words
int len: Size of the string
int vowcou: Count of words that start and end with a vowel
int i: Loop variable
char ch: Characters of the string
char ch1: First letter of each word in the string
char ch2: Last letter of each word in the string]
STEPS DESCRIPTION
1. Start the program
2. Import the package java.util.*
3. Name the class StartEndVowel
4. Declare the variables for the program
5. Get the input string from the user and initialize the
input variable
6. Add a space in the last of the string to include the last
word.
7. Find the size of the string
8. Open a for loop and find each character in the string.
9. If the character is not a space, add the characters to
make a word.
10. Otherwise, find the first letter of the word and the last
letter of the word, and check whether they are vowels.
11. If the first and the last letter of the word are vowels,
increment the number of words that start and end with
a vowel by 1 and make a set of these words by adding
them along with a space. Otherwise, to make a set of
the remaining words, add the words along with a
space.
12. Reinitialize the variable used for each word.
13. Print the number of words that start and end with
vowels and print the sentence with the words that start
and end with vowels at the front and the remaining
words after those words.
14. Compile the program.
15. End the program
PROGRAM:
//To accept a sentence with blank spaces and display the number of
words beginning and ending with a vowel and place the words
which begin and end with a vowel at the beginning, followed by the
remaining words as they occur in the sentence
import java.util.*;
class StartEndVowel
{
public static void main(String args[])
{
//Declaration of variables
String str,str1="",vow="",cons="";
int len,vowcou=0;
char ch,ch1,ch2;
Scanner kr=new Scanner(System.in);
System.out.println("Enter a string"); //user input
str=kr.nextLine(); //initialization
str=str+" "; //to include the last word
len=str.length(); //size of the string
for(int i=0;i<len;i++)
{
ch=str.charAt(i); //to take each character in the string
if(ch!=' ')
{
str1=str1+ch; //to make a word
}
else
{
ch1=str1.charAt(0); //to take the first letter in the word
ch2=str1.charAt(str1.length()-1); //to take the last letter
in the word
if("AEIOUaeiou".indexOf(ch1)>=0 &&
"AEIOUaeiou".indexOf(ch2)>=0) //to check whether both are
vowels
{
vowcou++; //counting the words which start and end
with a vowel
vow=vow+" "+str1; //joining the words that start and
end with a vowel
}
else
{
cons=cons+" "+str1; //joining the words that do not
start and end with a vowel
}
str1=""; //reinitializing the word
}
}
System.out.println("The number of words that starts and ends
with a vowel:"+vowcou); //printing the no. of words that start and
end with a vowel
System.out.println(vow+cons); //printing the sentence with the
vowel words at the front
}
}
SAMPLE INPUT:
Enter a string
Anamika and Suman are not quarrelling anymore
SAMPLE OUTPUT:
The number of words that starts and ends with a vowel:3
Anamika are anymore and Suman not quarrelling
PROGRAM 9: ICSE SCHOOLS AND ITS
LOCATIONS
AIM:
To store the names of 20 ICSE schools along with the name of the
town and search whether a name of the school is present in the list or
not. If it is present, print the name of the school along with its town,
otherwise display no found
ALGORITHM:
[String sch[]: Array for the schools
String loc[]: Array for the locations for schools
String str: User input for school to be searched
String str1: To store the array for school, if it is present in the list
String str2: to store the array for the location of the school searched, if it
is present in the list
int i, j: loop variables]
STEPS DESCRIPTION
1. Start the program
2. Import the package java.util.*
3. Name the class ICSE
4. Declare the variables including the array variables with its
size.
5. Get the names of schools and its locations accordingly
from the user and initialize the input variables.
6. Get the school to be searched from the user and initialize
the variable
7. If the searched school is present in the list of schools
already given, store the name of the school in one
variable and its corresponding location in another
variable.
8. If the variable for the school is empty, print that the school
is not found. Otherwise, print the school with its location.
9. Compile the program
10. End the program
PROGRAM:
//To store the names of 20 ICSE schools along with the name of the town
and search whether a name of the school is present in the list or not. If it
is present, print the name of the school along with its town, otherwise
display no found
import java.util.*;
class ICSE
{
public static void main(String args[])
{
//Declaration of variables
String sch[]=new String[20]; //for schools
String loc[]=new String[20]; //for the location of schools
String str,str1="",str2="";
int i,j;
Scanner kr=new Scanner(System.in);
System.out.println("Enter 20 schools"); //user input for schools
for(i=0;i<20;i++)
{
sch[i]=kr.nextLine(); //initialization
}
System.out.println("Enter the location of the school accordingly");
//user input for locations
for(j=0;j<20;j++)
{
loc[j]=kr.nextLine(); //initialization
}
System.out.println("Enter school needed to be searched"); //user
input to search school
str=kr.nextLine(); //initialization
for(i=0;i<20;i++)
{
if(sch[i].equals(str)) //if the searched school is present in the list
{
str1=sch[i];
str2=loc[i];
break;
}
}
if(str1=="")
System.out.println("School not found"); //if the school is not in
list
else
System.out.println(str1+", "+str2); //printing the school and its
location if the school is present in the list
}
}
SAMPLE INPUT:
Enter 20 schools
Greenwood High International School
Johnson Grammar School ICSE & ISC
Gitanjali Senior School
St Joseph’s Academy
Smt. Sulochanadevi Singhania School
N.L Dalmia High School
Holy Heart Schools
Greenwood High School
Presidency School
The Chintels School
Jamnabai Narsee School
Bombay Scottish School
Ryan International School
Hiranandani Foundation School
Jasudben ML School
Parle Tilak Vidyalaya
Wisdom High International School
Presidency School
New Horizon Public School
BGS World School
Enter the location of the school accordingly
Bengaluru, Karnataka
Hyderabad, Telangana
Hyderabad, Telangana
Dehradun, Uttarakhand
Thane (West), Maharashtra
Thane, Maharashtra
Amritsar, Punjab
Bengaluru, Karnataka
Bengaluru, Karnataka
Kanpur, Uttar Pradesh
Mumbai, Maharashtra
Mumbai, Maharashtra
Thane, Maharashtra
Mumbai, Maharashtra
Mumbai, Maharashtra
Mumbai, Maharashtra
Nashik, Maharashtra
Bengaluru, Karnataka
Bengaluru, Karnataka
Chickballapur, Karnataka
Enter school needed to be searched
Ryan International School
SAMPLE OUTPUT:
Ryan International School, Thane, Maharashtra
PROGRAM 10: SYMMETRIC MATRIX
AIM:
To create and store numbers in a square matrix and check
and display whether it is a symmetric matrix or not
ALGORITHM:
[int m: User input for size of square matrix
int i, j: Loop variables
boolean flag: To check whether the transpose of square matrix is
equal to the original matrix
int num[][]: Matrix array from user
int num1[][]: Transpose matrix]
STEPS DESCRIPTION
1. Start the program
2. Import the package java.util.*
3. Name the class Symmetric
4. Declare the variables for the program excluding the
array variables
5. Get the user input for the size of the square matrix
and initialize the size variable.
6. Declare the array variables int num[][] and int num1[][]
and initialize the input variable
7. Print the original matrix
8. Find the transpose of the original matrix and print it
9. Check whether all the elements in the transpose
matrix are equal to all the elements of the original
matrix.
10. Even if one element does not match, declare the flag
as false and give a break statement.
11. If the flag is false, print that the square matrix is not
symmetric. Otherwise, print that the matrix is
symmetric.
12. Compile the program
13. End the program
PROGRAM:
//To create and store numbers in a square matrix and check and
display whether it is a symmetric matrix or not
import java.util.*;
class Symmetric
{
public static void main(String args[])
{
//Declaration of variables
int m,i,j;
boolean flag=true;
Scanner kr=new Scanner(System.in);
System.out.println("Enter number for square matrix"); //user
input for size of matrix
m=kr.nextInt(); //initialization
//Declaring the arrays
int num[][]=new int[m][m];
int num1[][]=new int[m][m];
System.out.println("Enter matrix elements"); //user input for
matrix elements
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
num[i][j]=kr.nextInt(); //initialization
}
}
System.out.println("The original matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(num[i][j]+" "); //printing the original
matrix
}
System.out.println();
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
num1[j][i]=num[i][j]; //transpose of the matrix
}
}
System.out.println("The transpose matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(num1[i][j]+" "); //printing the transpose
matrix
}
System.out.println();
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(num1[i][j]!=num[i][j]) //declaring flag as false even if
one element is not same as the original
{
flag=false;
break;
}
}
}
if(flag==false) //if transpose is not equal to original
System.out.println("The square matrix is not symmetric");
else
System.out.println("The square matrix is symmetric");
}
}
SAMPLE INPUT:
Enter number for square matrix
3
Enter matrix elements
3
2
4
2
6
2
4
2
3
SAMPLE OUTPUT:
Original matrix:
3 2 4
2 6 2
4 2 3
Transpose matrix:
3 2 4
2 6 2
4 2 3
The square matrix is symmetric
PROGRAM 11: ELEMENTS ABOVE RIGHT
DIAGONAL
AIM:
To display the numbers which are above the right diagonal in a
5x5 matrix
ALGORITHM:
[int i, j: Loop variables
int a[][]: matrix array from user]
STEPS DESCRIPTION
1. Start the program
2. Import the package java.util.*
3. Name the class AboveRightDiagonal
4. Declare all the variables for the program and also
declare the size of the matrix.
5. Get the matrix elements from the user and declare the
matrix variables
6. Print the original matrix
7. Print the elements that are above the right diagonal
according to their positions in the 5x5 matrix
8. Compile the program
9. End the program
PROGRAM:
//To display the numbers which are above the right diagonal in a
5x5 matrix
import java.util.*;
class AboveRightDiagonal
{
public static void main(String args[])
{
//Declaration of variables
int i,j;
int a[][]=new int[5][5];
Scanner kr=new Scanner(System.in);
System.out.println("Enter matrix elements"); //user input for
matrix elements
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
a[i][j]=kr.nextInt(); //initialization
}
}
System.out.println("The original matrix:");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
System.out.print(a[i][j]+" "); //printing the original matrix
}
System.out.println();
}
System.out.println("The matrix above left diagonal:");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(i+j<=3) //position of elements above left diagonal
System.out.print(a[i][j]+" "); //printing the elements
above the left diagonal
}
System.out.println();
}
}
}
SAMPLE INPUT:
Enter matrix elements
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
SAMPLE OUTPUT:
The original matrix:
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
The matrix above right diagonal:
11 12 13 14
16 17 18
21 22
26
PROGRAM 12: PRODUCT OF ELEMENTS IN
ROWS AND COLUMNS
AIM:
To find the product of the elements stored in each row and
each column in a 4x4 matrix
ALGORITHM:
[int i, j: Loop variables
int prodcol: Product of each column
int prodrow: Product of each row
int a[][]: Matrix array from user]
STEPS DESCRIPTION
1. Start the program
2. Import the package java.util.*
3. Name the class Product
4. Declare the variables for the program and also declare
the size of the matrix
5. Get the user input for the matrix elements and initialize
the input variable.
6. Print the original matrix
7. Print the product of elements in each row in each
iteration
8. Print the product of elements in each column in each
iteration
9. Compile the program
10. End the program
PROGRAM:
//To find the product of the elements stored in each row and each
column
import java.util.*;
class Product
{
public static void main(String args[])
{
//Declaration of variables
int i,j,prodcol=1,prodrow=1;
int a[][]=new int[4][4];
Scanner kr=new Scanner(System.in);
System.out.println("Enter matrix elements"); //user input for
matrix elements
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
a[i][j]=kr.nextInt(); //initialization
}
}
System.out.println("The original matrix:");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(a[i][j]+" "); //printing the original matrix
}
System.out.println();
}
for(i=0;i<4;i++)
{
prodrow=1; //reinitializing the product of elements of row
for(j=0;j<4;j++)
{
prodrow=prodrow*a[i][j]; //finding the product of
elements in a row
}
System.out.println("The product of row
"+(int)(i+1)+"="+prodrow); //printing the product
}
for(j=0;j<4;j++)
{
prodcol=1; //reinitializing the product of elements in column
for(i=0;i<4;i++)
{
prodcol=prodcol*a[i][j]; //finding the product of elements
in a column
}
System.out.println("The product of column
"+(int)(j+1)+"="+prodcol); //printing the product
}
}
}
SAMPLE INPUT:
Enter matrix elements
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
SAMPLE OUTPUT:
11 12 13 14
15 16 17 18
19 20 21 22
23 24 25 26
The product of row 1=24024
The product of row 2=73440
The product of row 3=175560
The product of row 4=358800
The product of column 1=72105
The product of column 2=92160
The product of column 3=116025
The product of column 4=144144
STEPS DESCRIPTION
1. Start the program
2. Import the package java.util.*
3. Name the class Armstrong
4. In a return method int Armstrong, have a parameter int
n.
5. Declare the necessary variables inside the method.
Store the original value in another variable so that the
original value is not lost.
6. Open a while loop, get each digit in the number by
getting the remainder when divided by 10. Add the
sum of cubes of the digits and divide the number by 10
to reduce the number by one digit.
7. Close the while loop
8. If the sum of cubes of digits is equal to the original
number return 1 for true. Otherise, return 0 for false
and close the method.
9. Int the main() method, get the user input for the
number and initialize the input variable
10. Create an object and call the method in an integer
variable for true or false
11. If the variable is 1, print that the number is an
Armstrong number. Otherwise, print that the number is
not an Armstrong number
12. Compile the program
13. End the program
PROGRAM:
//to receive a number in a method Armstrong(int) and check
whether the number is Armstrong or not
import java.util.*;
class Armstrong
{
int Armstrong(int n) //method to accept the number and
checking if the number is Armstrong
{
int cubesum=0,d; //Declaration of variables
int m=n; //storing original number
while(n!=0)
{
d=n%10; //taking digit by digit
cubesum=cubesum+(d*d*d); //cubing the digit and adding
the cube of digits
n=n/10; //reducing the number by one digit
}
if(cubesum==m) //if the sum of cube of digits is equal to the
original number
return 1; //returning true value
else //if the sum of cube of digits is not equal to the original
number
return 0; //returning false value
}
public static void main()
{
Scanner kr=new Scanner(System.in);
System.out.println("Enter a number"); //user input
int n=kr.nextInt(); //initialization
Armstrong txt=new Armstrong(); //object creation
int r=txt.Armstrong(n);
if(r==1)
System.out.println("The number is an Armstrong number");
//printing it is an Armstrong number
else
System.out.println("The number is not an Armstrong
number"); //printing it is not an Armstrong number
}
}
SAMPLE INPUT:
Enter a number
153
SAMPLE OUTPUT:
[1^3+5^3+3^3=153]
The number is an Armstrong number
PROGRAM 14: SERIES
AIM:
To design a class to overload a method series:
(i) double series(double n) with one double type argument that
returns the sum of series
(ii)double series(double a,double n) with two double type arguments
that returns the sum of series
ALGORITHM:
[Double n: User input for number of terms
Double a: User input for a
Double i: Loop variable
Double sum1: Sum of the series (i)
Double sum2: Sum of the series(ii)
Int j: Numerator for series(ii)
Int exp: Exponent for a in series(ii)
Double r, p: Calling the methods in integer data types]
STEPS DESCRIPTION
1. Start the program
2. Import the package java.util.*
3. Name the class DoubleSeries
4. For series(i), in a return method double series with a
parameter double n, declare the necessary variables.
5. Open a for loop, find the sum of the series, having the
numerator till n terms and its denominator incremented
by 1 in each term and close the for loop
6. Return the sum of series(i) and close the method
7. For series(ii), in a return method double series with
parameters double a and double n, declare the
necessary variables.
8. Open a for loop for n terms, find the sum of the series,
having the numerator incremented by 3 in each term
and the denominator with a, raised to a power which is
incremented by 1 in each term. Close the for loop
9. Return the sum of series(ii) and close the method
10. In the main() method, get the user input for n and a for
the series.
11. Create an object and call the methods in two different
integer variables for each series.
12. Print the series separately
13. Compile the program
14. End the program
PROGRAM:
//To design a class to overload a method series: (i) double
series(double n) with one double type argument that returns the
sum of series
//(ii)double series(double a,double n) with two double type
arguments that returns the sum of series
import java.util.*;
class DoubleSeries
{
double series(double n) //method for series(i)
{
double sum1=0; //Declaration of variables
for(double i=1;i<=n;i++)
{
sum1=sum1+(i/(i+1)); //sum of the series(i)
}
return sum1; //returning the value of sum1 of series(i)
}
double series(double a,double n) //method for series(ii)
{
//Declaration of variables
double sum2=0;
int j=1,exp=1;
for(double i=1;i<=n;i++)
{
exp=exp+1; //value of exponent
sum2=sum2+(j/(Math.pow(a,exp))); //sum of the series(ii)
j=j+3; //incrementing numerator value by 3
}
return sum2; //returning the value of sum2 of series(ii)
}
public static void main()
{
Scanner kr=new Scanner(System.in);
System.out.println("Enter the value of n"); //user input for n
double n=kr.nextDouble(); //initialization
System.out.println("Enter the value of a"); //user input for a
double a=kr.nextDouble(); //initialization
DoubleSeries txt=new DoubleSeries(); //object creation
double r=txt.series(n);
double p=txt.series(a,n);
System.out.println("The sum of the series(i):"+r); //printing the
sum of series(i)
System.out.println("The sum of the series(ii):"+p); //printing
the sum of series(ii)
}
}
SAMPLE INPUT:
Enter the value of n
10
Enter the value of a
5
SAMPLE OUTPUT:
The sum of the series(i):7.980122655122655
The sum of the series(ii):0.08749983744
STEPS DESCRIPTION
1. Start the program
2. Import the package java.util.*
3. Name the class Discount
4. Declare all the variables for the program
5. In the input method, void input(), get the name of the
customer and cost of the article from the user and
initialize the input variables, and close the input method
6. Int the logic method, void cal(), calculate the discounts
and the amount after discount
7. If the cost of the article is less than or equal to Rs.5000,
no discount. If the cost of the article is between Rs.5000
and Rs.10001, the discount is 10% of the cost. If the
cost of the article is between Rs.10000 and Rs.15001,
the discount is 15% of the cost. If the cost of the article
is greater than Rs.15000, the discount is 20% of the
cost. Calculate the amount according to the condition.
8. Close the logic method
9. Int the display method, void display(), display the name
of the customer, the discount and final amount,
according to the condition and close the display method
10. In the main method, create an object and call the three
methods
11. Compile the program
12. End the program
PROGRAM:
//To accept the name of the customer and cost of article and
compute the discount and display the name of the customer,
discount and the amount to be paid
import java.util.*;
class Discount
{
//Instance variables
int cost;
String name;
double dc;
double amt;
//Input method
void input() //method to get input from user
{
Scanner kr=new Scanner(System.in);
System.out.println("Enter the name of customer"); //user input
for name of customer
name=kr.nextLine(); //initialization
System.out.println("Enter cost of article"); //user input for cost
of article
cost=kr.nextInt(); //initialization
}
//Logic method
void cal() //method to calculate discount
{
if(cost<=5000)
dc=0; //no discount
else if(cost>=5001 || cost<=10000)
{
dc=cost*(0.1); //discount is 10% of cost
amt=cost-dc; //amount after discount
}
else if(cost>=10001 || cost<=15000)
{
dc=cost*(0.15); //discount is 15% of cost
amt=cost-dc; //amount after discount
}
else if(cost>15000)
{
dc=cost*(0.2); //discount is 20% of cost
amt=cost-dc; //amount after discount
}
}
//Display method
void display() //method to display name, discount and total
amount
{
System.out.println("NAME OF THE CUSTOMER
DISCOUNT AMOUNT TO BE PAID");
if(dc==0)
System.out.println(name+" "+"No discount"+"
"+"Rs."+cost);
else if(cost>=5001 || cost<=10000)
System.out.println(name+" "+"Rs."+dc+" "+"Rs."+amt);
else if(cost<=10001 || cost<=15000)
System.out.println(name+" "+"Rs."+dc+" "+"Rs."+amt);
else if(cost>15000)
System.out.println(name+" "+"Rs."+dc+" "+"Rs."+amt);
}
public static void main()
{
Discount txt=new Discount(); //object creation
//calling the methods
txt.input();
txt.cal();
txt.display();
}
}
SAMPLE INPUT:
Enter the name of customer
Leela
Enter cost of article
10500
SAMPLE OUTPUT:
NAME OF THE CUSTOMER DISCOUNT AMOUNT TO BE PAID
Leela Rs.1050.0 Rs.9450.0