Examples of Strings and Functions
Program of swap using call by reference:)
#include<iostream.h>
#include<conio.h>
void swap(int &m, int &d)
{
int md;
md=m;
m=d;
d=md;
}
void main(void)
{
int a=10;
int b=20;
clrscr();
cout<<"\n Before swapping"<<endl;
cout<<"\n A= "<<a<<" and B= "<<b<<endl<<endl;
swap(a,b);
cout<<"\n After Swapping"<<endl;
cout<<"\n A= "<<a<<" and b= "<<b;
getch();
}
Here is the output :)))
2) Print total vowels from the string --->
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int vowel=0;
int k=0;
char r[50];
clrscr();
cout<<"\n Enter the string: ";
cin>>r;
while(k<50 && r[k]!='\0')
{
switch(r[k])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
vowel++;
break;
}
k++;
}
cout<<"\n There are "<<vowel<<" vowels in the string";
getch();
}
Here is the output :)))
3) Program to print the square of given number using function:))
#include<iostream.h>
#include<conio.h>
void main()
{
int sq(int);
int a,ans;
clrscr();
cout<<"\n Enter the value of a: ";
cin>>a;
ans=sq(a);
cout<<"\n Square= "<<ans;
getch();
}
int sq(int d)
{
int ans;
ans=d*d;
return ans;
}
Here is the output :)))
4) Write the program to find the simple interest using function:))
#include<iostream.h>
#include<conio.h>
void main()
{
double si(double,double,double);
double amt,r,n,ans;
clrscr();
cout<<"\n Enter the Amount: ";
cin>>amt;
cout<<"\n Enter the Interest Rate: ";
cin>>r;
cout<<"\n Enter the no of years: ";
cin>>n;
ans=si(amt,r,n);
cout<<"\n Simple Instrest = "<<ans;
getch();
}
double si(double a,double b,double c)
{
double ans;
ans=(a*b*c)/100;
return ans;
}
Here is the output :)))
5) Write the program to count the vowels and consonant from the given string:)))
#include<iostream.h>
#include<conio.h>
void main()
{
char s[50];
int d=0;
int vowel=0;
int consonant=0;
clrscr();
cout<<"\n Enter the string: ";
cin>>s;
while(d<50 && s[d]!='\0')
{
switch(s[d])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
vowel++;
break;
default:
consonant++;
break;
}
d++;
}
cout<<"\n There are "<<vowel<<" vowels ";
cout<<"\n and "<<consonant<<" consonants";
getch();
}
Here is the output :)))
No comments:
Post a Comment