/* *******************************************
1. Program to print welcome in c++
********************************************** */
#include
#include
void main()
{
clrscr();
cout<<"WELCOME";
getch();
}
OUTPUT
WELCOME
/************************************************************
2. To print square and cube of first 10 natural nos
************************************************************* */
#include
#include
void main()
{int i,s,c;
clrscr();
cout<<" Square Cube\n";
for(i=1;i<=10;i++)
{s=i*i;
c=i*i*i;
cout< "<
}
getch();
}
OUTPUT
Square Cube
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
/*********************************************
3.Program to convert inches to cm
***********************************************/
#include
#include
void main()
{ float inch, cm;
clrscr();
cout<<"Enter the length in inches : ";
cin>>inch;
cm = 2.54 * inch;
cout<
getch();
}
OUTPUT
Enter the length in inches : 34
12inches = 86.36 cm
/*******************************************************
04. To find given no. is even or odd
********************************************************/
#include
#include
int main()
{
int n,r;
clrscr();
cout<<"Enter any number"<
cin>>n;
r=n%2;
if(r==0)
{
cout<<"The number is even "<
}
else
{
cout<<"The number is odd"<
}
return 0;
}
OUTPUT:
Enter any number
34
The number is even
/*********************************************************
5. To check if a number is +ve, -ve or zero
**********************************************************/
#include
#include
void main()
{int n;
clrscr();
cout<<"Enter the number \n";
cin>>n;
if(n>0)
cout<
else
if(n<0)
cout<
else
cout<
getch();
}
OUTPUT
Enter the number
-34
-34 is less than 0
/*********************************************************
6. To print 1st n natural numbers
**********************************************************/
#include
#include
void main()
{int n;
clrscr();
cout<<"Enter the Limit \n";
cin>>n;
for(int i=1;i<=n;i++)
cout<
getch();
}
OUTPUT
Enter the Limit
12
1
2
3
4
5
6
7
8
9
10
11
12
/*************************************************************
7. To print sum of 1st n natural numbers
**************************************************************/
#include
#include
void main()
{int n,sum=0;
clrscr();
cout<<"Enter the Limit \n";
cin>>n;
for(int i=1;i<=n;i++)
{sum+=i;
}
cout<<"Sum of first "<
getch();
}
OUTPUT
Enter the Limit
4
Sum of first 4 natural numbers = 10
/********************************************************
8.prg to print sum of any given n numbers
*********************************************************/
#include
#include
void main()
{int n,no,sum=0;
clrscr();
cout<<"Enter the Limit \n";
cin>>n;
cout<<"Enter "<
for(int i=1;i<=n;i++)
{cin>>no;
sum+=no;
}
cout<<"Sum = "<
getch();
}
OUTPUT
Enter the Limit
4
Enter 4 numbers
7
20
3
4
Sum = 34
/******************************************************
9. To find factorial of a number
*******************************************************/
#include
#include
void main()
{int n,fact=1;
clrscr();
cout<<"Enter the Number to find factorial \n";
cin>>n;
for(int i=1;i<=n;i++)
{fact*=i;
}
cout<<"Factorial of "<
getch();
}
OUTPUT
Enter the Number to find factorial
5
Factorial of 5 = 120
/******************************************************
10. To find sum of digits of a number
*******************************************************/
#include
#include
void main()
{
int n,r,q,sum=0;
clrscr();
cout<<" enter any number"<
cin>>n;
while(n>0)
{
r=n%10;
sum=sum+r;
q=n/10;
n=q;
}
cout<<"sum of digits ="<
getch();
}
Output:
enter any number
456
sum of digits =15
0 comments:
Post a Comment