The document contains 10 C++ programs that demonstrate various programming concepts like arithmetic operators, if-else statements, loops, arrays, etc. Each program is presented with its source code and expected output.
7. 7
// 4. Program to implement if-else statements
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int year;
cout<<"nEnter the year:";
cin>>year;
if(year%4==0)
cout<<"nIt is a leap year";
else
cout<<"Not a leap year";
getch();
}
13. // 7. Program to implement while loop and it computes a power of b
13
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float a;
int b;
float pow;
cout<<"nEnter the values : ";
cin>>a>>b;
pow=1.0;
while(b>=1)
{
pow=pow*a;
b--;
}
cout<<"Power : "<<pow;
getch();
}
15. // 8. Program to implement do while and it computes the factorial of a number
15
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num,fact=1;
cout<<"nEnter the numnber : ";
cin>>num;
do
{
fact=fact*num;
num--;
}
while(num!=0);
cout<<"nFactorial :"<<fact;
getch();
}
17. 17
// 9. Program to display an array
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5],i;
cout<<"nEnter the elements in an array :n";
for(i=0;i<5;i++)
{
cin>>a[i];
}
cout<<"nDisplayed array : n";
for(i=0;i<5;i++)
{
cout<<"n"<<a[i];
}
getch();
}
19. // 10. Program to display multiples of a number using for loop
19
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,num;
cout<<"nEnter the number :";
cin>>num;
cout<<"The multiples are :";
for(i=1;i<=10;i++)
{
cout<<"n"<<num*i;
}
getch();
}