bookproblems
bookproblems
if(ch==1)
{
System.out.println("Enter the temp in centrigade");
double c=sc.nextDouble();
double f=(9*c+160)/5;
System.out.println("temp in farhenhite"+f);
}
else if(ch==2)
{
System.out.println("Enter the temp in farhenhite");
double f=sc.nextDouble();
double c=(5*f-160)/9;
System.out.println("temp in celcius"+c);
}
else
System.out.println("Sorry please try again");
}
}
Enter a four digit year and check whether it is a leap year or not
import java.util.*;
public class leap_year
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the year");
int yr=sc.nextInt();
if (yr%100==0)
{
if (yr%400==0)
System.out.println("Year"+yr+" is leap");
else
System.out.print("Year"+yr+" is not leap");
}
else
{
if(yr%4==0)
System.out.print("Year"+yr+" is leap");
else
System.out.print("Year"+yr+" is not leap");
}
}
}
Enter the three angles of a triangle and check whether they form an acute
triangle or obtuse triangle or scalene triangle
ans>>>
import java.util.*;
public class ang
{
public static void main()
{
double a,b,c;
Scanner sc=new Scanner(System.in);
ANS__
import java.util.*;
public class area_calc
{
public static void main()
{
area=a*a;
System.out.println("Area of the square is "+""+""+area);
break;
default:
System.out.print("VALID CHOICES WERE FROM 1 TO 4.YOUR CHOICE
EXCEEDED THE LIMIT");
}
System.out.println("Area of your figure is"+""+""+""+area);
}
}
WRITE A PROGRAM TO ENTER CHOICES AND PRINT THE DAYS OF THE WEEK
import java.util.*;
public class choice
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int ch;
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("THE DAY IS MONDAY");
break;
case 2:
System.out.println("the day is tuesday");
case 3:
System.out.println("the day is wednesday");
break;
case 4:
System.out.println("the day is thursday");
break;
case 5:
System.out.println("the day is friday");
break;
case 6:
System.out.println("the day is saturday");
break;
case 7:
System.out.println("the day is sunday");
break;
default:
System.out.println("the day is invalid");
}
}
}
WAP to display menu for volumes of different shapes and the calculate the
volume of the selected shape asking for required information
ans___
import java.util.*;
public class volume
{
public static void main()
{
Scanner sc=new Scanner(System.in);
double l,b,h,s,rd,volume=0;int choice;
System.out.println("VOLUME CALCULATOR");
System.out.println("1.SPHERE");
System.out.println("2.CUBOID");
System.out.println("3.CUBE");
System.out.println("Enter your choice");
choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.println("SPHERE VOLUME CALCULATION");
System.out.println("Enter the radius");
rd=sc.nextDouble();
volume=(4/3.0)*(22/7.0)*rd*rd*rd;
System.out.println("THE VOLUME OF THE SPHERE IS"+volume);
break;
case 2:
System.out.println("CUBOID VOLUME CALCULATION");
System.out.println("Enter the lenght ,breadth,height");
l=sc.nextDouble();
b=sc.nextDouble();
h=sc.nextDouble();
volume=l*b*h;
System.out.println("THE VOLUME OF THE CUBOID IS"+volume);
break;
case 3:
System.out.println("CUBE VOLUME CALCULATION");
System.out.println("Enter the side");
s=sc.nextDouble();
volume=s*s*s;
System.out.println("THE VOLUME OF THE CUBE IS"+volume);
break;
default:
System.out.println("Invalid choice");
}
System.out.println("The area of the figure is"+volume);
System.out.println("THANK YOU ,PLEASE TRY AGAIN");
}
}
Enter the lenght and breadth of a rectangle and print its diagonal.....
ANS>-
import java.util.*;
public class dia
{
public static void main()
{
Scanner sc=new Scanner(System.in);
double l,b,d;
System.out.println("Enter the lenght");
l=sc.nextDouble();
System.out.println("Enter the breadth");
b=sc.nextDouble();
d=Math.sqrt(l*l+b*b);
System.out.println("The lenght of the diagonal is"+""+""+d);
}
}
ANS---
import java.util.*;
class ShowRoom
{
String name;
long mobno;
double cost;
double dis;
double amount;
public ShowRoom()
{
name="";
mobno=0;
cost=0;
dis=0;
amount=0;
}
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE NECESSARY DETAILS");
name=sc.nextLine();// VARIABLE=OBJECT.NEXT(LINE/DOUBLE/INT/LONG)
mobno=sc.nextLong();
cost=sc.nextDouble();
}
public void calculate()
{
if(cost<=10000)
{
dis=(5/100.0)*cost;
}
else if(cost>10000 && cost<=20000)
{
dis=(10.0/100)*cost;
}
else if(cost>20000 && cost<=35000)
{
dis=(15.0/100)*cost;
}
else
{
dis=(20.0/100)*cost;
}
amount=cost-dis;
}
public void display()
{
System.out.println("NAME"+""+""+""+""+""+":"+name);
System.out.println("MOBILE NO "+""+""+""+""+""+":"+mobno);
System.out.println("TOTAL AMOUNT:"+""+""+""+""+""+":"+amount);
}
public static void main(String args[])
{
ShowRoom gf=new ShowRoom();
gf.input();
gf.calculate();
gf.display();
}
}
ANS>>>>>
import java.util.*;
public class time
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int sec,hr,min;
System.out.println("ENTER THE TIME IN SEC");
sec=sc.nextInt();
hr=sec/3600;
sec=sec%3600;
min=sec/60;
sec=sec%60;
System.out.println(hr+""+"hours"+min+""+"mins"+""+sec+"seconds");
}
}
Enter the no of calls and calculate the phone bill accordingly
First 100 calls rs 1.90/call
next 150 calls rs 1.90/call
next 200 calls rs 3.80/call
above 450 rs 4.0/call
ANS....
import java.util.*;
class Phonebill
{
public static void main(String args[])
{
double bill=0,net_charge;int call;
Scanner sc=new Scanner (System.in);
System.out.println("Enter the number of calls");
call=sc.nextInt();
if (call<=100)
{
bill=call*1.90;
}
else if(call>100 && call<=250)
{
bill=(100*1.90)+(call-100)*2.97;
}
else if(call>250 && call<=450)
{
bill=(100*1.90)+(150*2.97)+(call-250)*3.8;
}
else if(call>450)
{
bill=(100*1.90)+(150*2.97)+(200*3.80)+(call-450)*4.0;
}
else
{
System.out.println("Sorry ,try again");
}
net_charge=bill+300;
System.out.println("Your final bill"+net_charge);
}
}
******* PROBLEMS IN QUESTION PAPER*********
1>>>>>
import java.util.*;
public class Distance
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
double d,chg=0;int age;char gender;
System.out.println("Enter the age of the customer");
age=sc.nextInt();
System.out.println("Enter the distance travelled");
d=sc.nextDouble();
System.out.println("Enter your gender");
gender=sc.next().charAt(0);
if(age>=60)
{
if (gender=='F')
{
if(d<=100)
{
chg=100;
}
else if(d>100 && d<=300)
{
chg=400;
}
else
{
chg=600;
}
}
if(gender=='M')
{
if(d<=100)
{
chg=150;
}
else if(d>100 && d<=300)
{
chg=500;
}
else
{
chg=750;
}
}
}
else if(age<=10)
{
if(d<=100)
{
chg=50;
}
else if(d>100 && d<=300)
{
chg=250;
}
else
{
chg=350;
}
}
System.out.println("DISTANCE:-"+d);
System.out.println("GENDER:-"+gender);
System.out.println("FARES FOR THE RIDE"+chg);
System.out.println("**************THANK YOU ,HAVE A NICE
DAY**********************");
}
}
2>>>>>
import java.util.*;
public class Bloodgrp
{
public static void main(String args[])
{
int op;
Scanner sc=new Scanner(System.in);
System.out.println("*******MENU***********");
System.out.println("PRESS 1 IF YOU HAVE 'A' ANTIBODY");
System.out.println("PRESS 2 IF YOU HAVE 'B' ANTIBODY");
System.out.println("PRESS 3 IF YOU HAVE 'A' AND 'B' ANTIBODY");
System.out.println("PRESS 4 IF YOU HAVE NO ANTIBODY");
System.out.println("Enter the choice ");
op=sc.nextInt();
switch(op)
{
case 1:
System.out.println("BLOOD GROUP IS B POSITIVE");
break;
case 2:
System.out.println("BLOOD GROUP IS A POSITIVE");
break;
case 3:
System.out.println("BLOOD GROUP IS O POSITIVE");
break;
case 4:
System.out.println("BLOOD GROUP IS AB POSITIVE");
break;
default:
System.out.println("TRY AGAIN..");
}
}
}
import java.util.*;
public class Digit_alpha
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER A CHARECTER ");
String s;
char ch=sc.next().charAt(0);
if((ch>=65 && ch<=90)||(ch>=97 && ch<=122))
System.out.println("LETTER");
else if(ch>=48 && ch<=57)
System.out.println("DIGIT");
else
System.out.println("SPECIAL CHARECTER");
}
}
*************************************************************************
********************************************************************
LOOP
0, 4, 18, 48, 100, 180,
logic
0*(0*0),1*(2*2);2*(3*3);3*(4*4)
class Test {
public static void main(String[] args) {
int i, j, n = 5;
for (i = 0; i <= n; i++ ) {
j = i+1;
System.out.print((i*(j*j)) + ", ");
}
}
}
81 64 49 36 25
class p2
{
public static void main(String[]args)
{
int i;
for(i=9; i>=5 ; i--)
{
System.out.print((i*i)+" " );
}
}
}
FIBONACCI
2 9 28 65
class p5
{
public static void main(String args[])
{
int i;
for ( i=1 ; i<=4 ; i++)
{
System . out .print((i*i*i)+1 + " ");
}
}
}
_________________________________________________________________________
__________________________________
ALL LOOP PROBLEMS
_________________________________________________________________________
__________________________________
Q1:
11
9
22
20
33
31
44
42
ANS
class Q1
{
public static void main()
{
int i;
for(i=1;i<=4;i++)
{
System.out.println((11*i)+" ");
System.out.println((11*i-2)+" ");
}
}
}
Q2:
3 8 15 24 35 48 63
ANS
class Q2
{
public static void main()
{
int i,a=0;
for(i=2;i<=8;i++)
{
a=i*i-1;
System.out.print(a+" ");
}
}}
Q3:
2 6 15 31 56 logic:
1+1;2+4;6+9;15+16..
ANS
class Q3
{
public static void main()
{
int i,a=1;
for(i=1;i<=5;i++)
{
a=a+(i*i);
System.out.print(a+" ");
}
}}
Q4:
ANS
class Q4
{
public static void main()
{
int i,m=0,c=16,n=16;
for(i=2;i<=10;i+=2)
{
m=(n*i)+c;
System.out.print(m+" ");
}
}
}
Q5
2 3 4 4 6 8 8 12 16
ANS
class Q5
{
public static void main()
{
int i,a=2;
for(i=1;i<=4;i=i*2)
{
System.out.print(a+" ");
a=a+i;
System.out.print(a+ " ");
a=a+i;
System.out.print(a+ " ");
}
}
}
}
Q6
81 64 49 36 25
ANS
class Q6
{
public static void main(String args[])
{
for(int i=9;i>=5;i--)
System.out.print((i*i)+ " ");
}}
Q70 1 3 6 11
// (0+2^0)(1+2^1)(2+2^2)(3+2^3)//
class Newi
{
public static void main()
{
int i;
for(i=0;i<=3;i++)
{
System.out.print((i+(Math.pow(2,i))+ " "));
}
}
}
Q8:
LCM AND GCD
import java.util.*;
class lcmGCD
{
public static void main(String args[])
{
int n1,n2;
int gcd,lcm,remainder,numerator,denominator;
Scanner sc = new Scanner(System.in);
System.out.println("Enter two Numbers");
n1=sc.nextInt();
n2=sc.nextInt();
if (n1>n2)
{
numerator=n1;
denominator=n2;
}
else
{
numerator=n2;
denominator=n1;
}
remainder=numerator%denominator;
while(remainder!=0)
{
numerator=denominator;
denominator=remainder;
remainder=numerator%denominator;
}
gcd = denominator;
lcm = n1*n2/gcd;
System.out.println("GCD of "+n1+" and "+n2+" = "+gcd);
System.out.println("LCM of "+n1+" and "+n2+" = "+lcm);
}
}
import java.util.*;
1. NEON NUMBER
class Neon
{
public static void main(int n)
{int sq,d,s=0;
sq=n*n;
while(sq>0)
{
d=sq%10;
sq=sq/10;
s+=d;
}
if(s==n)
System.out.println("It's a Neon no!");
else
System.out.println("No,It's not a Neon no!");
}
}
2.ARMSTONG NUMBER
class Armstrong
{
public static void main(int n)
{
int d,s=0,a,no=n;
while(n>0)
{d=n%10;
n=n/10;
a=d*d*d;
s+=a;
}
if(no==s)
System.out.println("ARMSTRONG NO");
else
System.out.println("NOT ARMSTRONG");
}
}
3.PERFECT NO
class Perfect
{
public static void main(int n)
{
int i,s=0;
for(i=1;i<=n;++i)
{
if(n%i==0)
s+=i;
}
if (s==n)
System.out.println("IT'S A PERFECT NUMBER!");
else
System.out.println("NO,IT'S NOT A PERFECT NUMBER!");
}
}
4.palindrome
import java.util.Scanner;
public class Palindrome
{
public static void main()
{int n;
Scanner sc=new Scanner(System.in);
System.out.println("ENTER A NUMBER");
n=sc.nextInt();
int sum=0,r,temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("PALINDROME");
else
System.out.println("NOT A PALINDROME");
}
}
5.DUCK NUMBER
class Duck
{
public static void main(int n)
{
int c=0,d;
while(n>0)
{
d=n%10;
n=n/10;
if(d==0)
c++;break;
}
if(c==1)
System.out.print("duck");
else
System.out.print(" not duck");
}
}
6.ADAMS NO
class adams
{
public static void main(int n)
{
int sqn;
sqn=n*n;
int rn=0,revsq=0;
while(n>0)
{
rn=rn*10+n%10;
n=n/10;
}
while(sqn>0)
{
revsq=revsq*10+sqn%10;
sqn=sqn/10;
}
if(rn*rn==revsq)
System.out.println("ADAMS NO");
else
System.out.println(" not ADAMS NO");
}
}
import java.util.Scanner;
class Pal
{
static void palcheck()
{
Scanner sc=new Scanner( System.in);
System.out.println("ENTER A NUMBER");
int n;
n=sc.nextInt();
int sum=0,r;
int temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("PALINDROME");
else
System.out.println(" NOT PALINDROME");
}
if(temp==sum)
System.out.println("PALINDROME ");
else
System.out.println(" not PALINDROME");
}}
import java.util.Scanner;
class Prime
{
static void primeno()
{
Scanner sc=new Scanner( System.in);
System.out.print("ENTER A NUMBER");
int n;
n=sc.nextInt();
9, series
import java.util.Scanner;
class Mathseries
{
public static void main()
{
int x;int n;
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE NUMERATOR");
x=sc.nextInt();
System.out.println("ENTER THE LIMIT");
n=sc.nextInt();
double s=0,fact=1,i;
for(i=1; i<=n ;i++)
{
fact=fact*i;
if(i%2==1)
s=s+(int)Math.pow(x,i)/fact;
if(i%2==0)
s=s-(int)Math.pow(x,i)/fact;
}
System.out.println(s);
}
}
10. ENTER A LONG NUMBER AND A DIGIT AND CHECK HOW MANY TIMES THE DIGIT
OCCURS IN THE NUMBER
class freq
{
public static void main(long n,int di)
{
int d,c=0;
while(n>0)
{
d=(int)n%10;
n=n/10;
if(di==d)
c++;
}
System.out.println(c);
}
}
11. ENTER A LONG NUMBER AND A DIGIT AND CHECK HOW MANY TIMES THE DIGIT
OCCURS IN THE NUMBER(USING METHODS)
import java.util.Scanner;
class freq
{
static void freqno()
{
long n;int di,d,c=0;
Scanner sc=new Scanner(System.in);
System.out.println("ENTER ANY NUMBER");
n=sc.nextLong();
System.out.println("ENTER ANY DIGIT IN THAT NUMBER");
di=sc.nextInt();
while(n>0)
{
d=(int)n%10;
n=n/10;
if(di==d)
c++;
}
System.out.println(c);
}
public static void main(String args[])
{
long n=0;
int di=0,c=0;
Scanner sc=new Scanner(System.in);
System.out.println(c);
}
}
MISCELLNIOUS
PROBLEMS ON LOOPS
_________________________________________________________________________
_________________________________________________________________________
_QQ
Q1 >>1+2+3+4+5
class q1
{
public static void main(String args[])
{
int i,s=0;
for(i=1;i<=5;i++)
{
s+=i;
}
System.out.println(s);
}
}
Q2>>1*2*3*4*5
class q1
{
public static void main(String args[])
{
int i,s=1;
for(i=1;i<=5;i++)
{
s*=i;
}
System.out.println(s);
}
}
Q3>>FACTORIAL OF ANY NO
class fact
{
public static void main(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
System.out.println("Factorial of "+n+"is"+f);
}
}
Q4>> 10 20 30 40 50
class q1
{
public static void main(String args[])
{
int i;
for(i=10;i<=50;i+=10)
{
System.out.print(i+" ");
}
}
}
Q5:>> 50 40 30 20 10
class q1
{
public static void main(String args[])
{
int i;
for(i=50;i>=10;i-=10)
{
System.out.print(i+" ");
}
}
}
Q6>> 11 22 33 44 55
class Q2
{
public static void main(String args[])
{
int i;int p;
for(i=1;i<=5;i++)
{
p=11*i;
System.out.print(p+" ");
}
}
}
Q7>>
THE FACTORS OF A NUMBER
class factors
{
public static void main(int n)
{
int i;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
System.out.println(i+ " ");
}
}
}
}
class sum_of_factors
{
public static void main(int n)
{
int i,s=0;
for(i=1;i<=n;i++)
{
if (n%i==0)
{
s=s+i;
}
}
System.out.print("Sum of factors of the no"+ s);
}
}
Q9>> 1+3+5+7+9+11+.......n
class sum_of_odd_no
{
public static void main(int limit)
{
int i,s=0;
for(i=1;i<limit;i+=2)
{
s=s+i;
}
System.out.print(" THE SUM OF THE SERIES UPTO"+limit+" IS"+">>>"+s);
}
}
q10>> REVERSE OF A NO
class reverse_of_a_no
{
public static void main(int n)
{
int r=0,no=n,d;
while(n>0)
{
d=n%10;
n=n/10;
r=r*10+d;
}
System.out.println("THE REVERSED NO IS"+" "+r);
}
}
class binary
{
public static void main(int n)
{
String s=" ";int r;
while(n>0)
{
r=n%2;
s=s+r;
n=n/2;
}
System.out.println(s);
}
}
class sum_of_odd_even_no
{
public static void main(int n)
{
int d,se=0,so=0;
while(n>0)
{
d=n%10;
if(d%2==1)
se=se+d;
if(d%2==0)
so=so+d;
n=n/10;
}
System.out.println("The sum of the even numbers is:"+" "+se+"The sum of
the odd numbers is:"+" "+so);
}
}
Q13>> 1 3 6 10 15 21 28
class series1
{
public static void main(String args[])
{
int i,s=0;
for(i=1;i<=7;i++)
{
s=s+i;
System.out.println(s+" ");
}
}
}
Q14>> 1+3+6+10+15+21+...n(limit)
class series1same
{
public static void main(int limit)
{
int i,s=0,sum=0;
for(i=1;i<=limit;i++)
{
s=s+i;
sum=sum+s;
}
System.out.println("THE SUM OF THE SERIES UPTO "+" "+limit+" "+"
is"+sum);
}
}
Q15>>1 2 6 24 120
class series_of_fact
{
public static void main(String args[])
{
int fact=1,i;
for(i=1;i<=5;i++)
{
fact=fact*i;
System.out.print(fact+" ");
}
}
}
Q16>> 1!+2!+3!+4!+5!+6!
class sum_of_series_of_fact
{
public static void main(String args[])
{
int fact=1,i,s=0;
for(i=1;i<=6;i++)
{
fact=fact*i;
s+=fact;
}
System.out.println(s+" ");
}
}
class harshads
{
public static void main(int n)
{
int d,s=0,no=n;
while(n>0)
{
d=n%10;
n=n/10;
s=s+d;
}
if(no%s==0)
System.out.println("HARSHADS NO");
else
System.out.println("not HARSHADS NO");
}
}
Q18>> ARMSTRONG NUMBER
class armstrong
{
public static void main(int n)
{
int d,s=0,no=n;
while(n>0)
{
d=n%10;
s+=d*d*d;
n=n/10;
}
if(no==s)
System.out.println("ARMSTRONG NUMBER");
else
System.out.println(" NOT ARMSTRONG NUMBER");
}
}
class palindrome
{
public static void main(int n)
{
int d,r=0,no=n;
while(n>0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
if(no==r)
System.out.println("PALINDROME NUMBER");
else
System.out.println(" NOT PALINDROME NUMBER");
}
}
Q20>> SPY NO
class spy_no
{
public static void main(int n)
{
int p=1,s=0,d;
while(n>0)
{
d=n%10;
s+=d;
p*=d;
n=n/10;
}
if(s==p)
System.out.println("SPY NO");
else
System.out.println("not SPY NO");
}
}
import java.util.Scanner;
}
}
Q22>> ODIUS NO
class odius
{
public static void main(int n)
{
int r,c=0;
while(n>0)
{
r=n%2;
n=n/2;
if(r==1)
c++;
}
if(c%2==1)
System.out.println("Odius no");
else
System.out.println(" not Odius no");
}
}