0% found this document useful (0 votes)
133 views21 pages

C++ Programs for Basic Calculations

This document contains code snippets written in C++ and Java that demonstrate the use of conditional statements like if-else, switch case, and goto statements. It includes programs to check even/odd numbers, calculate income tax based on salary range, find the greatest of three numbers, check leap years, and more. The C++ programs use if-else, switch case and goto statements while the Java programs follow the standard class-main structure and perform basic calculations like finding the sum of two numbers or area of a rectangle.

Uploaded by

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

C++ Programs for Basic Calculations

This document contains code snippets written in C++ and Java that demonstrate the use of conditional statements like if-else, switch case, and goto statements. It includes programs to check even/odd numbers, calculate income tax based on salary range, find the greatest of three numbers, check leap years, and more. The C++ programs use if-else, switch case and goto statements while the Java programs follow the standard class-main structure and perform basic calculations like finding the sum of two numbers or area of a rectangle.

Uploaded by

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

C++ programme using if statement.

Write a prom to test whether the given nu. Is even or odd


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int var;
cout<<enter any nu;
cin>>var;
if(var%2==0)
{
cout<<even;
}
if(var%2!=0)
{
cout<<odd;
}
getch();
}

To compare two [Link] print the greater nu.


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int nu1,nu2;
cout<<enter any nu;
cin>>nu1>>nu2;
if(nu1>nu2)
{
cout<<nu1<<is greater than<<nu2;
}
if(nu2>nu1)
{
cout<<nu2<<is greater than<<nu2;
}
getch();
}

To calculate income tax according to following table:

Salary range

Income Tax

Less than Rs 5,000

Nil

5,000-10,000

5%

10,001-50,000

10%

50,001-1,50,000
More than 1,50, 000

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sal;
cout<<input your salary;
cin>>sal;
if(sal<5,000)
{
cout<<income tax is=<<nil;
}
If((sal>5,000)&&(sal<=10,000))
{
cout<<tax is=<<(sal*5/100);
}

15%
20%

if((sal>10000)&&(sal<=50000))
{
cout<<tax is= (sal*10/100);
}
if((sal>50001)&&(sal<=150000))
{
cout<<tax is =<<(sal*15/100);
}
if(sal>1,50,000)
{
cout<<tax is =<<(sal*20/100);
}
getch();
}

C++ programme using if else statement.


WAP to input the age of the user and check wheather the
user is valid to vote.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int age;
cout<<enter your age;
cin>>age;
if(age>18)
{
cout<<you can vote;
}
else
{
cout<<you cannot vote;
}
getch();
}

WAP to compare three numbers and display the greatest


number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<enter three number;
cin>>a>>b>>c;
if(a>b)&&(a>c)
{
cout<<greatest number is<<a;
}
else
{
if(b>c)
{
cout<<greatest number is<<b;
}
else
{
cout<<greatest number is<<c;

}
}
getch();
}
WAP to check wheather a year is leap year or not.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int year;
cout<<enter the year is;
cin>>year;
if((year%4==0)&&(year%100!=0))
{
cout<<leap year;
}
else
{
cout<<not a leap year;
}
getch();
}

WAP to input three sides of triangle and ascertion


wheather these three sides will form a triangle or not.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<enter three sides of triangle;
cin>>a>>b>>c;
if((b+c)>a&&(a+c)>b)
{
cout<<triangle will form;
}
else
{
cout<<no triangle will form;
}
getch();
}

WAP to convert temperature to farenhite or celcious as


per users choice.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int ch;
float f,c;
cout<<input 1 to convert celcious to farenhite or 2 to convert
farenhite to celcious;
cin>>ch;
if(ch==1)
{
cout<<input temperature in celcious;
cin>>c;
f=9/5*(c+32);
cout<<temperature in farenhite=<<f;
}
else
{
cout<<input temperature in farenhite;
cin>>f;

c=5/9*(f-32);
cout<<temperature in celcious=<<c;
}
getch();
}

WAP to check wheather a number is divided by 13.


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
cout<<input a number;
cin>>n;
if(n%13==0)
{
cout<<number is divided by 13;
}
else
{
cout<<number is not divided by 13;
}
getch();
}

WAP to input three sides of triangle and wheather it is a


right triangle.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<enter three sides of triangle;
cin>>a>>b>>c;
if((a*a==(b*b+c*c))||(b*b==(a*a+c*c))||(c*c==(a*a+b*b)))
{
cout<<it is a right triangle;
}
else
{
cout<<it is not a triangle ;
}
getch();
}

WAP to input a number and display its square if it is less


than 50 else display its cube.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,s,c;
cout<<input a number;
cin>>n;
if(n<50)
{
S=n*n;
cout<<square number is =<<s;
}
else
{
C=n*n*n;
cout<<cube of number is=<<c;
}
getch();
}

C++ programme using switch statement.

WAP to display a menu with options


add,substract,multiply and divide. Do the respective
operations on selection.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int p,q,i;
cout<<enter the number;
cin>>p>>q;
cout<<choose an option from given number;
cout<<[Link];
cout<<[Link];
cout<<[Link];
cout<<4,divide;
cin>>i;
switch(i)
{
Case 1:
cout<<sum is=<<p+q;
break;
Case 2:
cout<<substract is=<<p-q;

break;
Case 3:
cout<<multiply is=<<p*q;
break;
Case 4:
cout<<divide is=<<p/q;
break;
default:
cout<<invalid choice;
break;
}
getch();
}

WAP to read the marital status and display messages


according to the following chart:
Marital Status

Message

Married

Single

Devorce

Widow

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char MS;
switch(i)
{
Case M:
cout<<Married;
break;
Case S:
cout<<Single;
break;
Case D:

cout<<Devorce;
b9reak;
Case w:
cout<<\n widow;
break;
default:
cout<<\n invalid entry;
break;
}
getch();
}

C++ programme using GOTO statement.


WAP to find square root for five numbers.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float x,y;
int count=1;
avg:
cout<<enter a number;
cin>>x;
if(x<0)
{
cout<<invalid number;
}
else
{
Y=sqrt(x);
cout<<square root=<<y;
cout<<end1;
}
Count=count+1;

If(count<=5)
Goto avg;
cout<<finished;
getch();
}

General programming format in java


class class name
{
public static void main(String arg[])
{
.
.
//body of the program
.
.
}//closing of main
}closing of class

Programme in java
WAP to assign two numbers and calculate their sum.
class Number
{
public static void main(String arg[])
{
int a,b,c;
a=20;

b=30;
c=a+b;
[Link](the sum is=);
[Link](c);
}
}
WAP to assign the length and breadth of a rectangle and
calculate its area.
class Rectangle
{
public static void main(String arg[])
{
int l,b,area;
l=20;
b=30;
area=l*b;
[Link](the are of rectangle is=);
[Link](area);
}
}

You might also like