Monday, December 22, 2014

C++ Program for solution of Quadratic equation

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
double a , b , c; //quadratic eq coefficients
cout << "Enter the co-efficients of the quadratic eq...\n";
cout << "for x^2: ";
cin >> a;
cout << "for x^1: ";
cin >> b;
cout << "for x^0: ";
cin >> c;
double disc = (b*b) - (4*a*c); //for discriminant

if( disc<0 )
cout << "Discrimnant is Negative! so the solutions will be complexx numbers\n";
else
cout << "THe Discrimnant is +ve so the solution set will consist of real numbers\n";

double X1,X2; //for solutions
X1 = ( -b + sqrt(disc) ) / (2*a);
X2 = ( -b - sqrt(disc) ) / (2*a);
cout << "The solutions are:\n"
<< "{ "
<< X1
<< " , "
<< X2
<< " }"
<< endl;

//for discrimnant
unsigned int choice;
cout << "Press '1' to get the discrimnant of this equation: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "The discrimnant is: "
<< disc
<< endl;
default:
cout << endl;
}

return 0;
}

Tuesday, December 9, 2014

C++ program for finding 1st maximum and 2nd maximum in a list of numbers

#include<iostream>
using namespace std;

int main()
{
const int NUMBERS=20;
int num[NUMBERS];
for(int i=0 ; i<NUMBERS ; i++ )
{
cout << "Enter number "
<< (i+1)
<< ": ";
cin >> num[i];
}
int high1,high2,index;
high1=num[0];
index=0;
for( int i=0 ; i<NUMBERS ; i++ )
{
if( num[i]>high1 )
{
high1=num[i];
index=i;
}
}

if( high1==num[0] )
{
for( int j=0 ; j<NUMBERS ; j++ )
{
if( high1!=num[j] )
{
index=j;
break;
}
}
high2=num[index];
for( int j=index ; j<NUMBERS ; j++ )
{
if( num[j]>high2 && num[j]<high1 )
high2=num[j];
}

}
else
{
high2=num[0];
for( int j=index ; j<NUMBERS ; j++ )
{
if( num[j]>high2 && num[j]<high1 )
high2=num[j];
}
}
if( high1!=high2 )
{
cout << "\nLargest number is: "
<< high1
<< endl;
cout << "And second is: "
<< high2
<< endl;
}
else
{
cout << "\nLarget number is: "
<< high1
<< "\nAll the numbers are equal so there is no second largest."
<< endl;
}

return 0;
}

Friday, December 5, 2014

C++ Program for printing Z-shape

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int N;
cout << "Enter any number for the size of the Z-Shape: ";
cin >> N;
while( N<=3 )
{
cout << "Invalid Input! Enter again: ";
cin >> N;
}

int i,
space=N;
cout << "The Z-Shape pattern (with spaces) is:\n";
for( i=1 ; i<=N/2+1 ; i++)
cout << "* ";

for( i=1 ; i<=N-1 ; i++)
{
space-=1;
cout << endl
<< setw(space)
<< "*";
}

for( i=1 ; i<=N/2 ; i++)
cout << " *";
cout << endl;
return 0;
}

C++ Program that checks if a number is Palindrome or not

#include<iostream>
using namespace std;

int main()
{
int num,
rev=0,
original;
cout << "Enter any number for tsting: ";
cin >> num;
original=num;
while ( num!=0 )
{
rev=rev*10+num%10; //getting reverse of number
num/=10;
}
if( original==rev )
{
cout << "\nThe number is a Palindrome."
<< endl;
}
else
{
cout << "\nThe number is not a Palindrome."
<< endl;
}

return 0;
}

Tuesday, December 2, 2014

C++ Program that displays prime numbers in a range

#include<iostream>
using namespace std;

bool test_prime(int);

