Wednesday 29 January 2020

cpp program to show average of five integers using function .

Source Code: 

#include<iostream>
using namespace std;
void average ()
{
int a,b,c,d,e;
float avg;
avg=(a+b+c+d+e)/5;
cout<<avg<<endl;
}
void sum()
{
int v,w,x,y,z,add;
cout<<"Enter 5 integers: ";
cin>>v>>w>>x>>y>>z;
add=v+w+x+y+z;
cout<<"The sum is: "<<endl;
cout<<add<<endl;
}
int main()
{
sum();
average ();
return 0;
}


Program Output: 
Enter 5 integers: 1
2
3
4
5
The sum is: 
15
The average is: 
5

sum of natural numbers using loop

Source Code:

#include<iostream>
using namespace std;
int main()
{
int i,s,n;
cout<<"Enter one integer: ";
cin>>n;
for(i=1;i<=n;i++)
{
s=n*(n+1)/2;
}
cout<<s;
return 0;
}

Program Output:

sum of all the natural numbers between 1 to 100 using loop

Source Code:

#include<iostream>
using namespace std;
int main()
{
int i,sum;
for(i=1;i<=100;i++)
{
sum+=i;
}
cout<<"Sum = "<<sum;
return 0;
}

Program Output:

simple method of swapping two numbers in cpp using third variable

Source Code:

#include<iostream>
using namespace std;
int main()
{
int temp,x,y;
cout<<"Enter X: ";
cin>>x;
cout<<"Enter Y: ";
cin>>y;
temp=x;
x=y;
y=temp;
cout<<"X = "<<x<<endl;
cout<<"Y = "<<y<<endl;
return 0;

}

 Program Output: 

cpp program to swap values without using third variable

Source Code:

#include<iostream>
using namespace std;
int main()
{
int x,y;
cout<<"Enter X: "<<endl;
cin>>x;
cout<<"Enter Y: "<<endl;
cin>>y;
x=x+y;
y=x-y;
x=x-y;
cout<<"The swapped values are: "<<endl;
cout<<"X = "<<x<<endl;
cout<<"Y = "<<y<<endl;
return 0;
}

Program Output:

cpp program to show average of five integers

Source Code:

#include<iostream>
using namespace std;
int main()
{
int A,B,C,D,E,sum;
float avg;
cout<<"Enter five integers: ";
cin>>A>>B>>C>>D>>E;
sum=A+B+C+D+E;
avg=float(sum)/5;
cout<<sum<<endl;
cout<<avg<<endl;
return 0;
}
                              
Program Output:

Tuesday 28 January 2020

cpp program to find out smallest integer among three integer

Source Code:

#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three integers: "<<endl;
cin>>a>>b>>c;
if(a<b)
{
if(a<c)
{
cout<<"The smallest number is: "<<a;
}
else
{
cout<<"The smallest number is: "<<c;
}
}
else
{
if(b<c)
{
cout<<"The smallest number is: "<<b;
}
else
{
cout<<"The smallest number is: "<<c;
}
}

return 0;

}

Program Output:



cpp program to find out greatest integer using three integer

Source Code:

#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three integers: ";
cin>>a>>b>>c;
if(a>b)
{
if(a>c)
{
cout<<"The greatest number is: "<<a;
}
else
{
cout<<"The greatest number is: "<<c;
}
}

else
{
if(b<c)
{
cout<<"The greatest number is: "<<c;
}
else
                  [
cout<<"The greatest number is: "<<b;
                  ]
}

   return 0;
}

Program Output:
 

cpp program to convert temperature in fahrenheit to celsius

Source Code:

#include<iostream>
using namespace std;
int main()
{
float f,c;
cout<<"Enter the temperature: ";
cin>>f;
c=(f-32)/1.8;
cout<<"The temperature in celsius: "<<c;
return 0;
}

Program Output:

cpp program to convert temperature in celsius to fahrenheit

Source Code:

