You are on page 1of 27

1. Write a Program to display ASCII code of character.

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );

char a;
int b;
cout<<”Enter a character”;
cin>>a;
b=a;
cout<<”ASCII code is”<<b;
getch( );
}

OUTPUT-
Enter a character A
ASCII code is 65
2. Write a program to convert a given number of days into years, weeks and
days.

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int d,w,y,e;
cout<<”Enter number of days”<<”\n”;
cin>>d;
y=d/365;
w=(d%365)/7;
e=(d%365)%7;
cout<<”\n”<<y<<”years”,<<w<<”weeks,<<e<<”days”;
getch( );
}

OUTPUT-
Enter number of days 565
1year,28weeks,4days
3. Write a program to input principal, rate and time and calculate SI. If time is
more than 10 years calculate SI with 10% interest otherwise with 8%interest.

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int p,t,sia,sib;
cout<<”Enter Principal and Time”;
cin<<p<<t;
if (t>0)
{sia=(p*t*8)/100;
cout<<”\n”<<SI is<<sia;
}
else
{sib=(p*t*10)/100;
cout<<”\n”<<SI is<<sib;
}
getch( );
}

OUTPUT-
Enter Principal and Time 100
11
SI is 110
4. Write a program to input a number. If the number is even print its square
otherwise print its cube.

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int a;
cout<<”Enter a number”;
cin>>a;
if (a%2= =0)
cout<<a*a;
else
cout<<a*a*a;
}
getch( );
}

OUTPUT-

Enter a number 5
125
5. Write a program to input a number. If it is odd and positive print its square
root otherwise print n5

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int a;
cout<<”Enter a number”;
cin>>a;
if (a>0 && a%2!=0)
cout<<sqrt(a);
else
{cout<<a*a*a*a*a;
}getch( );
}

OUTPUT-
Enter a number 2
32
6. Write a program to input age of a person. If age>=18 print ‘You can vote’
otherwise print ‘You cannot vote’.

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int age;
cout<<”Enter your age \n”;
cin>>age;
(age>=18)?cout<<”You can vote”; : cout <<”You cannot vote”;
getch( );
}

OUTPUT-
Enter your age 17
You cannot vote
7. Write a program for temperature conversion menu that gives the user the
option of converting temperature from Fahrenheit to Celsius or Celsius to
Fahrenheit depending upon the user’s choice.

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int a;
float c,f;
cout<<”Choose your operation\n"
cout<<"1. Celsius to Fahrenheit;
cout<<"2. Fahrenheit to Celsius;
cin>>a;
if (a= =1)
{cout<<"Enter temperature in Celsius \n";
cin>>c;
f=(c*1.8)+32;
cout<<"Temperature in Fahrenheit is"<<f;
}
else
{cout<<"Enter temperature in Fahrenheit
cin>>c;
f=(c-32)/1.8;
cout<<"Temperature in Celsius is<<f;
}
getch( );
}

OUTPUT-
Choose your operation 1
Enter temperature in Celsius 10
Temperature in Fahrenheit is 40
8. Write a program to check whether a given year is a leap year or not.

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int a;
cout<<"Enter year";
cin>>a;
if (a%4==0)
cout<<"It is a leap year"'
else
cout<<"It is not a leap year";
getch( );
}

