0% found this document useful (0 votes)
5 views12 pages

Java Programs for Basic Calculations

Uploaded by

worldidol0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views12 pages

Java Programs for Basic Calculations

Uploaded by

worldidol0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

*;
class Prog1
{
static void main()
{
Scanner sc=new Scanner([Link]);
int n,d,s=0,rev=0;
[Link]("Enter Any Triple digit number ");
n=[Link]();
if(n>=100 && n<=999)
{
while(n!=0)
{
d=n%10;
s=s+d;
rev=rev*10+d;
n=n/10;
}
[Link]("Total of the digits : "+s);
[Link]("Reverse of the number :"+rev);
}
}
}

import [Link].*;
class Prog2
{
static void main()
{
Scanner sc=new Scanner([Link]);
int a,b,c,m,s=0;
[Link]("Enter any three different numbers");
a=[Link]();
b=[Link]();
c=[Link]();
m=(a>b)?(a>c?a:c):(b>c?b:c);

while(a>=10)
a=a/10;
while (b>=10)
b=b/10;
while (c>=10)
c=c/10;

s=a+b+c;
[Link]("Greater number is "+m);
[Link]("Sum of first digit of three numbers "+s);
}
}
import [Link].*;
class Prog3
{
static void main()
{
Scanner sc=new Scanner([Link]);
double feet, yard,inch;
[Link]("Enter measurement in feet ");
feet=[Link]();
yard=feet/3.0;
inch=feet*12;
[Link](feet+" feet = "+yard +" yard");
[Link](feet+" feet = "+inch+ " inch");
}
}

import [Link].*;
class Prog4
{
static void main()
{
Scanner sc=new Scanner([Link]);
int a,b,c,s=0;
[Link]("Enter any three different numbers");
a=[Link]();
b=[Link]();
c=[Link]();
s=(a)-(-b)-(-c);
[Link]("Sum is= "+s);
}
}
import [Link].*;
class Prog5
{
static void main()
{
Scanner sc=new Scanner([Link]);
int a,b,c;
int ch;
[Link]("Enter any two numbers :");
a=[Link]();
b=[Link]();
[Link]("----------MENU ----------");
[Link]("Press 1. For Addition ");
[Link]("Press 2. For Substraction ");
[Link]("Press 3. For Multiplication ");
[Link]("Press 4. For Division ");
[Link]("Enter your choice :");
ch=[Link]();
switch(ch)
{
case 1: c=a+b;
[Link]("Addition of two numbers is "+c);
break;
case 2: c=a-b;
[Link]("Substraction of two numbers is "+c);
break;
case 3: c=a*b;
[Link]("Multiplication of two numbers is "+c);
break;
case 4: c=a/b;
[Link]("Division of the two number is: "+c);
break;
default: [Link]("WRONG CHOICE INPUT");
}
}
}
import [Link].*;
class Prog6
{
static void main()
{
Scanner sc=new Scanner([Link]);
int k;
double fare=0;
[Link]("Enter Kilometers travelled");
k=[Link]();
if (k>0 && k<=5)
{
fare= 10*k;
}
else if (k>5 && k<=15)
{
fare= (5*10) + ((k-5)*8);
}
else if (k>15 && k<=25)
{
fare= (5*10) + (10*8) + ((k-15)*7);
}
else if (k>25)
{
fare= (5*10) + (10*8) + (10*7) + ((k-25)*5);
}
[Link]("Bus Fare is "+ fare);
}
}
import [Link].*;
class Prog7
{
static void main()
{
Scanner sc=new Scanner([Link]);
int n,d,rev=0,t;
[Link]("Enter Any number ");
n=[Link]();
t=n;
while(n!=0)
{
d=n%10;
rev=rev*10+d;
n=n/10;
}
if(t==rev)
{
[Link](t+" is Palindrome Number");
}
else
{
[Link](t+" is not Palindrome Number");
}
}
}
import [Link].*;
class Prog8
{
static void main()
{
Scanner sc=new Scanner([Link]);
double ar=0,l,b,s,r;
int ch;
[Link]("----------MENU ----------");
[Link]("Press 1. To find area of a Square ");
[Link]("Press 2. To find area of a Rectangle ");
[Link]("Press 3. To find area of a Circle ");
[Link]("Enter your choice :");
ch=[Link]();
switch(ch)
{
case 1: [Link]("Enter the Side of the Square ");
s= [Link]();
ar= s*s;
[Link]("Area of Square = "+ar);
break;
case 2: [Link]("Enter the Length and Breadth of the Rectangle ");
l= [Link]();
b= [Link]();
ar= l*b;
[Link]("Area of Rectangle = "+ar);
break;
case 3: [Link]("Enter the Radius of the Circle ");
r= [Link]();
ar= 3.14159*(r*r);
[Link]("Area of Circle = "+ar);
break;
default: [Link]("WRONG CHOICE INPUT");
}
}
}
import [Link].*;
class Prog9
{
static void main()
{
Scanner sc=new Scanner([Link]);
double c,f;
[Link]("Enter Temperature in Celsius ");
c=[Link]();
f=(1.8 * c) + 32;
[Link]("Temperature in Fahrenheit is "+f);
}
}