#include<iostream>
using namespace std;
int main()
{
float f,c;
cout<<"Enter the temperature in celsius: ";
cin>>c;
f=(c*1.8)+32;
cout<<"The temperature in fahrenheit: "<<f;
return 0;

}

Program Output:

cpp program to convert days into months and days

Source Code:

#include<iostream>
using namespace std;
int main()
{
int days,month;
cout<<"Enter the number of days: ";
cin>>days;
month=days/30;
days=days%30;
cout<<"Months = "<<month<<endl;
cout<<"Days = "<<days<<endl;
return 0;
}

Program Output:

Saturday 25 January 2020

Multiplication table of a positive number in CPP

Source code:
#include<iostream>
using namespace std;
int main()
{
int x;
cout<<"Enter a positive integer: ";
cin>>x;
for(int z=1;z<=10;z++)
{
cout<<x<<"*"<<z<<"="<<x*z<<endl;
}
    return 0;
}

Program output;

Friday 24 January 2020

Simple method to sum of two numbers in CPP

Source Code -
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter two numbers: ";
cin>>a>>b;
c=a+b;
cout<<"The sum of the numbers is: ";
cout<<c;
return 0;
}

Program Output -
Enter two numbers: 9
3
The sum of the numbers is: 12

Substraction of two numbers in CPP (Using Function)

Source Code -
#include<iostream>
using namespace std;
void subtract(int a,int b)
{
int c;
c=a-b;
cout<<"The subtraction of the two numbers are: ";
cout<<c;
}

int main()
{
int x,y;
cout<<"Enter two numbers: ";
cin>>x>>y;
subtract(x,y);
return 0;

}

Program Output -
Enter two numbers: 9
5
The subtraction of the two numbers are: 4

Thursday 23 January 2020

Simple method to subtract of two numbers in CPP

Source Code:
#include<iostream>
using namespace std;
void subtract(int a,int b)
{
int c;
c=a-b;
cout<<"The subtraction of the two numbers are: ";
cout<<c;
}

int main()
{
int x,y;
cout<<"Enter two numbers: ";
cin>>x>>y;
subtract(x,y);
return 0;

}


Program Output :
Enter two numbers: 9
2

The subtraction of the two numbers are: 7

Simple method to multiply two numbers in CPP

Source Code:
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter two numbers: ";
cin>>a>>b;
c=a*b;
cout<<"The product of two numbers is: ";
cout<<c;
return 0;
}

Program Output:
Enter two numbers: 5
6

The product of two numbers is: 30

Multiplication of two numbers in CPP (Using Function)

Source Code :
#include<iostream>
using namespace std;
void multiplication(int x,int y)
{
int z;
z=x*y;
cout<<"The multiplication of the two numbers is: ";
cout<<z;
}

int main()
{
int a,b;
cout<<"Enter two numbers: ";
cin>>a>>b;
multiplication(a,b);
return 0;
}


Program Output :
Enter two numbers: 5
7

The multiplication of the two numbers is: 35

Sum of two numbers in CPP (Using Function)

Source Code :
#include<iostream>
using namespace std;
void add(int a,int b)
{
int c;
c=a+b;
cout<<"The sum of the numbers are: ";
cout<<c;
 }

 int main()
 {
  int x ,y;
  cout<<"Enter two numbers: ";
  cin>>x>>y;
  add(x,y);
  return 0;
 }

Program Output :
Enter two numbers: 5
6

The sum of the numbers are: 11

Wednesday 22 January 2020

Program to check odd or even number in CPP

Source Code
#include<iostream>
#include<conio.h>
using namespace std ;
int main()
{
int n;
cout<<"Enter a number : ";
    cin>>n;
if(n%2==0)
{
cout<<"The number is even";
}
else
{
cout<<"The number is odd";
}
return 0;

}

Output
Enter a number : 5
The number is odd

Simple Hello World program in CPP

Source Code

#include<iostream>
using namespace std;
int main()
{
cout<<"Hello World";
return 0;
}


Output
Hello World