OUTPUT
Enter year 1992
It is a leap year
9. Write a program to calculate commission for the salesman. The commission
is to be calculated at the following rates
SALES COMMISSION
RATE
30001 onwards 15%
22001-30000 10%
12001-22000 7%
5001-42000 3%
0-5000 0%

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int a,c;
cout<<"Enter sales\n";
cin>>a;
{if(a>=0 && a<=5000)
cout<<"no commission";
}
else if(a>=5001 && a<=12000)
{c=a*0.03;
cout<<"Your commission is"<<c;
}
else if(a>=12001 && a<=22000)
{c=a*0.07;
cout<<"Your commission is"<<c;
}
else if(a>=22001 && a<=30000)
{c=a*0.10;
cout<<"Your commission is"<<c;
}
else if(a>=30001)
{c=a*0.15;
cout<<"Your commission is"<<c;
}
getch( );
}
OUTPUT
Enter Sales 30000
Your commission is 3000
10.Write a program to input a day number and translate into name of day.

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int a;
cout<<"Enter day number";
cin>>a;
switch(a)
{
case1: cout<<"Sunday";
break;
case2: cout<<"Monday";
break;
case3: cout<<"Tuesday ";
break;
case4: cout<<"Wednesday";
break;
case5: cout<<"Thursday";
break;
case6: cout<<"Friday";
break;
case7: cout<<"Saturday";
break;
}
getch( );
}

OUTPUT
Enter day number 1
Sunday
11.Write a program to find factorial of a number

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int num;
long int fact=1;
cout<<"Enter a number";
cin>>num;
while(num>=1)
{
fact=fact*num;
num--;
}
cout <<"Factorial of number entered is"<<fact;
getch( );
}

OUTPUT
Enter a number 5
Factorial of number entered is 120
12.Write a program to print 1 2 4 8 16 32 64 128

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

void main( )
{
clrscr( );
int a=0, b;
while(a<=7)
{b=pow(2,a);
cout<<b;
a++;
}
getch( );
}

OUTPUT
1 2 4 8 16 32 64 126
13.Write a program to generate Fibonacci sequence till 15 elements

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int a=1, i=0, j=1, k;
cout<<I;
cout j;
{
while (a<=13)
k=i+j;
cout<<k;
i-j;
j=k;
i++;
}
getch( );
}

OUTPUT
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
14.Write a program to print all even numbers between 1 and 50 in reverse order

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
i=50;
do
{
cout<<i;
i=i-2
}
while(i>=1)
getch( );
}

OUTPUT
48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2
15.Write a program to print the sum of first 10 natural numbers

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int i=1, sum=0;
do
{
Sum=sum+i;
i++
}while(i<=10)
getch( );
}

OUTPUT
55
16.Write a program to print
1
12
123
1234
12345

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int i, j;
for (i=1;i<=5;i++)
{
cout<<"\n";
for (j=1; j<=5; j++)
{
cout<<j;
}
}
getch( );
}

OUTPUT-
1
12
123
1234
12345
17.Write a program to print
*
**
***
****

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int i, j;
for (i=1;i<=4;i++)
{
cout<<"\n";
for (j=1; j<=1; j++)
{
cout<<"*";
}
}getch( );
}

OUTPUT

*
**
***
****
18.Write a program to print
A
AB
ABC
ABCD

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
char ch='A';
int i, j;
for (i=65;i<=68;i++)
{
cout<<"\n";
for (j=ch; j<=i; j++)
{
cout<<char j;
}
}
getch( );
}

OUTPUT

A
AB
ABC
ABCD
19.Write a program to input a character and change its case

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
char ch;
do
{
cout<<"Enter a character";
cin>>ch;
if(ch= ='\n')
{
ch=getchar();
}
Else
if(ch>=65 && ch<=90)
ch=ch+32;
Else if (ch>=97 && ch<=122)
ch=ch-32;
char (ch);
}while (ch!='0')
getch( );
}

OUTPUT

Enter a character A
a
20. Write a program to count the no. of characters entered.

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
char ch;
int a;
cout <<"Enter the characters:";
cin.get(ch);
while(ch!="\n")
{
a++;
}
cout.put(ch);
getch( );
}

OUTPUT
Enter the characters: pearl
5
21.Write a program to find the cube of a number

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
float cube(float);
float x,y;
cout<<"\n Enter the number whose cube is to be calculated:;
cin>>x;
y=cube(x);
cout <<"\n The cube of"<<x<<"is"<<y<<"\n";
getch( );
return 0;
}
float cube(float a)
{float n ;
n=a*a*a;
return(n);
}

OUTPUT

