Revision Work
Revision Work
Revision Work
(Slab Programming)
WAP to enter the name of a consumer, the consumer number and the units being consumed in a
month.
Find the electricity bill of the month considering the following conditions:
Units Charge
up to first 100 units Rs. 1.50 per unit
for next 100 units Rs. 1.85 per unit
for next 100 units Rs. 2.50 per unit
more than 300 units Rs. 3.50 per unit
Print the consumer name,consumer number and the bill amount.
*/
import java.util.*;
class decision_making_2
{
void main()
{
Scanner ob=new Scanner(System.in);
String name,consumer_no;
int units;
double amt=0.0;
System.out.println("Enter the name of the consumer:");
name=ob.nextLine();
System.out.println("Enter the consumer number:");
consumer_no=ob.nextLine();
System.out.println("Enter the units being consumed:");
units=ob.nextInt();//350=100+100+100+50
if(units<=100)
amt=units*1.50;//
else if(units>100 && units<=200)
amt=(100*1.50)+(units-100)*1.85;
else if(units>200 && units<=300)
amt=(100*1.50)+(100*1.85)+(units-200)*2.50;
else
amt=(100*1.50)+(100*1.85)+(100*2.50)+(units-300)*3.50;
System.out.println("Name of the consumer is:"+name);
System.out.println("Consumer number is:"+consumer_no);
System.out.println("Electricity Bill amount is:"+amt);
}
}
/*
WAP to compute the railway fare depending on the criteria as given below:
Age(in years) Distance (in kms) Fare(in Rupees)
Below or equals to10 Below or=to10 Rs. 5
Between 10 to 50 Rs. 20
Above 50 Rs. 50
Between 10 to 60 Below or=to10 Rs. 10
Between 10 to 50 Rs. 40
Above 50 Rs. 80
Above 60 Below or=to10 Rs. 4
Between 10 to 50 Rs. 15
Above 50 Rs. 35
*/
import java.util.*;
class rail_fare
{
void main()
{
Scanner ob=new Scanner(System.in);
int age,dis;
System.out.println("Enter the age:");
age=ob.nextInt();
System.out.println("Enter the distance to be travelled:");
dis=ob.nextInt();
if(age<=10)
{
if(dis<=10)
System.out.println("Fare is: Rs. 5.00");
else if(dis>10 && dis<=50)
System.out.println("Fare is: Rs. 20.00");
else
System.out.println("Fare is: Rs. 50.00");
}
else if(age>10 && age<=60)
{
if(dis<=10)
System.out.println("Fare is: Rs. 10.00");
else if(dis>10 && dis<=50)
System.out.println("Fare is: Rs. 40.00");
else
System.out.println("Fare is: Rs. 80.00");
}
else
{
if(dis<=10)
System.out.println("Fare is: Rs. 4.00");
else if(dis>10 && dis<=50)
System.out.println("Fare is: Rs. 15.00");
else
System.out.println("Fare is: Rs. 35.00");
}
}
}
/*
WAP to enter a number and check whether it is Unique-digit number or not.
Logic: If in a number,there are no repeated digits, then the number is considered as Unique-digit
number/
Unique number.
Ex: 324,4578 etc
Invalid Unique-digit numbers: 2332, 56589 etc
*/
import java.util.*;
class unique_num
{
void main()
{
Scanner ob=new Scanner(System.in);
int n,m,i,d,c,f=0;
System.out.println("Enter a number:");
n=ob.nextInt();// 434
for(i=0;i<=9;i++)//i=4
{
m=n;//434
c=0;
while(m!=0)
{
d=m%10;//4
if(d==i)
{
c++;//2
}
m/=10;//0
}
if(c>1)
{
f=1;
break;
}
}
if(f==0)
System.out.println(n+" is unique number");
else
System.out.println(n+" is not unique number");
}
}
/*
Write a menu driven program using switch case to perform the following:
i) s= x^2/3! + x^4/5! + x^6/7! + ......+up to n terms
ii) s= 1 + 1/3! + 1/5! + 1/7! + ......+ up to n terms
iii) s= 2! - 3! + 4! - 5! + 6! - 7! +..... up to n terms
*/
import java.util.*;
class switch_prog2
{
void main()
{
Scanner ob=new Scanner(System.in);
int ch,x,n,i,fact,k,m,sum=0;
double s=0.0;
System.out.println("1. Find the sum of the first series");
System.out.println("2. Find the sum of the second series");
System.out.println("3. Find the sum of the third series");
System.out.println("Enter the choice(1-3):");
ch=ob.nextInt();
switch(ch)
{
case 1: System.out.println("Enter the value of x and n:");
x=ob.nextInt();
n=ob.nextInt();
m=2;
for(i=1;i<=n;i++)
{
fact=1;
for(k=1;k<=(m+1);k++)
{
fact=fact*k;//
}
s=s+(double)(int)Math.pow(x,m)/fact;
m=m+2;//4
}
System.out.println("Sum is:"+s);
break;
case 2: System.out.println("Enter the value of n:");
n=ob.nextInt();
m=1;
for(i=1;i<=n;i++)
{
fact=1;
for(k=1;k<=m;k++)
{
fact=fact*k;
}
s=s+(double)1/fact;
m=m+2;//3
}
System.out.println("Sum is:"+s);
break;
case 3: System.out.println("Enter the value of n:");
n=ob.nextInt();
m=2;
for(i=1;i<=n;i++)
{
fact=1;
for(k=1;k<=m;k++)
{
fact=fact*k;
}
if(m%2==0)
{
sum=sum+fact;
}
else
{
sum=sum-fact;
}
m=m+1;//3
}
System.out.println("Sum is:"+sum);
break;
default: System.out.println("Wrong Choice");
}
}
}
/*
Write a menu driven program using switch case to perform the following:
a) Print first 10 Composite numbers.
b) Print first 10 terms of a Fibonacci series.
c) Print the Abundant numbers from 100 to 120.
*/
import java.util.*;
class switch_prog3
{
void main()
{
Scanner ob=new Scanner(System.in);
int ch,i,m,c,k,f=0,a,b,s;
System.out.println("1. Print first 10 Composite numbers.");
System.out.println("2. Print first 10 terms of a Fibonacci series.");
System.out.println("3. Print the Abundant numbers from 100 to 120.");
System.out.println("Enter the choice(1-3):");
ch=ob.nextInt();
switch(ch)
{
case 1: for(i=1;;i++)// infinite loop
{
m=i;//
c=0;
for(k=1;k<=m;k++)
{
if(m%k==0)
{
c++;//4
}
}
if(c>2)
{
System.out.println(i);//4 6.......
f++;//10
}
if(f==10)
break;// to stop the execution of the infinite loop
}
break;
case 2: a=0;
b=1;
System.out.println(a);
System.out.println(b);
for(k=1;k<=8;k++)
{
c=a+b;//8
System.out.println(c);//0 1 1 2 3 5 8
a=b;//3
b=c;//5
}
break;
case 3: for(i=100;i<=120;i++)
{
m=i;//100
s=0;
for(k=1;k<m;k++)
{
if(m%k==0)
{
s=s+k;
}
}
if(s>m)
System.out.println(i);
}
break;
default: System.out.println("Wrong choice");
}
}
}
/*
Write a menu driven program using switch case to perform the following:
i) Enter a number and find the smallest digit of it.
ii) Enter a number. Remove odd digits from it and reprint the number.
iii) Enter a number and find the sum of factors of it.
*/
import java.util.*;
class switch2
{
void main()
{
Scanner ob=new Scanner(System.in);
int ch,n,m,d,small=0,f=0,s=0,c=0,i;
System.out.println("Enter 1: To input a number and find the smallest digit of it");
System.out.println("Enter 2: To input a number and reprint the number after removing odd digits
from it");
System.out.println("Enter 3: To input a number and find the sum of factors of it");
System.out.println("Enter the choice (1-3):");
ch=ob.nextInt();//
switch(ch)
{
case 1: System.out.println("Enter a number:");
n=ob.nextInt();
m=n;
while(n!=0)
{
d=n%10;
if(f==0)
{
small=d;
f=1;
}
if(d<small)
small=d;
n=n/10;
}
System.out.println("Smallest digit in the number "+m+" is: "+small);
break;
case 2: System.out.println("Enter a number:");
n=ob.nextInt();
m=n;
while(n!=0)
{
d=n%10;
if(d%2==0)
{
s=s+(int)Math.pow(10,c++)*d;
}
n=n/10;
}
System.out.println("After removing the odd digits "+m+" will become "+s);
break;
case 3: System.out.println("Enter a number:");
n=ob.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
{
s=s+i;
}
}
System.out.println("Sum of factors of "+n+" is: "+s);
break;
default: System.out.println("Wrong choice");
}
}
}