int main()
{
int primeNums;
int num,
count,number;
bool flag=1;
cout << "Enter any prime number: ";
cin >> num;
while( num<=1 )
{
cout << "Wrong Input!"
<< "\nEnter again: ";
cin >> num;
}
flag=test_prime(num); //calling function for testing as prime
while( flag==0 )
{
cout << "Not a prime number!"
<< "\nEnter again: ";
cin >> num;
flag=test_prime(num); //calling function again for testing as prime
}

cout << "\nEnter the number of primes to be displayed after "
<< num
<< ": ";
cin >> primeNums;
number=num;
int* prime=new int[primeNums]; //dinamically allocating memory
count=0;
while( count!=primeNums )
{
number+=1;
prime[count]=number;
for( int i=2 ; i<number ; i++ )
{
if( number%i==0 )
{
prime[count]=0;
break;
}
}
if( prime[count]!=0 )
count++;
}
cout << "\nPrimeNumbers after "
<< num
<< " are:";
for( count=0 ; count<primeNums ; count++ )
{
cout << "  "
<< prime[count];
}
cout << ".\n";
delete prime;
prime=0;

return 0;
}

bool test_prime(int N)
{
bool flag=1;
for( int i=2 ; i<N ; i++ )
{
if( N%i==0 )
{
flag=0;
break;
}
}
return flag;
}

Sunday, November 30, 2014

C++ program that creates four different patterns of different types

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
unsigned int N,
i,j,
p2,p3,p4;
cout << "Enter the size between 2to9: ";
cin >> N;
while( N<2||N>9 )
{
cout << "Wrong Input! (should be between 2to9)"
<< "\nEnter again: ";
cin >> N;
}
cout << "Pattern 1";
cout << setw(15)
<< "Pattern 2"
<< setw(15)
<< "Pattern 3"
<< setw(15)
<< "Pattern 4"
<< endl;
p2=N;
p3=N;
p4=0;
for( i=1 ; i<=N ; i++ )
{
for( j=1 ; j<=N ; j++ )
{
if( j==i )
cout << N;
else
cout << "$";
}
cout << setw(16-N);
for( j=1 ; j<=N ; j++ )
{
if(j==p2 )
cout << N;
else
cout << "$";
}
p2-=1;
cout << setw(16-N);
for( j=1 ; j<=N ; j++ )
{
if( j<=p3 )
cout << "$";
else
cout << N;
}
p3-=1;
cout << setw(16-N);
for( j=1 ; j<=N ; j++ )
{
if( j<=p4 )
cout << N;
else
cout << "$";
}
p4+=1;
cout << endl;
}


return 0;
}

Thursday, November 27, 2014

C++ Program printing *-shape (with spaces)

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int DecN , N;
cout << "Enter number for one line of X-shape: ";
cin >> N;
while( N<=0 || N%2==0 )
{
cout << "Wrong Input! Enter again: ";
cin >> N;
}

cout << "The X-Shape is:\n";
DecN=N;
for( int i=1 ; i<=N ; i++ )
{
cout << setw(25);

if(i==DecN)
{
for( int j=1 ; j<=(N/2+2) ; j++)
cout << "* ";
}
else
{
for( int j=1 ; j<=N ; j++)
{
if( j==i || j==DecN || j==((i+DecN)/2) )
cout << "*";

else
cout << " ";
}
}
cout << endl;
DecN-=1;
}
return 0;
}

C++ Program printing *-shape

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int DecN , N;
cout << "Enter number for one line of X-shape: ";
cin >> N;
while( N<=0 || N%2==0 )
{
cout << "Wrong Input! Enter again: ";
cin >> N;
}

cout << "The X-Shape is:\n";
DecN=N;
for( int i=1 ; i<=N ; i++ )
{
cout << setw(25);
for( int j=1 ; j<=N ; j++)
{
if( j==i || j==DecN || j==((i+DecN)/2) || i==DecN )
cout << "*";
else
cout << " ";
}
cout << endl;
DecN-=1;
}
return 0;
}

Wednesday, November 26, 2014

C++ Program printing X-shape

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int DecN , N;
cout << "Enter number for one line of X-shape: ";
cin >> N;
while( N<=0 || N%2==0 )
{
cout << "Wrong Input! Enter again: ";
cin >> N;
}

