0% found this document useful (0 votes)
10 views

Sample C - Programming

The document contains sample C++ programs to add first three natural numbers, calculate perimeter and area of a rectangle, calculate sum of interest, convert temperature from Celsius to Fahrenheit, calculate DA, HRA, gross pay from net pay, check if a number is positive, negative or zero, find the maximum of two numbers, calculate a quadratic formula, and display student name, total and average marks.

Uploaded by

amitkumarbera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Sample C - Programming

The document contains sample C++ programs to add first three natural numbers, calculate perimeter and area of a rectangle, calculate sum of interest, convert temperature from Celsius to Fahrenheit, calculate DA, HRA, gross pay from net pay, check if a number is positive, negative or zero, find the maximum of two numbers, calculate a quadratic formula, and display student name, total and average marks.

Uploaded by

amitkumarbera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SAMPLE C++ PROGRAMS

Write a C++ Program which will add first three natural number
and print the result
/*This Program will add first three natural number and print the result*/

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

void main()
{
int a=1,b=2,c=3;
int result=a+b+c;
cout<<"The addition of first three natural number is = "<<result;
getche();
}

Write a C++ Program which will calculate the Perimeter and Area
of a Rectangle

*This Program will calculate the Perimeter and Area of a Rectangle*/


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

void main()
{
int length, breadth, perimeter, area;
cout<<"Please enter the length of the Recatangle";
cin>>length;
cout<<"Please enter the breadth of the Recatangle";
cin>>breadth;
perimeter=2*(length+breadth);
area=length*breadth;
cout<<"Perimeter of the Rectangle is"<<perimeter;
cout<<"Area of the Rectangle is"<<area;
getche();
}
SAMPLE C++ PROGRAMS

Write a C++ Program which will calculate the sum of interest of a principle
/*This Program will calculate the sum of interest of a principle*/

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

void main()
{
float p,roi,t;
float soi;
cout<<"Please enter the principle amount";
cin>>p;
cout<<"Please enter the rate of interest";
cin>>roi;
cout<<"Please enter the time";
cin>>t;
soi=(p*roi*t)/100;
cout<<"The sum of Interest is"<<soi;
getche();
}

Write a C++ Program which will convert a temperature from


Celsius to Fahrenheit
/*This Program will convert a temperature from Celsius to Fahrenheit*/

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

void main()
{
int cel;
int fa;
cout<<"Please enter the temperature in Celsius";
cin>>cel;
fa=(9*cel+160)/5;
cout<<"After converting in to Fahrenheit the result is"<<fa;
getche();
}
SAMPLE C++ PROGRAMS

Write a C++ Program which will calculate and display


DA, HRA, Medical Allowance and Gross Pay from Net Pay given by the User

DA = 30% of Net Pay, HRA = 10% of Net Pay, Medical Allowance = 1500

Gross Pay = Net Pay + DA + HRA + Medical Allowance

/* This Program will take the input from user and calculate DA, HRA, Medical
Allowance and Gross Pay*/

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

void main()
{
double netp, da, hra, med_all, gpay;
cout<<"Please enter the Net Pay";
cin>>netp;
da=(netp*30)/100;
hra=(netp*10)/100;
med_all=1500;
gpay=netp+da+hra+med_all;
cout<<"Net Pay"<<netp;
cout<<"DA"<<da;
cout<<"HRA"<<hra;
cout<<"Medical Allowance"<<med_all;
cout<<"Gross Pay"<<gpay;
getche();
}

Write a C++ Program which will check a number positive, negative or zero

/*This program will check a number positive, negative or zero*/

#include"iostream.h"
#include"conio.h"
void main()
{
int number;
cout<<"Please enter the number";
cin>>number;
if(number>0)
cout<<"It is a Positive number";
if(number<0)
cout<<"It is a negative number";
if(number==0)
cout<<"The number is a Zero";
getche();
}

Write a C++ Program which will check a maximum number from 2 numbers
when both the numbers are not same
/*This program will find out maximum number from 2 numbers*/

#include"iostream.h"
#include"conio.h"

void main()
{
int a,b;
cout<<"Please insert the first number";
cin>>a;
cout<<"Please insert the second number";
cin>>b;
if(a>b)
cout<<"First number is bigger than Second number";
if(b>a)
cout<<"Second number is bigger than First number";
getche();
}
Write a C++ Program which will calculate and display
the final value of the formula a2+2ab+b2
/*This program will calculate the value of the formula*/

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

void main()
{
int a,b,result;
cout<<"Enter the first number as a";
cin>>a;
cout<<"Enter the first number as b";
cin>>b;
result=a*a+2*a*b+b*b;
cout<<"The final result is"<<result;
getche();
}

Write a C++ Program which will enter the names of a student and marks
obtained in three subjects English, Math, Science. Display the name, total
marks and average

*This program will display name, total marks and average of a student*/

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

void main()
{
char ch[100];
int eng, mat, sc;
float grand_total, average;

cout<<"Enter the name of the student";


cin.get(ch,100);
cout<<"Enter the marks of English";
cin>>eng;
cout<<"Enter the marks of Math";
cin>>mat;
cout<<"Enter the marks of Science";
cin>>sc;

grand_total=eng+mat+sc;
average=grand_total/3;
cout<<"Name of the Student"<<ch;
cout<<"Total Marks"<<grand_total;
cout<<"Avearge Marks"<<average;
getche();
}

You might also like