++ program to swap two variables
without using third variable or temporary variable
#include
#include
using namespace std;
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;
}
#include
using namespace std;
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: "<
var2=var1-var2;
var1=var1-var2;
cout<<" Values after swapping: "<
}
C++ program to find the last prime
number before number entered
#include
#include
using namespace std;
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;
}
using namespace std;
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;
}
C++ program to find fibonacci series
with simple logic
#include
#include
using namespace std;
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;
}
C++
program to swap two numbers using built in swap function in c++ standard
library
#include
#include
using namespace std;
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