RSS

Category Archives: Miscellaneous Programs

PROGRAM TO OVERLOAD MULTIPLICATION OPERATOR TO WORK WITH FRACTIONS

//THIS PROGRAM OVERLOADS MULTIPLICATION OPERATOR TO WORK WITH FRACTIONS AS WELL AS INTEGERS

#include <iostream>
#include <cstdlib>

using namespace std;

class fraction
{
	int numerator,denominator;
	public:
		fraction(int num = 0, int den = 1):numerator(num), denominator(den)
		{}
		void print()
		{
			cout<<numerator<<"/"<<denominator<<endl;
		}
		friend fraction operator*(fraction, fraction);
		friend fraction operator*(fraction, int);
		friend fraction operator*(int, fraction);
};

fraction operator*(fraction a, fraction b)
{
	int num = a.numerator * b.numerator;
	int den = a.denominator * b.denominator;
	return fraction(num,den);
}

fraction operator*(fraction a, int b)
{
	int num = a.numerator * b;
	int den = a.denominator;
	return fraction(num,den);
}

fraction operator *(int a, fraction b)
{
	return operator*(b,a);
}

int main()
{
	int choice;
	char wish;
	do{
		system("clear"); //change to system("cls") if on a windows machine
		cout<<"\nChoose an Option\n";
		cout<<"\n1. MUltiply two fractions\n";
		cout<<"\n2. Multiply a fraction and an integer\n";
		cin>>choice;
		while(choice != 1 && choice != 2)
		{
			cout<<"\nIncorrect choice\nEnter a valid one\n";
			cin>>choice;
		}
		if(choice == 1)
		{
			int num,den;
			cout<<"\nEnter the numerator and denominator of first fraction respectively\n";
			cin>>num>>den;
			while(den == 0)
			{
				cout<<"\nDenominator cannot be zero. Enter once again\n";
				cin>>den;
			}
			fraction f1(num,den);
			cout<<"\nEnter the numerator and denominator of second fraction respectively\n";
			cin>>num>>den;
			while(den == 0)
			{
				cout<<"\nDenominator cannot be zero. Enter once again\n";
				cin>>den;
			}
			fraction f2(num,den);
			fraction product = f1*f2;
			cout<<"\nThe product is: \t";
			product.print();
		}
		else if(choice == 2)
		{
			int num,den;
			cout<<"\nEnter the numerator and denominator of fraction\n";
			cin>>num>>den;
			while(den == 0)
			{
				cout<<"\nDenominator cannot be zero. Enter once again\n";
				cin>>den;
			}
			fraction frac(num,den);
			cout<<"\nEnter the integer\n";
			cin>>num;
			fraction product = frac*num;
			cout<<"\nTHe product is: \t";
			product.print();
		}
		cout<<"\nDo once again? (Y/n)\t";
		cin>>wish;
		while(wish != 'Y' && wish != 'N' && wish != 'y' && wish != 'n')
		{
			cout<<"\nInvalid input\nEnter valid one\n";
			cin>>wish;
		}
	}
	while(wish == 'Y' || wish == 'y');
	
	return 0;
}

 
Leave a comment

Posted by on May 23, 2016 in Miscellaneous Programs

 

PROGRAM TO FIND THE NUMBER OF TRAILING ZEROES IN THE FACTORIAL OF A NUMBER

#include <iostream>
#include <cmath>
 
using namespace std;
 
int main()
{
  long long int i = 0,num;
     cin>>num;
     long long power = 1,facts = 0;
     while(floor(num/pow(5,power)) != 0)
     {
       facts += floor(num/pow(5,power));
       power++;
     }
     cout<<facts<<'\n';
    return 0;
} 
 
Leave a comment

Posted by on April 21, 2016 in Miscellaneous Programs

 

Protected: MINESWEEPER GAME

This content is password protected. To view it please enter your password below:

 
Enter your password to view comments.

Posted by on December 17, 2015 in Miscellaneous Programs

 

THIS PROGRAM PRINTS THE LARGEST GAP BETWEEN TWO PRIME NUMBERS SMALLER THAN AN ENTERED NUMBER

// THIS PROGRAM PRINTS THE LARGEST GAP BETWEEN TWO PRIME NO.S IN A RANGE

#include <iostream.h>
#include <conio.h>


void main()
{
 clrscr();
 unsigned long long range;
 cout<<"\nEnter the range of prime numbers\n";
 cin>>range;

 unsigned long long *arr = new unsigned long long [range-1];

 for(unsigned long long i = 2; i<=range;i++)
 {
  arr[i-2] = i;
 }

 for(i = 0; arr[i]<=range; i++)
 {
  if(arr[i] != 0)
  {
   for(unsigned long long j = i+1; arr[j]<=range; j++)
   {
    if(arr[j] % arr[i] == 0)
    arr[j] = 0;
   }
  }
 }

cout<<"\nThe prime numbers in this range are\n";

 for(i = 0; i<= range-2; i++)
 {
  if(arr[i] != 0)
  cout<<arr[i]<<' ';
 }

 
 int gap = 0;
 int bestgap = 0;
 unsigned long long loc = 0;

 for(i = 0; i<=range-2; i++)
 {
  gap = 0;
  for(unsigned long long j = i+1; (arr[j] == 0 && j<=range-2); j++)
  {
   gap++;
  }
  if(gap>bestgap)
  {
   bestgap = gap;
   loc = i;
  }
  j--;
  i = j;
 }

 cout<<"\n\nThe bestgap is \n"<<bestgap;
 cout<<"\n\nThe numbers comprising this gap are \n";

 loc++;

 for(i = loc; i<loc+bestgap; i++)
 cout<<i+2<<' ';

 getch();
}
 

