Swap two variables without
using third variable
#include
int main()
{
int var1, var2;
cout<<"enter value for first integer: ";
cin>>var1;
cout<<"enter value for second integer: ";
cin>>var2;
cout<<" values before swapping: "<
cout<<"first integer ="<
cout<<"second interger ="<
var1=var1+var2;
var2=var1-var2;
var1=var1-var2;
cout<<" values after swapping: "<
cout<<"first integer ="<
cout<<"second interger ="<
return 0;
}
{
int var1, var2;
cout<<"enter value for first integer: ";
cin>>var1;
cout<<"enter value for second integer: ";
cin>>var2;
cout<<" values before swapping: "<
var1=var1+var2;
var2=var1-var2;
var1=var1-var2;
cout<<" values after swapping: "<
}
Find last prime number occur before enter
number
#include
#include
#include
int
main()
{
int num,count=0;
cout<<"Enter number to find last prime number occurs before it: ";
cin>>num;
for( int a=num-1;a>=1;a--)
{
for(int b=2;b {
if(a%b==0)
count++;
}
if(count==0)
{
if(a==1)
{
cout<<"no prime number less than 2";
break;
}
cout< break;
}
count=0;
}
return 0;
}
{
int num,count=0;
cout<<"Enter number to find last prime number occurs before it: ";
cin>>num;
for( int a=num-1;a>=1;a--)
{
for(int b=2;b {
if(a%b==0)
count++;
}
if(count==0)
{
if(a==1)
{
cout<<"no prime number less than 2";
break;
}
cout< break;
}
count=0;
}
return 0;
}
C++ program
to find Fibonacci Series upto given range
#include
#include
int main()
{
int range, first = 0,
second = 1, fibonicci=0;
cout <<
"enter range for terms of fibonacci sequence: ";
cin >>
range;
cout <<
"fibonicci series upto " << range << " terms
"<< endl;
for ( int c = 0
; c < range ; c++ )
{
if ( c
<= 1 )
fibonicci = c;
else
{
fibonicci = first + second;
first = second;
second = fibonicci;
}
cout << fibonicci <<" ";
}
return 0;
}
#include
#include
void swap()
{
cout<<"this is my
swap";
}
int main()
{
int firstnum , secondnum;
cout<<"enter value for
first number: ";
cin>>firstnum;
cout<<"enter value for
second number: ";
cin>>secondnum;
cout<<"\n\n";
cout<<"values before
calling built in swap function"<
cout<<"\tfirst
number = "<
cout<<"\tsecond number
= "<
cout<<"\n\n";
std::swap( firstnum , secondnum );
cout<<"values after
calling bulit in swap function"<
cout<<"\tfirst number
= "<
cout<<"\tsecond number
= "<
return 0;
}
No comments:
Post a Comment