Programs of Control Structures.


 Write a program to find whether the given character is a digit or a letter.

# include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
cout<<”Enter any character”;
cin>>ch;
if((ch>=’0’)&&(ch<=’9’)
cout<<”Entered character is a digit”;
else
if(((ch>=’a’)&&(ch<=’z’)||((ch>=’a’)&&(ch>=’z’)))
cout<<”Entered character is an alphabet”;
else
cout<<”wrong input”;
getch();
}
Output
Enter   any character1
Entered character is a  digit

 Write the program to input the number and print the sum of the digit.

#include<iostream.h>
#include<conio.h>
void main()
{
long int n;
int rem,s=o;
cout<<”Enter any integer”;
cin>>n;
while(n!=o)
{
rem=n%10;
s=s+rem;
n=n/10;
}
cout<<”/n sum of digit=”<<&;
getch()
}
Output
Enter  any integer 5741
            sum of digit
 


 Write a program to print square and cubes of all the number from  1 to 10.

#include<iostream.h>
#include<conio.h>
Void main()
{
int I;
cout<<”number/t square/t cube”;
for(i=1;i<=10;i++)
cout<<”/n”<<i<<”/t”<<i*i<<”/t”<<i*i*I;
}
getch();
}
Output
number     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




 Write a program to input a number from 1 to 9 and print the following pattern.

1)if n=1
    1
2)if n=2
    1
    22
3)if n=5
    1
    22
    333
    4444
     55555
#include<iostream.h>
#include<conio.h>
void main()
{
int n,I,j;
cout<<”Enter any number(1-9)”;
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=I;j++)
cout<<i;
}
cout<<”/n”;
}
getch();
}
Output
Enter any number4
1
22
333
4444


 Write a program using switch case to input  a digit and print in words.

#include<iostream.h>
#include<conio.h>
void main()
{
int n;
cout<<”Enter any digit (0-9)”;
cin>>n;
switch(n)
{
case 1: cout<<”/none”;
break;
case 2: cout<<”/ntwo”;
break;
case 3: cout<<”/nthree”;
break;
case 4: cout<<”/nfour”;
break;
case 5: cout<<”/nfive”;
break;
case 6: cout<<”/nsix”;
break;
case 7: cout<<”/nseven”;
break;
case 8: cout<<”/neight”;
break;
case 9: cout<<”/nnine”;
break;
case 0: cout<<”/nzero”;
break;
defaut: cout<<”wrong input”;
}
getch();
}
Output
Enter any digit(0-9)8
eight
Programs on string manipulation


 Write a program to input a string from the user and count its length & no. of alphabets.

#include<iostream.h>
#include<studio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
int l,na=0,i:
char str[100];
cout<<”Enter any string”;
gets(str);
l=strlen(str);
for(i=0;i<l;i++)
{
if(isalpha(str[i]))
{
na++;
}
}
cout<<”\nstring length”<<l;
cout<<”\n number of alphabet”<<na;
getch();
}
output
Enter any stringthe sun rises from east
string lenght23
number of alphabet19

 write a program to input a string  from the user and check  whether is it palindrome or not

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str[100],rev[100];
int c;
cout<<”enter the string”;
gets(str);
strcpy(rev,str);
strrev(rev);
c=strcmp(str,rev);
if(c==0)
cout<<”\npalindrome”;
else
cout<<”\nnon palindrome”;
getch();
}
output
enter any stringmalyalam
palindrome

write a program to input first name and last name  from  the user and concatenate also convert it in uppercase.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char ln[25],fn[25];
cout<<”enter the  first name”;
gets(fn);
cout<<”enter the last name”;
gets(ln);
strcat(fn,ln);
strupper(fn);
cout<<”\n”<<fn;
getch();
}
output
enter the first namesunidhi
enter the last namechauhan
SUNUDHICHAUHAN
 

 Write a program to input a string and a character from the user and count how many times that character appeared in the string.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
char str[50],ch;
int I,c=0;
cout<<”enter any string”;
gets(str);
cout<<”enter the character”;
cin>>ch;
for(i=0;str[i]!=NULL;i++)
{
if(str[i]==ch)
{
c++;
}
}
cout<<”\nthe number times the character appeared in the string”<<c;
getch();
}
output
enter any stringmalyalam
enter the character a
the number times character appeared in the string4
 

 Write a program to count number of words in a string as well as number of  character other than space.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
int sp=0,l,i;
char str[70];
cout<<”enter any string”;
gets(str);
l=strlen(str);
for(i=0;str[i]!=NULL;i++)
{
if(str[i]==32)
{
sp=sp+1;
}
}
cout<<”\nnumber of words”<<sp+1;
cout<<”\nnumber of characters”<<l-sp;
getch();
}
output
enter the stringbe good
number of words2
number of characters6