Enter the number whose cube is to be calculated:9


The cube of 9 is 729
22.Write a program to swap two values

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
void swap(int &, int&) ;
int a,b;
a=7;
b=4
cout<<"\n The original values are:a="<<a<<",b="<<b<<"\n";
swap(a,b);
cout <<"The new values are:a="<<a<<",b="<<b<<"\n";
getch( );
return 0;
}
void swap(int &x, int&y)
{
int temp;
temp=x;
x=y;
y=temp;
}
getch( );
}

OUTPUT

The original values are:a=7,b=4


The new values are:a=4,b=7
23.Write a program to read marks of 50 students and store them under an
array.

#include<iostream.h>
#include<conio.h>
int main( )
{
const int size=50;
float marks [size];
for (int i=0; i<size; i++)
{
cout<<"Enter marks of student"<<i+1<<"\n";
cin>>marks[i];
}
cout<<"\n";
for(i=0; i<size; i++)
cout<< "Marks [" <<i+1<< "]="<<marks[i]<<"\n";
return 0;
}

OUTPUT

Enter marks of student 1


95
Enter marks of student 2
74
Enter marks of student 3
89
Marks[1]=95
Marks[2]=74
Marks[3]=89
24.Write a program to replace every space in a string with a tilde.

#include<iostream.h>
#include<conio.h>
#include<string.h>
int main( )
{
clrscr();
char string[81];
cout<<"Enter string(max 80 characters)\n";
cin.getline(string, 81);
int x1 = strlen(string);
for(int i=0; string[i]!='\0'; i++)
if(string[i]==' ')
sting[i]= ' ~ ';
cout<<"The changed string is \n";
cout.write(string,x1);
return 0 ;
}

OUTPUT
Enter string(max 80 characters)
My name is Pearl.
The changed string is
My~name~is~Pearl.
25.Write a program to reverse words of a string individually.

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

void main( )
{
clrscr( );
int l,I,k=0;
char str[81], word[81];
cout<<"Enter string(max 80 characters)\n";
gets(str);
strcat(str, " ");
for(i=0; str{i} !='\0' ;i++)
{
if( str [i] !=' ')
{
word[k]=str[i];
k=k+1;
}
else
{
while (k>0)
{cout<<word[--k];
}
cout str[i];
}
}
getch( );
}

OUTPUT
Enter string(max 80 characters): I am Pearl
I ma lraeP
INDEX
S.No. Title Page No.
1 Program to display ASCII code of character 1
2 Program to convert a given number of days into 2
years, weeks and days
3 Program to input principal, rate and time and 3
calculate SI
4 Pr Program to input a number. If the number is even 4
prprint its square otherwise print its cube.
5 Program to input a number. If it is odd and positive 5
print its square root otherwise print n5
6 Program to input age of a person. If age>=18 print 6
‘You can vote’ otherwise print ‘You cannot vote’
7 Program for temperature conversion menu that gives 7
the user the option of converting temperature from
Fahrenheit to Celsius or Celsius to Fahrenheit
8 Program to check whether a given year is a leap year 8
or not
9 Program to calculate commission for the salesman 9
10 Program to input a day number and translate into 10
name of day
11 Program to find factorial of a number 11
12 Program to print 1 2 4 8 16 32 64 128 12
13 Program to generate Fibonacci sequence till 15 13
elements
14 1. Program to print all even numbers between 1 and 50 14
in reverse order

15 Program to print the sum of first 10 natural numbers 15


16 2. Program to print 16
1
12
123
1234
12345
17 3. Program to print 17
*
**
***
****

18 4. Program to print 18
A
AB
ABC
ABCD

19 Program to input a character and change its case 19


20 Program to count the no. of characters entered 20
21 Program to find the cube of a number 21
22 Program to swap two values 22
23 Program to read marks of 50 students and store them 23
under an array.

24 Program to replace every space in a string with a tilde 24


25 Program to reverse words of a string individually 25

You might also like