Tuesday, 21 January 2020

TO FIND ANY ELEMENT IN ARRAY IN C++

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,a[10],b;
cout<<"enter the size\n";
cin>>n;
cout<<"enter the elements\n";
for(int i=0;i<n;i++)
{
 cin>>a[i];
 }
cout<<"enter the element you want to find";
cin>>b;
for(int j=0;j<n;j++)
{
if(a[j]==b)
{
cout<<j+1;
}
}
getch();
}

FACTORIAL BY RECURSION IN C++

#include<iostream.h>
#include<conio.h>
int fact(int p);
void main()
{
clrscr();
int a,b;
cout<<"enter";
cin>>a;
b=fact(a);
cout<<b;
getch();
}
int fact(int p)
{
int d;
if(p==1)
return 1;
else
return p*fact(p-1);
}

CALCULATOR BY C++

#include<iostream.h>
#include<conio.h>
int sum();
int sub();
int mul();
void main()
{
clrscr();
int d,ch;
cout<<"menu   \n";
cout<<"1 add\n"<<"2  sub\n"<<"3 mul\n";
cout<<"enter your choice\n";
cin>>ch;
switch(ch)
{
case 1:d=sum();
break;
case 2:d=sub();
break;
case 3:d=mul();
break;
default:
cout<<"wrong choice";
}
cout<<d;
getch();
}
int sum()
{   int a,b,c;
cout<<"enter two values";
cin>>a>>b;
c=a+b;
return c;
}
int sub()
{
 int a,b,c;
cout<<"enter two values";
cin>>a>>b;
c=a-b;
return c;
}

int mul()
{   int a,b,c;
cout<<"enter two values";
cin>>a>>b;
c=a*b;
return c;
}

Swapping of two numbers by using third variable in C++

#include<iostream.h>
#include<conio.h>
void swap(int x,int y);
void main()
{
clrscr();
int a,b;
cout<<"enter any two number";
cin>>a>>b;
cout<<"value of a is"<<a<<"\n";
cout<<"value of b is "<<b<<"\n";
swap(a,b);
getch();
}
void swap(int x,int y)
{ int a;
   a=x;
   x=y;
   y=a;
   cout<<"value of a after swapping "<<x<<"\n"<<"value of b after swapiing"<<y;
   }

To print cube of a number in C++

#include<iostream.h>
#include<conio.h>
float cube(int x);
void main()
{
clrscr();
int a,c;
cout<<"enter any no";
cin>>a;
c=cube(a);
cout<<c;
getch();
}
float cube(int x)
{
int r;
r=x*x*x;
return (r);
}

To check whether the number is palindrome or not in C++

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,rev=0;
cout<<"enter any number";
cin>>n;
int temp=n;
int sum=0;
while(temp>0)
{
int d=temp%10;
sum=sum+d;
rev=rev*10+d;
temp=temp/10;
}
cout<<"sum of number is"<<sum<<"\n";
cout<<"reverse of number is\n "<<rev<<"\n";
if(rev==n)
cout<<"palindrome\n";
else
cout<<"not palindrome\n";
getch();
}