Sie sind auf Seite 1von 17

WAP to do Swapping by Call By Value

#include<iostream.h>
#include<conio.h>
int main()
{clrscr();
void swap(int, int);
int a=7,b=4;
cout<<"\nOriginal value\n";
cout<<"a="<<a<<"\n";
cout<<"b="<<b;
swap(a, b);
cout<<"\nValues after swap\n";
cout<<"a="<<a<<"\n";
cout<<"b="<<b;
return 0;
}
void swap(int x, int y)
{int temp=x;
x=y;
y=temp;
cout<<"\nSwapped values are:\n";
cout<<"a="<<x<<"\n";
cout<<"b="<<y;}

WAP to do Swapping by Call by Reference


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

WAP to read a Character & Print Next

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main()
{clrscr();
char a, b;
cout<<"\nEnter any character\n";
a= getchar();
b=a+1;
cout<<"\nNext character is=";
putchar(b);
getch();
return 0;
}

WAP to find weather a Character is Alphanumeric or Digit


#include<iostream.h>

#include<ctype.h>
#include<conio.h>
void main()
{clrscr();
char a;
cin>>a;
if(isalnum(a))
{cout<<"\nAlphanumeric\n";
if(isalpha(a))
cout<<"\nAlphabet\n";
else
cout<<"\nDigit\n";
}
else
cout<<"\nOther Character\n";
getch();
}

To print
*
*

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

WAP to print
*

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

WAP to find cube of a number


#include<iostream.h>

#include<conio.h>
void main()
{
clrscr();
float cube(float);
float a,cu;
cout<<"Enter any number:";
cin>>a;
cu=cube(a);
cout<<"\nCube of "<<a<<" is "<<cu;
getch();
}
float cube(float a)
{
float cu;
cu=a*a*a;
return(cu);
}

WAP to use Array for Entering and print Marks


#include<iostream.h>

int main ( )
{
const int size=5;
int marks [size];
int i;
for (i =0; i < =5; i++)
{
cout<<"Enter marks");
cin>>marks[i];
}
cout<<"\n"
for (i = 0; i < = 5; i ++)
cout<<"Marks["<<i<<"]="<<marks[i]<<"\n";
return 0;
}

WAP to enter number and display sum, product and average of


the number
#include<iostream.h>

int main ( )
{double price[5],sum,avg,prod;
sum=avg= 0;
prod=1;
p
for (i=0; i<5; i++)
{
cout<<"Enter price"<<i+1<<"=";
cin>>price[i];
sum +=sum[i];
prod*=prod[i];
}
avg=sum/5
cout<<"\nSum of prices is = "<<sum<<endl;
cout<<"\nProduct of prices is = "<<prod<<endl;
cout<<"\nAverage of prices is = "<<avg<<endl;
return 0;
}

WAP to search a specific element in a 1-d array


#include<iostream.h>
int main()

{
int A[10],size,i,flag=0,num,pos;
cout<<"Enter number of elements you want to insert ";
cin>>size;
for(i=0;i<size;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>A[i];
}
cout<<"Enter the number you want to search ";
cin>>num;
for(i=0;i<size;i++)
if(A[i]==num)
{flag=1;
pos=i;
break;
}
if (flag==0)
cout<<"\nItem not found";
else
cout<<"\nItem found at"<<(pos+1);
return 0;
}

WAP to find a substring of a given string


#include<iostream.h>
int main()

{
char x[50], y[50];
int i,j;
cout<<"Enter big string";
cin>>x;
cout<<"Enter search string";
cin>>y;
for( i=0; x[i]!='\0'; i++)
{
for( j=0; y[j]!='\0'; j++)
{
if ( x[i+j]!=y[j]) break;
}
if(y[j]=='\0')
{
cout<< "Found at "<<i<<endl;
break;
}
}
return 0;
}

WAP to delete duplicate elements from a vector.


#include<iostream.h>
int main()
{ int i,j,k,num,ans=0;

int vec[20];
cout<<"\nEnter size of the array: ";
cin>>num;
cout<<"\nEnter elements into the array: \n";
for(i=0;i<num;i++)
cin>>vec[i];
cout<<"\nOrignal vector\n";
for(i=0;i<num;i++)
cout<<"\n"<<vec[i];
{for(i=0;i<num-1;++i)
for(j=1+i;j<num;++j)
{

if(vec[i]==vec[j])
{num=num-1;
for(k=j;k<num;++k)

vec[k]=vec[k+1];
ans=1;
j=j-1;
}
}if(ans==0)
cout<<"\nVector without duplication\n";
else
{ cout<<"\nThe array after removing duplicates is: ";
for(i=0;i <num ;i++)
cout<<"\n"<<vec[i];
}return 0;}
}

WAP to count number of lowercase entered


#include <iostream>
int main()
{ int chcount=0;
const char ent='\n';
char ch;

cout << " Enter Charecter:\n";


cin.get(ch);
while(ch!=ent)
{
if(ch>='a'&&ch<='z')
{ chcount++;
cout.put(ch);
}
cin.get(ch);
}
cout <<"Enter the numbers of charecter="<<chcount<<"\n';
return 0;
}
}

WAP to find fibonacci series


#include<iostream>
int main()
{ int n, c, first = 0, second = 1, next;
cout << "Enter the number of terms of Fibonacci series you want" << endl;
cin >> n;
cout << "First " << n << " terms of Fibonacci series are :- " << endl;

for ( c = 0 ; c < n ; c++ )


{

if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout << next << endl;

return 0;
}

WAP to find factorial of a number


#include<iostream>
using namespace std;
int main()
{
int num,factorial=1;
cout<<" Enter Number To Find Its Factorial: ";

cin>>num;
for(int a=1;a<=num;a++)
{
factorial=factorial*a;
}
cout<<"Factorial of Given Number is ="<<factorial<<endl;
return 0;
}

WAP to find if it is lower case,upper case or digit


include<iostream.h>
#include<conio.h>
void main()
{
char ch;
cout<<"enter the character"<<endl;
cin>>ch;

if((ch>=65)&&(ch<=90))
cout<<"the Character is a Upper Case Alphabet"<<endl;
if((ch>=97) &&(ch<=122))
cout<<" the character is a Lower Case Alphabet"<<endl;
if((ch>=48) && (ch<=57))
cout<<" the entered char is a Numeric value"
getch();
}

Das könnte Ihnen auch gefallen