cout << "The X-Shape is:\n";
DecN=N;
for( int i=1 ; i<=N ; i++ )
{
cout << setw(25);

for( int j=1 ; j<=N ; j++)
{
if( j==i || j==DecN )
cout << "*";

else
cout << " ";
}

cout << endl;
DecN-=1;
}

return 0;
}

Thursday, November 20, 2014

C++ Program for printing Isosceles triangle (written with alphabets)

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int x,N;
cout << "Enter any number for the height of the isosceles triangle: ";
cin >> N;
int space = N;
x=N;
int integer=65;
char ch;
for( int i=1 ; i<=N ; i++)
{
ch = integer;
cout << setw(space);
space-=1;

for( int j=x-i ; j<N ; j++ )
{
if( i==N )
cout << ch;
else if( j==x-i || j==N-1 )
cout << ch;
else
cout << " ";
}
integer+=6;
x-=1;
cout << endl;
}

return 0;
}



C++ Program for printing Isosceles triangle (empty)

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int x,N;
cout << "Enter any number for the height of the isosceles triangle: ";
cin >> N;
int space = N;
x=N;
for( int i=1 ; i<=N ; i++)
{
cout << setw(space);
space-=1;

for( int j=x-i ; j<N ; j++ )
{
if( i==N )
cout << "*";
else if( j==x-i || j==N-1 )
cout << "*";
else
cout << " ";
}
x-=1;
cout << endl;
}

return 0;
}

C++ Program for printing Isosceles triangle (filled)

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int x,N;
cout << "Enter any number for the height of the isosceles triangle: ";
cin >> N;
int space = N;
x=N;
for( int i=1 ; i<=N ; i++)
{
cout << setw(space);
space-=1;

for( int j=x-i ; j<N ; j++ )
cout << "*";
x-=1;
cout << endl;
}

return 0;
}

C++ Program for printing Inverted Isosceles triangle


#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int N;
cout << "Enter any number for base of Isosceles trianlge: ";
cin >> N;
while( N<=0 || N%2==0 )
{
cout << "Wrong Input! Enter again: ";
cin >> N;
}
int space,
triangle;
space = triangle = N;
for(int i=1 ; i<=N ; i+=2)
{
cout << setw(space);
for( int j=1 ; j<=triangle ; j++)
cout << "*";
cout << endl;
space +=1;
triangle -=2;
}

return 0;
}

Saturday, November 15, 2014

C++ Program for getting LCM

#include<iostream>
using namespace std;

int main()
{
const int N=20; //value of N may be increased here
int numbers[N],LCM;
bool flag=1;
for( int i=0 ; i<N ; i++ )
{
do
{
cout << "Enter number "
<< (i+1)
<< ": ";
cin >> numbers[i]; }while(numbers[i]<0);
}
for( int i=0 ; i<N ; i++ )
{
if( numbers[i]==0 )
{
flag=0;
}
}
if(!flag)
{
cout << "\nL.C.M is zero(0)."
<< endl;
}
else
{
int large,prod;
large=numbers[0];
prod=1;
for( int i=0 ; i<N ; i++ )
{
if( numbers[i]>large )
large=numbers[i];
prod*=numbers[i];
}
int index;
for( int i=large ; i<=prod ; i++ )
{
/*if( i%numbers[0]==0 && i%numbers[1]==0 && i%numbers[2]==0 )
{
LCM=i;
break;
} */
index=-1;
for( int j=0 ; j<N ; j++ )
{
if( i%numbers[j]!=0 )
{
index=j;
break;
}
}
if( index==-1 )
{
LCM=i;
break;
}
}
cout << "\nL.C.M is: "
<< LCM
<< ".\n";
}

return 0;
}

C++ Program for getting HCF

#include<iostream>
using namespace std;