PROGRAM TO PRINT PRIME NUMBERS IN A RANGE(USING SIEVE OF ERATOSTHENES)

// THIS PROGRAM DISPLAYS THE PRIME NUMBERS IN A RANGE (USING SIEVE OF ERATOSTHENES)

#include <iostream.h>
#include <conio.h>
#include <math.h>

void main()
{
clrscr();
int range, arr[100];

cout<<“\nENTER THE RANGE (INCLUSIVE) \n”;
cin>>range;

int j = 2;

for(int k = 0; j<=range; k++, j++)
arr[k] = j;

int prime = 0;

if(range == 1)
cout<<“\nNO PRIME NUMBERS \n”;
else
{
for(int i = 2; i<=floor(sqrt(range)) ; i++)
{
for(k = i-1; k<range; k++)
{
if(arr[k] % i == 0 && arr[k] != 0)
{
arr[k] = 0;
}
}
}
}

cout<<“\nTHE PRIME NUMBERS ARE \n”;

for(k = 0; k<range-1 ; k++)
{
if(arr[k] != 0)
{
cout<<arr[k]<<‘ ‘;
prime++;
}
}

cout<<“\n\nTHERE ARE A TOTAL OF “<<prime<<” PRIME NUMBERS \n”;
getch();
}

 
Leave a comment

Posted by on November 21, 2015 in Miscellaneous Programs

 

PROGRAM TO TEST THE MULTIPLICATION SKILLS OF PRIMARY STUDENTS

// THIS PROGRAM TESTS MULTIPLICATION SKILLS OF PRIMARY STUDENTS

#include <iostream.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>

void main()
{
clrscr();

long seed = time(NULL);
char choice;
int num1, num2, ans, num, marks=0;

srand(seed);

cout<<“Hello and Welcome to this quiz for primary students\n”;
cout<<“\nPlease choose your Group from the Menu below\n”;
cout<<“\n\nGROUP A : Classes 1 and 2 (Press A)\n”;
cout<<“GROUP B : Classes 3 to 5 (Press B)\n”;
cin>>choice;
clrscr();
if(choice == ‘A’  ||  choice == ‘B’)
{
cout<<“So lets begin..\n”;
cout<<“\nPlease Enter the number of Questions\n”;
cin>>num;
}

for(int i=1; i<=num; i++)
{
if(choice == ‘A’)
{
num1 = rand()%9 + 1;
num2 = rand()%9 + 0;
}
else if(choice == ‘B’)
{
num1 = rand()%20 + 12;
num2 = rand()%20 + 13;
}

else
{
cout<<“Wrong input\n”;
break;
}

cout<<“\n”<<i<<“. What does “<<num1<<” X “<<num2<<” equals to?\t”;
cin>>ans;

if(ans == num1*num2)
{
cout<<“EXCELLENT”;
marks++;
}
else
{
cout<<“WRONG ANSWER! “;
cout<<“THE CORRECT ANSWER WAS “<<num1*num2;
}

cout<<‘\n’;

}

if(marks == num)
cout<<“\nYOU ANSWERED ALL THE QUESTIONS CORRECTLY. KEEP IT UP\n”;
else if(marks == 0)
cout<<“\nYOU NEED TO WORK REALLY HARD ON YOUR MULTIPLICATIONS. TRY AND YOU’LL LEARN IT\n”;
else
cout<<“\nIT WAS FINE BUT YOU HAVE TO STILL WORK HARD\n”;

cout<<“\nThanks!”;
cout<<“\nPress any Key to Terminate\n”;
getch();
}

 
Leave a comment

Posted by on August 17, 2015 in Miscellaneous Programs

 

PROGRAM TO WAIT TILL THE USER PRESSES A KEY

// PROGRAM TO WAIT TILL THE USER PRESSES A KEY (USING kbhit function)

#include <iostream.h>
#include <conio.h>
#include <dos.h>

void main()
{
clrscr();
long long int ctr = 0;

while(kbhit() == 0)
{
clrscr();
cout<<“THIS COUNTER WILL GO ON TILL YOU PRESS A KEY\n”;
cout<<“\nCOUNTER = “<<ctr++;
delay(60);
}

cout<<“\n\nYOU PRESSED A KEY\n”;
cout.flush();
getch();
}

 
Leave a comment

Posted by on August 17, 2015 in Miscellaneous Programs