Tuesday, October 27, 2015

Odd or Even in C++







#include <iostream>
using std::cin;
using std::cout;
using std::endl;
 
int main()
{
    int x;
 
   cout << "Enter integer :\n";
  cin >> x;
 
  if(x % 2 == 0)
    cout << "The integer is even.\n";
  if(x % 2 == 1)
     cout <<  "The integer is odd.\n";
 
 
   return 0;
}
 
 

Write a program that calculates the squares and cubes of the integers from 0 to 10














#include <iostream>
using std::cin;
using std::cout;
using std::endl;
 
int main()
{
   cout << "integer\t\tsquare\t\tcube\n";
  cout << 0 << "\t\t" << 0*0 << "\t\t" << 0*0*0 << "\n";
  cout << 1 << "\t\t" << 1*1 << "\t\t" << 1*1*1 << "\n";
  cout << 2 << "\t\t" << 2*2 << "\t\t" << 2*2*2 << "\n";
  cout << 3 << "\t\t" << 3*3 << "\t\t" << 3*3*3 << "\n";
  cout << 4 << "\t\t" << 4*4 << "\t\t" << 4*4*4 << "\n";
  cout << 5 << "\t\t" << 5*5 << "\t\t" << 5*5*5 << "\n";
  cout << 6 << "\t\t" << 6*6 << "\t\t" << 6*6*6 << "\n";
  cout << 7 << "\t\t" << 7*7 << "\t\t" << 7*7*7 << "\n";
  cout << 8 << "\t\t" << 8*8 << "\t\t" << 8*8*8 << "\n";
  cout << 9 << "\t\t" << 9*9 << "\t\t" << 9*9*9 << "\n";
  cout << 10 << "\t\t" << 10*10 << "\t\t" << 10*10*10 << "\n";
 
   return 0;
}

Write a program that inputs a five-digit integer, separates the integer into its digits and prints them separated by three spaces each.






#include <iostream>
using std::cin;
using std::cout;
using std::endl;
 
int main()
{
   int fivedigit;
   int first, second, third, fourth, fifth;
 
    cout << "Please enter the five digit integer: ";
  cin >> fivedigit;
 
    if(fivedigit < 10000)
    {
    cout << "Incorrect digit! Try again!";
  }
 
  if(fivedigit > 99999)
  {
    cout << "Incorrect digit! Try again!";
  }
 
  first = fivedigit/10000;
  second = (fivedigit/1000)%10;
  third = (fivedigit/100)%10;
  fourth = (fivedigit/10)%10;
  fifth = fivedigit%10;
 
  cout << first;
  cout << " " << second;
  cout << " " << third;
   cout << " " << fourth;
   cout << " " << fifth;
 
   return 0;
}

Checkerboard pattern with eight output statements in C++










#include <iostream>
using std::cin;
using std::cout;
using std::endl;
 
int main()
{
  cout << "* * * * * * * * \n";
  cout << " * * * * * * * *\n";
  cout << "* * * * * * * * \n";
  cout << " * * * * * * * *\n";
  cout << "* * * * * * * * \n";
  cout << " * * * * * * * *\n";
  cout << "* * * * * * * * \n";
  cout << " * * * * * * * *\n";
   cout << endl;
  cout << "* * * * * * * * \n * * * * * * * *\n* * * * * * * * \n * * * * * * * *\n* * * * * * * * \n * * * * * * * *\n* * * * * * * * \n * * * * * * * *\n";
 
 
   return 0;
}

Guessing number game in C++

#include <stdlib.h>
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
srand(time(NULL));    
int number=rand()%100; 
int guess=-1;
int trycount=0;
while(guess!=number && trycount<8)

{
cout<<"Please enter a guess: ";
cin>>guess;

if(guess<number)
cout<<"Too low"<<endl;

if(guess>number)
 cout<<"Too high"<<endl;

trycount++;
}
if(guess==number)
cout<<"You guessed the number";
else
cout<<"Sorry, the number was: "<<number;
return 0;
}

What is your name? in C++








// Example program
#include <iostream>
#include <string>

int main()
{
  std::string name;
  std::cout << "What is your name? ";
  getline (std::cin, name);
  std::cout << "Hello, " << name << "!\n";
}

i/o example in C++





// i/o example

#include <iostream>
using namespace std;

int main ()
{
  int i;
  cout << "Please enter an integer value: ";
  cin >> i;
  cout << "The value you entered is " << i;
  cout << " and its double is " << i*2 << ".\n";
  return 0;

}