int main()
{
const int N=3;
int numbers[N],HCF;
int i,small;
cout << "Enter nmbers:\n";
for( i=0 ; i<N ; i++ )
{
do
{
cout << "Enter number "
<< (i+1)
<< ": ";
cin >> numbers[i];}while(numbers[i]<=0);
}
small=numbers[0];
for( i=1 ; i<N ; i++ )
{
if( numbers[i]<small )
small=numbers[i];
}
int index;
for( i=small ; i>=0 ; i-- )
{
index=-1;
for( int j=0 ; j<N ; j++ )
{
if( numbers[j]%i!= 0 )
{
index=i;
break;
}
}
if( index==-1 )
{
HCF=i;
break;
}
/*if( numbers[0]%i==0 && numbers[1]%i==0 && numbers[2]%i==0 )
{
HCF=i;
break;
}
}
cout << "\nHCF is: "
<< HCF
<< endl;

return 0;
}

Thursday, November 13, 2014

C++ Program for printing the Fibonacci series upto a given number

#include<iostream>
using namespace std;

int main()
{
int N;
cout << "How many numbers of Fibonacci Series are to be displayed? ";
cin >> N;
int* fab=new int[N]; //dinamically allocating memory
fab[0]=0;
fab[1]=1;
for( int i=2 ; i<N ; i++ )
{
fab[i]=fab[i-1]+fab[i-2];
}
cout << "The Fibonacci Series: \n"
<< fab[0];
for( int i=1 ; i<N ; i++ )
{
cout << " , " << fab[i];
}
cout << "..." << endl;
delete fab;
fab=0;

return 0;
}

Wednesday, November 12, 2014

C++ Program for getting factorial of a number

#include<iostream>
using namespace std;

int main()
{
unsigned int N;
cout << "Enter the number to get its factorial: ";
cin >> N;
long double fact=1;
for( int i=N ; i>=1 ; i-- )
fact *=i;
cout << "The factorial of "
    << N
    << " is: "
    << fact
    << endl;

return 0;
}

C++ program to get the sum of the series: SUM=1-(1/2!)+(1/3!)-(1/4!)...+(1/99!)-(1/100!)

#include<iostream>
using namespace std;

int main()
{
int N;
cout << "Enter the number upto which the series is to be added: ";
cin >> N;     
       double fact,even,odd,total;
even=odd=0;
for( int i=1 ; i<=N ; i++ )
{
fact=1;
for( int j=i ; j>=1 ; j-- )
fact*=j;

if( i%2==0 )
even+=(1/fact);
else
odd+=(1/fact);
}
total = odd - even;
cout << "The Sum of the series is: "
<< total
<< endl;

return 0;
}

Tuesday, November 11, 2014

C++ Program for Math tutor

#include<iostream>
#include<stdlib.h>
#include<ctime>
using namespace std;

int main()
{
unsigned num1,num2,ForSeed=time(0);
int SumUser,SumCPU;
srand(ForSeed);
num1=rand();
num2=rand();
cout << "The two Random Numbers are: " << endl;
cout << num1 << endl;
cout << num2 << endl;
cout<< "Now Guess the Sum of these Numbers:\n";
cin >> SumUser;
SumCPU = num1+num2;
if(SumUser == SumCPU)

cout << "Congratulations!\nYou got the Correct answer";
else
cout << "Your Answer is incorrect! \nThe Exact Answer is: " <<SumCPU << endl;

return 0;
}

Monday, November 10, 2014

C++ Program for printing Empty Diamond Pattern

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int i=1,
StarinLine=-1,
StarVariable,
space,
N;
cout << "Enter the number (should be an Odd Number) for the height of the Diamond: ";
cin >> N;
while( N<=0 || N%2==0 )
{
cout << "Wrong Input! Enter again: ";
cin >> N;
}

cout << "The Diamond Pattern is:\n";
space=N;
while( i<=N )
{
if( i<=(N/2+1) )
{
StarVariable=1;


StarinLine+=2;
space-=1;
cout << setw(space);
while( StarVariable<=StarinLine )
{
if(StarVariable==1 || StarVariable==StarinLine)
cout << "*";
else
cout << " ";
StarVariable++;
}
}
else
{
StarVariable=1;
StarinLine-=2;
space+=1;
cout << setw(space);
while( StarVariable<=StarinLine )
{
if(StarVariable==1 || StarVariable==StarinLine)
cout << "*";
else
cout << " ";
StarVariable++;
}
}

cout << endl;
i++;
}

return 0;
}

C++ program for printing a Diamond pattern

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int StarinLine=-1,
StarVariable,
space,
N;
cout << "Enter the number (should be an Odd Number) for the height of the Diamond: ";
cin >> N;
while( N<=0 || N%2==0 )
{
cout << "Wrong Input! Enter again: ";
cin >> N;
}

cout << "The Diamond Pattern is:\n";
space=N;
for( int i=1 ; i<=N ; i++)
{
if( i<=(N/2+1) )
{
StarinLine+=2;
space-=1;
cout << setw(space);
for( StarVariable=1 ; StarVariable<=StarinLine ; StarVariable++ )
{
cout << "*";
}
}
else
{
StarinLine-=2;
space+=1;
cout << setw(space);
for( StarVariable=1 ; StarVariable<=StarinLine ; StarVariable++ )
{
cout << "*";
}
}

cout << endl;
}

return 0;
}

Friday, November 7, 2014

C++ Program for Binary to Decimal conversion

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
int num,decimal,calB;
double exp;
cout << "Enter a Binary number: ";
cin >> num;
calB=num;
decimal=exp=0;
while( calB!=0 )
{
decimal+=pow(2.0,exp)*(calB%10);
calB/=10;
exp++;
}
cout << "Decimal of "
<< num
<< " is: "
<< decimal
<< ".\n";

return 0;
}

C++ Program for Decimal to Binary conversion

//Decimal to Binary

#include<iostream>
using namespace std;

int find_binary(int);
void show_binary(int,int);

int main()
{
int num,binary;
cout << "Enter a Decimal number: ";
cin >> num;
if( num<0 )
{
num=abs(num);
binary=find_binary(num);
show_binary(-num,-binary);
}
else
{
binary=find_binary(num);
show_binary(num,binary);
}

return 0;
}

int find_binary(int num )
{

int calD,calB,binary;
calD=num;
calB=1;
while( calD!=0 )
{
calB=calB*10+calD%2;
calD/=2;
}
binary=0;
while( calB>=10 )
{
binary=binary*10+calB%10;
calB/=10;
}
return binary;
}

void show_binary(int num,int binary)
{
cout << "Binary form of "
<< num
<< " is: "
<< binary
<< ".\n";
}

Wednesday, November 5, 2014

C++ program for arranging numbers in Asceding and Descending order

#include<iostream>
using namespace std;

int main()
{
unsigned int N;
cout << "How many numbers you want to enter for arranging?(should be less than 100) ";
cin >> N;
if( N>0 && N<100 )
{
double num[100]; //array for taking numbers
int i,j; //for loops
//taking numbers
cout << "Now Enter the numbers: \n";
for( i=0 ; i<=N-1 ; i++ )
{
cin >> num[i];
}

//taking choice from user to show the output
char choice; //for choice
cout << "To arrange in ascending order press 'A' and to arrange in Descending order press 'D' and for getting both orders press 'B':\n";
cin >> choice;
double temp; //for manipulation
switch (choice)
{
case 'A': //for ascending order
{
//now manipulating for asecnding order
for( i=0 ; i<=N-1 ; i++ )
{
for ( j=i+1 ; j<=N-1 ; j++ )
{
if( num[j]<num[i] )
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
//for printing in ascending order
cout << "The numbers in Ascending order are: \n";
for( i=0 ; i<=N-1 ; i++ )
{
cout << num[i] << endl;
}
break;
}
case 'D': //for descending order
{
//now manipulating for descending order
for(i=0 ; i<=N-1 ; i++ )
{
for( j=i+1 ; j<=N-1 ; j++ )
{
if( num[j]>num[i] )
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
//for printing in descending order
cout << "The numbers in Descending order are: \n";
for( i=0 ; i<=N-1 ; i++ )
{
cout << num[i] << endl;
}
break;
}
case 'B': //for both orders
{
//now manipulating for asecnding order
for( i=0 ; i<=N-1 ; i++ )
{
for ( j=i+1 ; j<=N-1 ; j++ )
{
if( num[j]<num[i] )
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
//for printing in ascending order
cout << "The numbers in Ascending order are: \n";
for( i=0 ; i<=N-1 ; i++ )
{
cout << num[i] << endl;
}

//now manipulating for descending order
for(i=0 ; i<=N-1 ; i++ )
{
for( j=i+1 ; j<=N-1 ; j++ )
{
if( num[j]>num[i] )
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
//for printing in descending order
cout << "The numbers in Descending order are: \n";
for( i=0 ; i<=N-1 ; i++ )
{
cout << num[i] << endl;
}
break;
}

default:
cout << "Wrong Input!" <<endl;
}
}
else
cout << "The program cannot arrange the required numbers!"
<< endl;

return 0;
}

C++ program that arranges names alphabetically

#include<iostream>
#include<string>
using namespace std;

int main()
{
int N;
string ch1[50],
  temp;
cout << "Enter the number of names to be arranged(should be less than 50): ";
cin >> N;
/* using cin >>  and then calling cin.get causes problems because
the >> operator leaves the newline character in the keyboard buffer. When the cin.get function executes,
the first character it sees in the keyboard buffer is the newline character, so it reads no further.
The same problem exists when a program uses cin >> and then calls cin.getline to read a line of input!!! */
//so cin.igbore is used...
cin.ignore();
cout << "Enter the names:";
for( int i=0 ; i<N ; i++ )
{
//cin >> ch1[i];
cout << "\nEnter Name "
<< (i+1)
<< ": ";
getline(cin,ch1[i]);
}
//for manipulation...
for( int i=0 ; i<(N-1) ; i++ )
{
for( int j=i+1 ; j<N ; j++ )
{
if(ch1[j]<ch1[i])
//if(strcmp(ch1[j],ch1[i])
{
temp = ch1[i];
ch1[i] = ch1[j];
ch1[j] = temp;
}


}
}
cout << "\nThe Names arranged in Ascending order are:\n";
for( int i=0 ; i<N ; i++ )
{
cout << "Name "
<< (i+1)
<< ": "
<< ch1[i]<<endl;
}

return 0;
}

Sunday, November 2, 2014

C++ code that shows ASCII values corresponding to their Decimal values

//ASCII code generation
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
char ascii;
cout << "Decimal value............ASCII value\n";
for( int i=0 ; i<=127 ; i++ )
{
ascii=i;
cout <<setw(13) << i
<< "............"
<< ascii
<< endl;
}

return 0;
}

