basic programs of c++

22
// 1. Program to implement arithmetic operators #include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b,sum,sub,mul,div; cout<<"\nenter the numbers : "; cin>>a>>b; sum=a+b; sub=a-b; mul=a*b; div=a/b; cout<<"\nSum:"<<sum; cout<<"\nSubtraction:"<<sub; cout<<"\nMultipication:"<<mul; cout<<"\nDivision:"<<div; getch(); } 1

Upload: bharat-kalia

Post on 15-Jun-2015

169 views

Category:

Education


3 download

DESCRIPTION

Basic Programs of C++ includes loops and conditional statement and basics

TRANSCRIPT

Page 1: Basic Programs of C++

// 1. Program to implement arithmetic operators

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int a,b,sum,sub,mul,div;

cout<<"\nenter the numbers : ";

cin>>a>>b;

sum=a+b;

sub=a-b;

mul=a*b;

div=a/b;

cout<<"\nSum:"<<sum;

cout<<"\nSubtraction:"<<sub;

cout<<"\nMultipication:"<<mul;

cout<<"\nDivision:"<<div;

getch();

}

1

Page 2: Basic Programs of C++

Output :

2

Page 3: Basic Programs of C++

// 2. Program to calculate simple interest

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int principle,rate,time,si;

cout<<"\nEnter the principle : ";

cin>>principle;

cout<<"\nEnter the rate : ";

cin>>rate;

cout<<"\nEnter the time :";

cout<<interest;

si=(principle*rate*time)/100;

cout<<"\nSimple Interest :"<<si;

getch();

}

3

Page 4: Basic Programs of C++

Output :

4

Page 5: Basic Programs of C++

// 3. Program to implement increment and decrement operators

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int x=5,y=5;

cout<<x++;

cout<<"\n";

cout<<++x;

cout<<"\n";

cout<<y++;

cout<<"\n";

cout<<++y;

getch();

}

5

Page 6: Basic Programs of C++

Output :

6

Page 7: Basic Programs of C++

// 4. Program to implement if-else statements

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int year;

cout<<"\nEnter the year:";

cin>>year;

if(year%4==0)

cout<<"\nIt is a leap year";

else

cout<<"Not a leap year";

getch();

}

7

Page 8: Basic Programs of C++

Output :

8

Page 9: Basic Programs of C++

// 5. Program to implement if-else if ladder statement

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

float sal;

float tax;

cout<<"\nEnter the salary : ";

cin>>sal;

if(sal>=9000)

tax=sal*0.4;

else if((sal>=7500)&&(sal<8999))

tax=sal*0.3;

else

tax=sal*0.2;

cout<<"Tax:"<<tax;

getch();

}

9

Page 10: Basic Programs of C++

Output :

10

Page 11: Basic Programs of C++

// 6. Program to implement switch case statement

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int ch;

cout<<"\nEnter your choice : ";

cin>>ch;

switch(ch)

{

case 1 : cout<<"\nCase 1 : Good Morning";

break;

case 2 : cout<<"\nCase 2 : Good Afternoon";

break;

case 3 : cout<<"\nCase 3 : Good Evening";

break;

case 4 : cout<<"\nCase 4 : Good Night";

break;

deafult: cout<<"\nWrong choice";

}

getch();

}

11

Page 12: Basic Programs of C++

Output :

12

Page 13: Basic Programs of C++

// 7. Program to implement while loop and it computes a power of b

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

float a;

int b;

float pow;

cout<<"\nEnter the values : ";

cin>>a>>b;

pow=1.0;

while(b>=1)

{

pow=pow*a;

b--;

}

cout<<"Power : "<<pow;

getch();

}

13

Page 14: Basic Programs of C++

Output :

14

Page 15: Basic Programs of C++

// 8. Program to implement do while and it computes the factorial of a number

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int num,fact=1;

cout<<"\nEnter the numnber : ";

cin>>num;

do

{

fact=fact*num;

num--;

}

while(num!=0);

cout<<"\nFactorial :"<<fact;

getch();

}

15

Page 16: Basic Programs of C++

Output :

16

Page 17: Basic Programs of C++

// 9. Program to display an array

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int a[5],i;

cout<<"\nEnter the elements in an array :\n";

for(i=0;i<5;i++)

{

cin>>a[i];

}

cout<<"\nDisplayed array : \n";

for(i=0;i<5;i++)

{

cout<<"\n"<<a[i];

}

getch();

}

17

Page 18: Basic Programs of C++

Output :

18

Page 19: Basic Programs of C++

// 10. Program to display multiples of a number using for loop

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int i,num;

cout<<"\nEnter the number :";

cin>>num;

cout<<"The multiples are :";

for(i=1;i<=10;i++)

{

cout<<"\n"<<num*i;

}

getch();

}

19

Page 20: Basic Programs of C++

Output :

20