import [Link].*;
class Prog10
{
static void main()
{
Scanner sc=new Scanner([Link]);
int a,b,c;
[Link]("Enter 3 sides of a Triangle ");
a=[Link]();
b=[Link]();
c=[Link]();
if(a==b && b==c && c==a)
{
[Link]("Triangle is Equilateral Triangle");
}
else if (a==b || b==c || c==a)
{
[Link]("Triangle is Isosceles Triangle");
}
else if (a!=b && b!=c && c!=a)
{
[Link]("Triangle is Scalene Triangle");
}
}
}
import [Link].*;
class Prog11
{
static void main()
{
Scanner sc=new Scanner([Link]);
int n,i,even=0,odd=0;
[Link]("Enter any 10 numbers");
for(i=1;i<=10;i++)
{
n=[Link]();
if(n%2==0)
{
even=even+n;
}
else if (n%2!=0)
{
odd=odd+n;
}
}
[Link]("Total of all Even numbers "+even);
[Link]("Total of all Even numbers "+odd);
}
}

class Prog12
{
static void main()
{
int i,a=0,b=1,c;
[Link]("First 10 Fibonacci Series are: "+a+" "+b);
for(i=1;i<=8;i++)
{
c=a+b;
[Link](" "+c);
a=b;
b=c;

}
}
}
import [Link].*;
class Prog13
{
static void main()
{
Scanner sc=new Scanner([Link]);
int a,b,x,i,hcf=0,lcm=0,ch;
[Link]("Enter Two Numbers ");
a=[Link]();
b=[Link]();
x=a>b?a:b;
for(i=x;i>=1;i--)
{
if(a%i==0 && b%i==0)
{
hcf=i;
break;
}
}
lcm=a*b/hcf;
[Link]("Press 1. to get L.C.M.");
[Link]("Press 2. to get H.C.F.");
[Link]("Enter your choice: ");
ch=[Link]();
switch(ch)
{
case 1: [Link]("L.C.M. is "+lcm);
break;
case 2: [Link]("H.C.M. is "+hcf);
break;
default: [Link]("Enter 1 or 2 only");
}
}
}
import [Link].*;
class Prog14
{
static void main()
{
Scanner sc=new Scanner([Link]);
int n,d,c=0,t;
[Link]("Enter any number ");
n=[Link]();
t=n;
while(n>0)
{
d=n%10;
if(d==0)
{
c++;
}
n=n/10;
}
if(c>0)
{
[Link](t+" is Duck number");
}
else
{
[Link](t+" is not Duck number");
}
}
}
import [Link].*;
class Prog15
{
static void main()
{
Scanner sc=new Scanner([Link]);
int n;
[Link]("Enter any number ");
n=[Link]();
if(n%10==7 || n%7==0)
{
[Link](n+" is Buzz number");
}
else
{
[Link](n+" is not Buzz number");
}
}
}
Question 16:
(a)
class Pattern1
{
static void main()
{
for (int i=1;i<=5;i++)
{
for (int j=1;j<=i;j++)
{
[Link](j+" ");
}
[Link]();
}
}
}

(b)
class Pattern2
{
static void main()
{
for (int i=5;i>=1;i--)
{
for (int j=1;j<=i;j++)
{
[Link]("*"+" ");
}
[Link](i);
}
}
}

(c)
class Pattern3
{
static void main()
{
for (int i=5;i>=1;i--)
{
for (int j=1;j<=i;j++)
{
[Link]((j*j)+" ");
}
[Link]();
}
}
}

You might also like