0% found this document useful (0 votes)
19 views9 pages

C++ Programs by

The document contains C++ code examples demonstrating basic programming concepts like Hello World, addition, averages, tables, percentages, loops, if/else statements, arrays, and a bill calculation program. All code examples include standard header files and use functions like cout, cin, getch() to output text and accept user input.

Uploaded by

M Noaman Akbar
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)
19 views9 pages

C++ Programs by

The document contains C++ code examples demonstrating basic programming concepts like Hello World, addition, averages, tables, percentages, loops, if/else statements, arrays, and a bill calculation program. All code examples include standard header files and use functions like cout, cin, getch() to output text and accept user input.

Uploaded by

M Noaman Akbar
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
You are on page 1/ 9

C++ Programs

By

Muhammad Saleem Akhtar


[email protected]

MUL
Minhaj University Lahore

Hello World
#include<iostream.h>
#include<conio.h>
main()
{
cout<<"Hello World";
getch();
}

Addition
#include<iostream.h>
#include<conio.h>
main()
{
int a = 2, b = 3;
int sum;
sum = a+b ;
cout<<"The sum is: \t"<<sum;
getch();
}

Average
#include<iostream.h>
#include<conio.h>
main()
{
int a = 4,b = 3,c = 10;
int avg;
avg = (a+b+c)/3;
cout<<"The the Average is: \t"<< avg;
getch();
}

Table
#include<iostream.h>
#include<conio.h>
main()
{
int number = 2;
for(int i=1 ; i<=10 ; i++)
cout<<number<<" x "<<i<<" = "<<number * i<<endl;
getch();
}

Percentage
#include<iostream.h>
#include<conio.h>
main()
{
float a,b;
cout<<"\nEnter number of a student:\t";
cin>>a;
b = ((a/1100)*100);
cout<<"\nPercentage of "<<a<<" out of 1100 is "<<b<<"%";
getch();
}

Star Program
#include<iostream.h>
main()
{
for(int i = 1; i<=5 ; i++)
{
for(int j = 1; j<=i ; j++)
cout<<"*";
cout<<endl;
}
}

IF Statement
#include<iostream.h>
#include<conio.h>
main()
{
int a , b ;
cout<<"Enter 1st value: ";
cin>> a;
cout<<"Enter 2nd value: ";
cin>> b;
if(a > b)
cout<<"1st is greater";
else
cout<<"2nd is greater";
getch();
}

Even Values
#include<iostream.h>
#include<conio.h>
main()
{
for(int i = 1; i<=20 ; i++)
{
if( i % 2 == 0 )
cout<<i<<"\n";
}
}

Odd Values
#include<iostream.h>
#include<conio.h>
main()
{
for(int i = 1; i<=20 ; i++)
{
if( i % 2 != 0 )
cout<<i<<"\n";
}
}

Square Program
#include<iostream.h>
main()
{
int a;
cout<<"Enter a mumber: ";
cin>>a;
cout<<"The square of "<<a<<" is "<<a*a;
}

Nester If
#include<iostream.h>
main()
{
int a = 100, b = 200;
if( a >= 50)
{
if( a >= b)
cout<<"A is geater";
else
cout<<"B is greater";
}
}

Array Declaration & Average


#include<iostream.h>
#include<conio.h>
main()
{
int array[5] = {10,20,30,40,50};
int sum = 0 , avg = 0;
for(int i = 0; i<5 ; i++)
{
sum += array[i];
}
avg = sum/5;
cout<<"The sum of all arrays is: "<<sum<<endl;
cout<<"The avg of sum of array is: "<<avg;
getch();
}

Bill Program
#include<iostream.h>
#include<conio.h>
main()
{
int unit = 0, bill = 0;
cout<<"Enter the Units: ";
cin>>unit;
if( unit > 100 )
bill = unit * 3;
else if( unit > 200 )
bill = unit * 5;
else
bill = unit * 1;
cout<<"The bill is: "<<bill;
getch();
}

You might also like