Need a project done?

C++ Programming Developer

Search This Blog

Program to make Simple Calculator in C++.


/*
Simple Calculator. Write the expression as you write in your calculator. Can only handle 2 variables.
*/
#include <iostream>
using namespace std;

void main()
{
double x,y;char o;
cin>>x>>o>>y;
if (o == '+')
cout << x+y;
else
if (o == '-')
cout <<x-y;
else
if (o == '*')
cout <<x*y;
else
if (o=='/')
cout << x/y;
else
cout <<"Sntax Error!";
}

How to copy one string into another in C++?

In this program the contents of aray2 are being copied and pasted in aray.
#include <iostream>
#include <cstring>
using namespace std;

void main()
{
char aray[20];
cin.getline(aray,10,'\n'); //Prompts user to enter data
char aray2[20];
cin.getline(aray,10,'\n'); //Prompts user to enter data

char *strcpy(char * aray ,char const *aray2); //const since we don't want elements in aray2 to be changed //accidently
cout << aray <<endl;
}

Search through the elements of character type array and tell the number of each words which user enters.

/*
Program to search through elements of character type array and tells the number of Capital words.
*/

#include <iostream>
using namespace std;

int main()
{
cout << "Enter a Sentence: ";
char sentence[100];
cin.getline(sentence,100,'\n');

int tAlphabets[26] = {0};
char alphabets = 65;
int j = 0;
while (alphabets<=91)//till z.
{
for (int i = 0; i<100; i++)
{
if ( char(alphabets) == sentence[i] )
++tAlphabets[j];
}
j++;

alphabets++;
}
cout <<"CAPITAL:\n";
for (int i=0; i<26; i++)
{
if (tAlphabets[i] == 0)
continue;
cout << char(i+65) << ": " <<tAlphabets[i]<<endl;
}
}
//==============================================================
If you want your code to work for both Capital and Small letters then add a simple condition and your program will look like

/*
Program to search through elements of character type array and tells the number of Capital words.
*/

#include <iostream>
using namespace std;

int main()
{
cout << "Enter a Sentence: ";
char sentence[100];
cin.getline(sentence,100,'\n');

int tAlphabets[26] = {0};
char alphabets = 65;
int j = 0;
while (alphabets<=91)//till z.
{
for (int i = 0; i<100; i++)
{
if ( char(alphabets) == sentence[i] || char(alphabets+32) == sentence[i] )
++tAlphabets[j];
}
j++;

alphabets++;
}
for (int i=0; i<26; i++)
{
if (tAlphabets[i] == 0)
continue;
cout << char(i+65) << ": " <<tAlphabets[i]<<endl;
}
}

Convert Lower case alphabets to Upper case in C++

#include <iostream>
using namespace std;
int main()
{
 char string[20];
 cin.getline(string,20,'\n');
 for (int i = 0; i<20; i++)
 {
  if (islower(string[i]))
  {
   string[i] = char(toupper(string[i]));
  }
 }
 cout <<string;
}

Write a program to enter the values into an array using pointer notation in C++.


#include <iostream>
using namespace std;

int main()
{
int array[10];
cout <<"Enter 10 values with spaces and press enter in the end.";
int *parray = array;
for (int i = 0; i<10; i++)
cin>>*parray++;
}

Program to Copy one String in another String in C++

#include <iostream>
using namespace std;

void copy1( char *, const char * );

int main()
{
char String1[10];
char String2[] = "Hello";

copy1(String1 , String2);
cout << String1 << endl;

return 0;
}

void copy1(char *String1, const char *String2)
{
for (int i = -1;  i<10;  String1[i] = String2[i])
i++;
}
//============================================================================
You can also use following copy function.

void copy1(char *String1, const char *String2)
{
for (int i = 0; (String1[i] = String2[i]) != '\0'; i++);
}
//============================================================================
Another way: or you can use following copy function.

void copy1(char *String1, const char *String2)
{
for ( ; (*String1 = * String2) != '\0' ; String1++, String2++);
}

Pass array in a function in C++


/*
Write a program to initialize value into 1-D array. Pass the array as argument to a function and
compute the sum of array. The function should return the calculate sum to the calling function.
*/


#include <iostream>
using namespace std;

Default Arguments in C++


/*
(Default arguments)
Write a program by defining a function "time" that takes three default arguments of integer type for Hours, Minutes and Seconds.
The function should print the time in standard format. The function should be called four times as:
*Function call without using any argument.
*Function call by passing values only for hours as arguments.
*Function call by passing values of hours and minutes as arguments.
*Function call by passsing values of hours, minutes and seconds as arguments
*/

#include <iostream>
using namespace std;

void time(int hours = 12, int minutes = 0, int seconds = 0);
int main()
{
cout<<"Function called without using any argument : ";time();
cout<<"Function called by passing value only for hours as argument";time(2);
cout<<"Function called by passing values of hours and minutes as argument";time(5,4);
cout<<"Function called by passing values of hours, minuts and seconds as arguments"; time(3,2,1);

return 0;
}

void time(int hours, int minutes, int seconds)
{
cout<<hours << ":" << minutes << ":" << seconds << endl;
}

Function "swap" to exchange two values by passing arguments by reference to the function in C++


/*
Write a program by defining a function "swap" to exchange two values by passing arguments by reference to the function.
*/

#include <iostream>
using namespace std;

void swap (double &, double &);

int main()
{
int Num1,Num2;
cout<<"Number1 = "; cin>>Num1;
cout<<"Number2 = "; cin>>Num2;
swap(Num1,Num2);
cout<<endl;
cout<<"Number1 = " << Num1 << endl;
cout<<"Number2 = " << Num2 << endl;
return 0;
}

void swap(double &Num1, double &Num2)
{
int Temp = Num1;
Num1 = Num2;
Num2 = Temp;
}

Function to test whether a given integer is Even or Odd


/*
Write  a program by defining a function "even_odd" to test whether a given integer is even or odd.
Pass the integer value as a argument to function.
*/

#include <iostream>
using namespace std;

void even_odd(int);

int main()
{
int Num;
cout<< "Number: "; cin>>Num;
even_odd(Num);

return 0;
}

void even_odd(int Num)
{
if ( 0 == Num%2 ) //or u can write if(Num%2 == 0).
cout<<Num<<" is Even" << endl;
else
cout<<Num<<" is Odd"  << endl;
}

Function to display solid square of asterisks


/*
Write a program by defining a function "Square" to display solid square of asterisks (insert space between asterisks) whose side is passed a integer parameter to the function. For Example, if side is 5, then Solid Square should be displayed as
shown below.
*/

#include <iostream>
using namespace std;

Function to calculate the sum of three floating-point values


/*
Write a Program by defining a function "sum" to calculate the sum of three floating-point values. Enter the values and then pass these values to function as arguments. Print the result on Screen.
*/

#include <iostream>
using namespace std;

inline float Sum (float,float,float);

Factorial Calculator

Calculate Factorial C++:
Factorial Calculator Program in C++:

#include <iostream>
using namespace std;
"Don't let anyone ever make you feel like you don't deserve what you want."