Need a project done?

C++ Programming Developer

Search This Blog

C++ Program to enter 20 unique numbers. No number should be entered twice.

C++ Program to enter 20 unique numbers. No number should be entered twice.

Solution:

C++ program Integer to English conversion



Write a program that takes an integer and displays the English name of that value.

Examples:

10 -> ten
121 -> one hundred twenty one
1032 -> one thousand thirty two
11043 -> eleven thousand forty three
1200000 -> one million two hundred thousand

Solution:

C++ program to turn a decimal into a fraction. Writes the number in numerator/denomenator form.

C++ program to turn a decimal into a fraction

Output: 


I would recommend to visit C++ Program to simplify fractional form before understanding the code of this program.

Code:

#include <iostream>
#include <cmath>

void simplify_fraction(int num, int denom);

using namespace std;

int main()
{
double number;
cout << "Enter Number: ";
cin >> number;

double fractional_part = number - floor(number);

for (double i = 0.1, j = 10;  ; i/=10, j*=10)
{
if ( fractional_part >= i )
{
simplify_fraction(number * j, j);
break;
}
}
return 0;
}


void simplify_fraction(int num, int denom)
{
for (int i = denom; i>=2; i--)
{
if (num % i == 0 && denom % i == 0)
{
num = num/i;
denom = denom/i;
}
}
cout << "Fraction: ";
if (denom == 1)
cout << num;
else
cout << num << "/" << denom;
cout << endl;
}

C++ Program to simplify fractional form in C++

C++ Program to simplify fractional form in C++

Outputs: 

Code:
#include <iostream>
using namespace std;

int main()
{
int num;
int denom;
char sign;
cout << "Enter fraction like 100/2: ";
cin >> num >> sign >> denom;

if (sign != '/')
exit(0); // This program is for division Only

for (int i = denom; i>=2; i--)
{
if (num % i == 0 && denom % i == 0)
{
num = num/i;
denom = denom/i;
}
}
cout << "Simplified: ";
if (denom == 1)
cout << num;
else
cout << num << "/" << denom;
cout << endl;

return 0;
}

Program that counts the number of keys pressed until the user presses the ‘!’ key in C++


//Program that counts the number of keys pressed until the user presses the ‘!’ key in C++

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

using namespace std;

int main()
{
int a = 0;
while (!GetAsyncKeyState(VkKeyScan('x')))
{
getch();
a++;
Sleep(100);
}
cout << "Others Keys Pressed " << a-1 << " times before pressing !." << endl;
return 0;
}

C++ Program to Search a keyword (in character type array or) through the user entered text.

C++ Program to search through character type array
/*
C++ Program to Search a keyword through the user entered text.
*/









//#include <iostream>

#include <iostream>
using namespace std;

int main()
{
cout << "Write somethig:\n";
char *string = new char [100];
cin.getline (string,100,'\n');

cout << endl << "Search: ";
char *keyword = new char [10];
cin.getline (keyword,10,'\n');

for (int i = 0, j = 0, k = 0, m = 0; string[j] != '\0'; i++,j++)
{
if (keyword[i] == '\0')
{
i = 0;
m = 0;
j = ++k;
}

if ( keyword[i] == string[j] )
{
m++;
}
if (m == strlen(keyword))
{
cout << "FOUND" << endl;
exit (0);
}
}
cout << "Not Found" << endl;
return 0;
}

C++ basic percentage program

/*
WRITE A PROGRAM THAT WILL ASK SOMEONE HOW THEY SPEND THEIR DAY. aT LEAST 5 QUESTIONS:
HOW MANY HOUR DO YOU SPEND SLEEPING,
HOW MANY HOURS FO SPEND EATING,
DRINKING,
WALKING,
WORKING,.
CALCULATE PERCENTAGES
THE OUTPUT SHOULD LOOK SOMETHING LIKE THIS.
SLEEPING EATING DRINKING WALKING WORKING
40.00% 40.00% 7.00% 4.00% 9.00%
*/



#include <iostream>
using namespace std;

int main()
{
cout << "You spend time (in hours) in 1 day: " <<endl;
cout << "In Playing: "; int play; cin >> play;
cout << "In Sleeping: "; int sleep; cin >> sleep;
cout << "In Eating: "; int eat; cin >> eat;
cout << "In Working: "; int work; cin >> work;

cout << "In percentage: " << endl;
cout << "Playing: " << play/24.0 * 100 << "%" << endl;
cout << "Sleeping: " << sleep/24.0 * 100 << "%" << endl;
cout << "Eating: " << eat/24.0 * 100 << "%" << endl;
cout << "Working: " << work/24.0 * 100 << "%" << endl;

return 0;
}