C++ program that shows the list of Armstrong numbers in a range

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
int first,last,digits,Num,sum,countF,countN;
double forPower;
cout << "Enter first number: ";
cin >> first;
cout << "Enter last number of range: ";
cin >> last;
digits=first;
countF=0;
while( digits!=0 )
{
countF++;
digits/=10;
}
cout << "Amstrongs from "
<< first
<< " to "
<< last
<< " are: ";
Num=first;
while(Num<=last)
{
sum=0;
digits=Num;
while(digits!=0)
{
forPower=digits%10;
sum+=pow(forPower,countF);
digits/=10;
}
if(sum==Num)
cout << " " << Num;
Num++;
countN=0;
digits=Num;
while(digits!=0)
{
countN++;
digits/=10;
}
if(countN>countF)
countF++;
}
cout << endl;

return 0;
}

C++ program that checks a number if it is an Armstrong or not

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
int num,sum=0,digits,count=0;
double forPower;
cout << "Enter any number: ";
cin >> num;
digits=num;
while( digits!=0 )
{
count++;
digits/=10;
}
digits=num;
for( int i=1 ; i<=count ; i++ )
{
forPower=digits%10;
sum+=pow(forPower,count);
digits/=10;
}
if( sum==num )
cout << "Number "
<< num
<< " is an Armstrong.\n";
else
cout << num
<< " is NOT an Armstrong.\n";

return 0;
}