Professional Documents
Culture Documents
Page 1
C++ Programming
Programs
1) Write a simple C program to display “Hello World” message on the
screen.
#include <iostream>
using namespace std;
int main()
{
// prints hello world
cout << "Hello World";
return 0;
}
Page 2
C++ Programming
Page 3
C++ Programming
Page 4
C++ Programming
int main()
cout << "\nThe Square of the Given " << number << " = " << square;
return 0;
Page 5
C++ Programming
#include<iostream>
using namespace std;
int main()
{
int length, area, peri;
cout<<"Enter the length of square = ";
cin>>length;
area=length*length;
peri=4*length;
cout<<"area = "<<area<<"\n";
cout<<"perimeter= "<<peri;
return 0;
}
Page 6
C++ Programming
8) Write a program to read given two numbers from user and perform arithmetic
operations such as addition, subtraction, multiplication, division and modulus
division and display results of respective.
#include<iostream>
using namespace std;
int main()
{
int x,y;
cout<<"enter fist value = ";
cin>>x;
cout<<"enter second value = ";
cin>>y;
int sum=x+y;
int sub=x-y;
int mul=x*y;
float div=(float)x/y;
float mod=x%y;
cout<<"The Summation is = "<<sum;
cout<<"\nThe Subtraction is = "<<sub;
cout<<"\nThe Multiplication is = "<<mul;
cout<<"\nThe Division is = "<<div;
cout<<"\nThe Modulus is = "<<mod;
return 0;
}
Page 7
C++ Programming
Page 8
C++ Programming
Page 9
C++ Programming
Page 10
C++ Programming
14) Write a program to display student’s grade “Pass” if marks is greater than or
equal to 40 and display “Fail” if marks is less than 40.
#include <iostream>
using namespace std;
int main()
{
int marks;
cout<<"enter marks "<<endl;
cin>>marks;
if(marks>=40)
{
cout<<"Pass "<<endl;
}
else
{
cout<<"fail";
}
return 0;
}
Page 11
C++ Programming
Page 12
C++ Programming
#include <iostream>
int main() {
int number;
return 0;
Page 13
C++ Programming
ii) To calculate area of circle if given radius value is not equal to zero.
#include <iostream>
int main() {
if(radius>0)
return 0;
Page 14
C++ Programming
return 0;
}
Page 15
C++ Programming
ii) To check that user entered number is positive or negative and accordingly
display messages.
#include<iostream>
using namespace std;
int main ()
{
int num;
cout << "Enter the number to be checked : ";
cin >> num;
if (num >= 0)
cout << num << " is a positive number.";
else
cout << num << " is a negative number.";
return 0;
}
Page 16