C++: Suppose you are organizing a pizza and will give every person who attends exactly 3 slices of pizza. write program that accepts a number of people and then computes the minimum number of pizzas that you must order assuming there are 8 slices per pizza. Your program then outputs the number of pizzas needed and the number of slices that will be left over.

/*
C++ program:
Suppose you are organizing a pizza and will give every person who attends exactly 3 slices of pizza. write program that accepts a number of people and then computes the minimum number of pizzas that you must order assuming there are 8 slices per pizza. Your program then outputs the number of pizzas needed and the number of slices that will be left over.
*/

Output

Solution:

int main()
{
cout << "Peoples in the Party: "; int people; cin >> people;
int Pizzas_required = ceil(people * 3.0/8.0);
cout << Pizzas_required << " Pizzas required." << endl; // 1 person will eat 3/8th Pizza.
cout << Pizzas_required*8 - people*3 << " pieces left." << endl;
return 0;
}



Age and Sleeping time Calculator in C++

/*
A GUIDE TO PROGRAMING IN C++ BY TIM CORICA
Write a program that calculates the number of hours of your life that you have spent sleeping. Assume that you sleep 8 hours each night. To simplify the problem , assume that there are always 30 days in each month , and 65 days in each year. The program output should look like this:

Enter your birthdate(using numbers for months):
month:9
day:27
year:1984
Enter today's date (using numbers for months):
month:10
day:26
year:2002
you have been alive for 6599 days.
you have slept for 52792 hours.

*/




#include <iostream>
using namespace std;

int main()
{
cout << "Enter your birthdate(using numbers for months): " << endl;
cout << "Month: "; int m; cin >> m;
cout << "Day: "; int d; cin >> d;
cout << "Year: "; int y; cin >> y;

cout << "\nEnter today's date (using numbers for months): " << endl;
cout << "Month: "; int tm; cin >> tm; // tm => Today's month
cout << "Day: "; int td; cin >> td;
cout << "Year: "; int ty; cin >> ty;

unsigned long int days_from_dateOfBirth = (365*ty + tm*30 + td) - (365*y + m*30 + d);
cout << "You have been alive for " << days_from_dateOfBirth << " days." << endl;
cout << "You have slept for " << days_from_dateOfBirth * 8 << " hours." << endl;
return 0;
}


C++: Write a function that finds the element closest in value from 2-d array to the number entered by user. The row and column indices of the element closest in value to the element should be the output.

/*
Write a function that finds the element closest in value from 2-d array to the number entered by user. The row and column indices of the element closest in value to the element should be the output.
*/



Code:

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

void closest_number_finder(int [][5], int);

int main()
{
cout << "Array:" << endl << endl;
int array[5][5];
for (int r = 0; r<5; r++)
{
for (int c = 0; c<5; c++)
{
array[r][c] = rand()%101 - 50; //Storing random numbers from -50 to 50.
cout << char(179) << setw(3) << array[r][c] << setw(3) << char(179);
}
cout << endl;
}

cout << "\nEnter a Number, I'll tell nearest of this number from my array: ";
int user;
cin >> user;
closest_number_finder(array, user);

return 0;
}

void closest_number_finder(int array[][5], int user)
{
int difference[5][5];

for (int r = 0; r<5; r++)
{
for (int c = 0; c<5; c++)
{
difference[r][c] = fabs(array[r][c] - user);
}
}
int R, C;
//Now we've to find the smallest difference.
int smallest = difference[0][0];
for (r = 0; r<5; r++)
{
for (int c = 0; c<5; c++)
{
if (smallest > difference[r][c])
{
smallest = difference[r][c];
R = r; C = c;
}

}
}

cout << "Closest to your entered number is " << array[R][C] << " at table[" << R << "][" << C << "]" << " with a difference of " << smallest << endl;
}

Whether a given string (char array) is Palindrome or not in C++


/*
Write a function to check whether a given string (char array) is
Palindrome or not. Your program should take only one parameter char array and
should return 1 if string is Palindrome otherwise it must return 0.
Note: A string is a Palindrome if it is same whether read from left to right or from
right to left. For example aba is a Palindrome.
*/

#include <iostream>
using namespace std;

bool Palindrome(char []);

int main()
{
cout << "Write something, I'll tell whether it's Palindorme or Not:\n\n";
char s[50];
cin.getline(s,50,'\n');

cout << "\nYou "<< ((Palindrome(s) == 1)? "":"did not ")<<  "entered a Palindrome String." << endl;

return 0;
}

bool Palindrome(char s[])
{
//we'll locate Null character in the string.
for (int i = 0; s[i] != '\0'; i++);
//Now we've Null character at s[i].
//therefore we can start comparing to know whether this string is palindrome or not.
//int Null_Character = i.
i--;// Now s[i] is the last character of string.
for (int j = 0; j<i; j++,i--)
{
if (s[j] != s[i])
{
return 0;
}
}
return 1;
}

