Record Pgms
Record Pgms
Write the program on the right side and variable description table on
the left side.
Follow the same format.
Start the next program on a new page.
Do the variable description table by yourself from pgm-2. One has
been done for you in the first program.
Program: 1
JAVA PROGRAM TO DO ELECTRICITY BILL CALCULATION
AIM:
PROGRAM CODING :
import java.util.*;
class deci1
int units;
double charge;
units=s.nextInt();
if(units<=100)
charge=units*0.40+50;
charge=100*0.40+(units-100)*0.60+50;
else
charge=100*0.40+200*0.60+(units-300)*0.90+50;
}}
INPUT/OUTPUT:
250
RESULT:
AIM:
A Java program to implement menu driven calculator to find the area of square,
rectangle, circle.
PROGRAM CODING :
import java.util.*;
class deci2
int ch;
double area,r,l,b,side;
ch=s.nextInt();
switch(ch)
side=s.nextDouble();
area=side*side;
System.out.println("area of square"+area);
break;
l=s.nextDouble();
b=s.nextDouble();
area=l*b;
System.out.println("area of rectangle"+area);
break;
r=s.nextDouble();
area=3.14*r*r;
System.out.println("area of circle"+area);
break;
}}
INPUT/OUTPUT:
1.area of square
2.area of rectangle
3.area of circle
area of rectangle 35
RESULT:
AIM:
PROGRAM CODING:
import java.util.*;
class loop1
int i,n,rev=0;
System.out.println("Enter n");
n=s.nextInt();
int n1=n;
while(n>0)
int d=n%10;
rev=rev*10+d;
n=n/10;
if(rev==n1)
System.out.println("Palindrome number");
else
}}
INPUT/OUTPUT:
Enter n
121
Palindrome number
RESULT:
AIM:
PROGRAM CODING :
import java.util.*;
class loop2
float x,n,i,s=0,f;
System.out.println("enter n");
n=s1.nextFloat();
System.out.println("enter x");
x=s1.nextFloat();
int sign=-1,j;
for(i=1;i<=n;i++)
{ f=1;
sign=sign*-1;
for(j=1;j<=i;j++)
f=f*j;
s=s+sign*(i*x/f);
System.out.println("Answer is"+s);
}}
INPUT/OUTPUT:
enter n
Enter x
Answer is 1
RESULT:
AIM:
Write a java program to find the number of positive odd, positive even, negative
odd and negative even number using do..while statement.
PROGRAM CODING:
import java.util.*;
class loop3
int n,i,pe=0,ne=0,po=0,no=0,o=0;
do
System.out.println("enter n");
n=s.nextInt();
pe++;
ne++;
po++;
no++;
}while(n!=0);
}}
INPUT/OUTPUT:
this program reads n numbers and counts no of odd & positive number
-1
RESULT:
FIBONACCI SERIES
AIM:
Write a java program to generate a method fib to print the Fibonacci series till n
terms.
PROGRAM CODING :
import java.util.*;
class method1
void fib()
System.out.println("enter n terms");
n=s.nextInt();
System.out.print(f+" "+se);
for(i=3;i<=n;i++)
t=f+se;
f=se;
se=t;
System.out.print(" "+t);
{
method1 m=new method1();
m.fib();
}}
INPUT/OUTPUT :
enter n terms
0 1 1 23 5 8
RESULT:
PRIME NUMBER
AIM :
Write a java program to create a method prime which receives a number and
checks whether it is a prime or not.
PROGRAM CODING :
import java.util.*;
class prime
void pri(int n)
int i,c=0;
for(i=1;i<=n;i++)
if(n%i==0)
c++;
if(c==2)
else
int n;
n=s.nextInt();
p.pri(n);
}}
INPUT/OUTPUT:
enter n
5 is a prime number
RESULT:
ARMSTRONG NUMBER
AIM:
PROGRAM CODING:
import java.util.*;
class armstr
int arms()
int n,i,s=0,d,orig_n;
System.out.println("enter n");
n=sc.nextInt();
orig_n=n;
for(;n>0;n=n/10)
d=n%10;
s=s+d*d*d;
if(orig_n==s)
return 1;
else
return 0;
int x=a.arms();
if(x==1)
else
}}
INPUT/OUTPUT:
Enter n
345
RESULT:
STUDENT’S MARKLIST
AIM:
Write a java program to define a class student with the descriptions rollno,
name, marks of 5 subjects, total and average and initialize the above members
using constructors and calculate the average.
PROGRAM CODING:
import java.util.*;
class cons1
int rno;
String n;
float m1,m2,m3,m4,m5,tot,avg;
public cons1()
rno=0;
n=" ";
m1=0;
m2=0;
m3=0;
m4=0;
m5=0;
tot=0;
avg=0;}
rno=r;
n=nm;
m1=mk1;
m2=mk2;
m3=mk3;
m4=mk4;
m5=mk5;
tot=0;
avg=0;}
tot=m1+m2+m3+m4+m5;
avg=tot/5;
void dis()
//c.dis();
c1.dis();
}}
INPUT/OUTPUT:
Roll no: 5
Name: anu
Mark1: 67
Mark2: 78
Mark3: 89
Mark4: 90
Mark5: 97
Total: 421
average: 84.2
RESULT:
AIM:
Write a menu driven program to change the string into uppercase and lowercase,
finding the length of a string, combine the strings, reverse a string, find a
position of a substring and compare the two strings using library classes.
PROGRAM CODING:
import java.util.*;
class string
int ch;
ch=sc.nextInt();
switch(ch)
break;
break;
break;
String x1=sc.next();
System.out.println("result is "+x.concat(x1));
break;
case 5:System.out.println("result is "+x.reverse());
break;
break;
String x11=sc.next();
if(x.equals(x11))
else
break;
String xx1=sc.next();
}}
String x;
System.out.println("enter a string");
x=sc.next();
s(x);
}}
INPUT/OUTPUT:
enter a string
COMPUTER
enter ch
1.to upper case
2.lowercase
3.string length
5. reverse
7.string compare
8.substring
computer
RESULT:
PALINDROME
AIM:
Special words are those words which start and end with the same
letter.
Example: EXISTENCE, COMIC, WINDOW
Palindrome words are those words which read the same from left to right
and vice-versa.
Example: MALYALAM, MADAM, LEVEL, ROTATOR, CIVIC
All palindromes are special words but all special words are not palindromes.
Write a program to accept a word. Check and display whether the word is a
palindrome or only a special word or none of them.
PROGRAM CODING:
import java.util.*;
class Palin
System.out.println(“Enter a word”);
String s=sc.next();
String rv=””;
for(int i=0;i<s.length();i++)
rv=s.charAt(i)+rv;
if(s.equals(rv))
System.out.println(“Palindrome string”);
else
}}
Palindrome string
RESULT:
STRING PATTERN
AIM:
Example 1:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 1
Enter the number of terms 5
Sample Output:
*****
****
***
**
*
Example 2:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 2
Enter the number of terms 5
Sample Output:
ABCDE
ABCD
ABC
AB
A
import java.util.Scanner;
switch (ch) {
case 1:
for (int i = 0; i <= n; i++) {
for (int j = 0; j < i; j++) {
System.out.print(' ');
}
for (int k = i; k <= n; k++) {
System.out.print('*');
}
System.out.println();
}
break;
case 2:
default:
System.out.println("Incorrect choice");
break;
}
}
}
INPUT/OUTPUT
RESULT:
SEARCHING
AIM:
Write a program in Java to store 10 different country names and their capitals
in two different Single Dimensional Arrays (SDA). Display the country names
(that starts with a vowel) along with their capitals in the given format.
PROGRAM CODING:
import java.util.Scanner;
System.out.println("Country Names\t\tCapital");
for (int i = 0; i < SIZE; i++) {
char ch = Character.toUpperCase(cnt[i].charAt(0));
if (ch == 'A' ||
ch == 'E' ||
ch == 'I' ||
ch == 'O' ||
ch == 'U') {
System.out.println(countries[i] + "\t\t" + cap[i]);
}
}
}
}
INPUT/OUTPUT
RESULT:
BUBBLE SORT
AIM:
PROGRAM CODING:
import java.util.Scanner;
//Bubble Sort
for (int i = 0; i < nms.length - 1; i++) {
for (int j = 0; j < nms.length - 1 - i; j++) {
if (nms[j].compareToIgnoreCase(nms[j + 1]) > 0) {
String t = nms[j + 1];
nms[j + 1] = nms[j];
nms[j] = t;
}
}
}
System.out.println("\nSorted Names");
for (int i = 0; i < nms.length; i++) {
System.out.println(nms[i]);
}
}
}
INPUT/OUTPUT:
RESULT:
CALCULATING AVERAGE
AIM:
Write a program to accept name and total marks of N number of students in two
single subscript arrays name[ ] and totalmarks[ ].
Calculate and print:
(i) The average of the total marks obtained by N number of students.
[average = (sum of total marks of all the students)/N]
(ii) Deviation of each student's total marks with the average.
[deviation = total marks of a student - average]
PROGRAM CODING:
import java.util.Scanner;
RESULT:
AIM:
PROGRAM CODING:
import java.util.Scanner;
int t = arr[i];
arr[i] = arr[sm];
arr[sm] = t;
}
RESULT:
ODD/EVEN
import java.util.Scanner;
int N= 20;
Scanner in = new Scanner(System.in);
int i = 0;
System.out.println("Enter 20 numbers:");
for (i = 0; i < N; i++) {
arr[i] = in.nextInt();
}
int e = 0, od = 0;
for (i = 0; i < N; i++) {
if (arr[i] % 2 == 0)
even[e++] = arr[i];
else
odd[od++] = arr[i];
}
System.out.println("Even Numbers:");
for (i = 0; i < e; i++) {
System.out.print(even[i] + " ");
}
System.out.println("\nOdd Numbers:");
for (i = 0; i < od; i++) {
System.out.print(odd[i] + " ");
}
}
}
Output
RESULT:
CONSTRUCTOR-I
AIM:
String flavour stores the flavour of the juice (e.g., orange, apple, etc.)
int pack_size stores package size (e.g., 200 mL, 400 mL, etc.)
Member
Purpose
Methods
PROGRAM CODING:
import java.util.Scanner;
public FruitJuice() {
p_code = 0;
flr = "";
p_type = "";
p_size = 0;
p_price = 0;
}
RESULT:
AIM:
Member
Purpose
Methods
Experience Increment
Write the main method to create an object of the class and call all the member
methods.
PROGRAM CODING
import java.util.Scanner;
public Grade_Revision() {
nm = "";
bas = 0;
expn = 0;
inc = 0.0;
nbas = 0.0;
}
Output
RESULT:
TELEPHONE BILL
AIM:
Define a class Bill that calculates the telephone bill of a consumer with the
following description:
Member
Purpose
Methods
Create an object in the main() method and invoke the above functions to
perform the desired task.
PROGRAM CODING
import java.util.Scanner;
public Bill() {
bno = 0;
name = "";
call = 0;
amt = 0.0;
}
Output
RESULT: