Basics of CPP Programs

In this post you will learn fundamental C++ programs. These programs for freshers or beginners.

1 .write a cpp program to display hello world message
#include<iostream.h>
int main()
{
count<<"Hello World\n";
//prints Hello World on user screen
return 0;
}

Output:

Hello World

2. cpp program to use cout and cin

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();

int a,b,s;

cout<<"enter first num:";
cin>>a;
cout<<"enter second num:";
cin>>b;

s=a+b;

cout<<"\n sum= ";
cout<<s;

getch();
}

output:
Enter first Num:10
Enter second Num:20
sum=30

3. write a cpp program to use variables

#include<iostream.h>
using namespace std;

int main()
{
//defination of variables
int a,b,c;
float d;

//initialization of variables

a=5;
b=10;
c=a+b;

cout<<c<endl;

d=15.0/4.0;
cout<<d<<endl;

return 0;
}

output:
15
3.75

4 CPP program to swap two numbers using thrid variable

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();

int a,b,temp;

cout<<"\n enter two numbers :";
cin>>a>>b;

cout<<"\n a="<<a<<",b="<<b;

temp=a;
a=b;
b=temp;

cout<<"\n a=" <<a",b="<<b;

getch();
}
Output:
enter two numbers: 10 20
a=10,b=20
a=20,b=10

5.CPP program to swap two numbers without thrid variable
#include<iostream.h>
#include<conio.h>

void main()
{


int a,b;
clrscr();

cout<<"enter the value of a and b:"<<endl;
cin>>a>>b;

cout<<"before swap:\n"<<"A:"<<a<<"\n B:<<b<<endl;
a=a+b;
b=a-b;
a=a-b;
cout<<"after swap:\n"<<"A:"<<a<<"\n B:<<b<<endl;

getch();
}
Output:
Enter the value of a and b:
10 20
before swap:
A:10
B:20
after swap:
A:20
B:10


No comments:

Post a Comment

Basics of CPP Programs

In this post you will learn fundamental C++ programs. These programs for freshers or beginners. 1 .write a cpp program to display hello w...