Prime Number Checker in C++

//Prime Number Checker in C++. Program to check whether Entered Number is Prime Number or not.

#include <iostream>
using namespace std;

int main()
{
cout <<"Enter a Number, I'll tell u, whether it's Prime or Not: ";
int x;
cin>>x;

bool n=0;

for (int i = 2; i<= (x-1); i++)
{
if ( x%i == 0 )
{
n = 1;
break;
}
}

cout << x << ((n==1)? " is not ": " is ") << "prime number"<<endl;

return 0;
}

Seperate 5 digits in C++


//Program to seperate 5 digits in C++

#include <iostream>
using namespace std;

int main()
{
int x;
cin>>x;

int a,b,c,d,e;

a = x/10000;
b = (x/1000)%10;
c = (x/100)%10;
d = (x/10) %10;
e = x%10;

cout << "First: "<<a<<endl;
cout << "Second: "<<b<<endl;
cout << "Third: "<<c<<endl;
cout << "Fourth: "<<d<<endl;
cout << "Fifth: "<<e<<endl;


return 0;
}

Generic Digits Seperator in C++


//Generic Digits Seperator in C++
//Output: Only 1st line is Entered by User.


#include <iostream>

using namespace std;

int main()
{
int x;
cin >> x;
int a[50];
int i = 0;

while (x != 0)
{
a[i] = x%10;
x /= 10;
i++;
}

i--;
int j = 0;

for (; i>=0; i--,j++)
{
cout << j <<". " << a[i] << endl;
}

return 0;
}

Program for the addition of 2 matrices of any Order in C++

//Program for the addition of 2 matrices of any Order in C++.


#include <iostream>
using namespace std;

int main()
{
cout << "Size of Matrix A:";
int rows1, columns1;
cin>>rows1>>columns1;
//Initializing a 2-d dynamic array.
int ** m1 = new int *[rows1];

for (int i = 0; i<rows1; i++)
m1[i] = new int [columns1];

//Inputing Matrix.
cout << "Enter Matrix A\n";
for (int r = 0; r<rows1; r++)
for (int c = 0; c<columns1; c++)
cin>>m1[r][c];
//--------------------------------------
cout << "Size of Matrix B:";
int rows2, columns2;
cin>>rows2>>columns2;

int ** m2 = new int *[rows2];

for (i = 0; i<rows2; i++)
m2[i] = new int [columns2];

//Inputing Matrix.
cout << "Enter Matrix B\n";
for ( r = 0; r<rows2; r++)
for (int c = 0; c<columns2; c++)
cin>>m2[r][c];
//--------------------------------------

//Printing Matrix A.
cout << "\nA:\n";
for ( r = 0; r<rows1; r++)
{
for (int c = 0; c<columns1; c++)
cout << m1[r][c] << ' ';

cout << endl;
}

//Printing Matrix B.
cout << "\nB:\n";
for ( r = 0; r<rows2; r++)
{
for (int c = 0; c<columns2; c++)
cout << m2[r][c] << ' ';

cout << endl;
}


//A+B
cout << endl <<endl << "A+B =\n";
for (r = 0; r<rows1; r++)
{
for (int c = 0; c<columns1; c++)
cout << m1[r][c] + m2[r][c] << ' ';

cout << endl;
}


return 0;
}

Place a zero in each element of an array passed to it in C++

Write a function & a program to test it that will place a zero in each element of an array passed to it from the calling program. Pass the size of the array as well.



#include <iostream>
using namespace std;

void zero(char [], int);

int main()
{
char a[100];
cin.getline(a,100,'\n');

zero(a,100);

cout << a << endl;

return 0;
}

void zero (char a[], int size)
{
for (int i = 0; i < size; i++)
if (a[i] != '0')
a[i] = '0';
}


Pass 2-d array to function which will initialize all of its elements to Zero

/*
Write a program to perform the following functions.
Declare a 5*4 int array in the main ().
Pass it to another function that will initialize all of its elements to zero,
without using the subscript operator [ ].
*/




#include <iostream>
using namespace std;
void zero( char [][4], int, int);
int main()
{
char a[5][4];

zero(a,5,4);

//Printing array
for (int r=0; r<5; r++)
{
for (int c=0; c<4; c++)
cout << a[r][c];

cout << endl;
}
return 0;
}

void zero (char a[][4], int r, int c)
{
for (int j = 0; j<4; j++)
for (int i = 0; i<5; i++)
{
*(*(a+i)+j) = '0';
}
}

"Don't let anyone ever make you feel like you don't deserve what you want."