#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;
}
Monday, December 22, 2014
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;
}
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;
}
#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;
}
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;
}
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;
}
Subscribe to:
Comments (Atom)