Showing posts with label iostream. Show all posts
Showing posts with label iostream. Show all posts

Sunday, August 23, 2015

Using Switch Case to convert Digit to Word


Q. 88. Write a program to input a digit and convert the Digit to corresponding Word and Print it.



Copy Code: 

#include <iostream>
using namespace std;

int main()
{
  char a ;
  cout << "Enter a digit ranging from 0 to 9 : "; cin >>a;
  
  switch (a)
  {
     case '0' : cout << "ZERO"; break;

     case '1' : cout << "ONE"; break;

     case '2' : cout << "TWO"; break;

     case '3' : cout << "THREE"; break;

     case '4' : cout << "FOUR"; break;

     case '5' : cout << "FIVE"; break;

     case '6' : cout << "SIX"; break;

     case '7' : cout << "SEVEN"; break;

     case '8' : cout << "EIGHT"; break;

     case '9' : cout << "NINE"; break;

     default  : cout << "Not a Digit"; break;
   }

  return 0;
}




Using Ternary Operator Check Even Odd number


Q. 89. Using C++ Ternary Operator, input a number and check if its Even or Odd.

Copy Code:


#include <iostream>
using namespace std;

int main()

{
  
   int a ;
  
   cout << "Enter a number ";
  
   cin   >> a;
  
   cout << (a%2==0 ? "EVEN" : "ODD");

  return 0;

}


Upto 5 Multiples of Any Number - While Loop


Q. 90. Program to print upto 5 multiples of any number using "while" loop.

Copy Code:


#include <iostream>

using namespace std;

int main()

{

  int a, i=1;

  cout << "Enter a number: ";
  
  cin >> a;

  while(i<=5)

  {

     cout << a << " * " << i << " = " <<a * i << "\n";

     ++i; 

  }

return 0;

}

Multiplication Table of Any Number

Q. 91. Write a program to accept a number and print its Multiplication Table. Also, prompt the user to Continue the process or Not. 



Copy Code:

#include <iostream>

using namespace std;

int main()

{

  int a, i ; char ch ;

  do

  {

     cout << "Enter a number ";
     
     cin >> a;

     for (i=1 ; i<=10 ; ++i)

       {
           cout << a << " * " << i << " = " << a * i << "\n";
       }

     cout << "Do you want to continue (Y/N) ? : ";
     
     cin >> ch ;

   }while (ch == 'y'  ||  ch == 'Y') ;

return 0;

}


Use Break, Continue to find Odd numbers within n Natural Numbers

 Q. 92. Find Odd  Numbers between 1 and "n" using "break" and "continue".



Copy Code:

#include <iostream>

using namespace std;

int main()

{

  int n, i=0 ;

  cout << "Enter a number : ";

  cin >> n ;

  cout << "Odd numbers upto " << n << " are : ";

  do

  {

     ++i ;

     if (i % 2 == 0 )

        continue ;

     cout << i << " " ;

  } while ( i < n) ;

 

return 0;

}

Find Numbers Above Average - Array Manipulation

Q. 93. Write a program to accept "n" numbers into array. Find the Average of n Numbers and print all numbers that are Above Average.



Copy Code:


#include <iostream>
using namespace std;

int main()

{
  int a[100], n, i, sm=0, av=0 ;
  cout << "How many numbers can you provide? -> ";
  cin >> n;
  cout << "Okay... \nEnter " << n << " numbers \n" ;
  for ( i = 0 ; i < n ; ++i)
   {
      cin >> a [ i ] ; 
      sm = sm + a[ i ] ;
   }

  av = sm/n ;
  cout << "Average is : " << av ;
  cout << "\nNumbers above Average " << av << " are : \n" ;

  for (i=0 ; i<n ; ++i)
   {
      if (a[ i ] > av)
         cout << a[ i ] << " ";
   }

  return 0;
}

Biggest Number in Array and its Position

Q. 94. Program to input 'n' numbers into an array. Find the biggest among them and print its position (array index position + 1).


Copy Code:

#include <iostream>

using namespace std;

int main()

{

  int a[25], n, i, bg = 0, pn = 0; 

  cout << " How many numbers can you provide? -> " ;

  cin >> n ;

  cout << "Enter " << n << " numbers \n" ;

  

  for ( i = 0 ; i < n ; ++i)

  {

     cin >> a [ i ] ; 

     if (a[ i ] > bg)

      {

         bg = a[ i ] ; pn = i ;

      }

  }

  cout << "Biggest Number is -> " << bg << " at position " << pn+1;

  return 0;

}





Program To Find Word Length


Q. 95. Write a C++ program to accept a Word and find out length of the Word (the number of characters).



Copy Code:

#include <iostream>
using namespace std;

int main()

{
  int i = 0, len = 0; char ch [50];

  cout << "Enter any Word : ";
  cin >> ch ;

  while (ch [ i ] != '\0' ) 
   {
      len+=1;  i+=1;  
   }
  cout << "Total Number of Characters = " << len;

  return 0;
}


Check if a Word is Palindrome


Q. 96. Write a C++ program to input a Word and check if it is a Palindrome (eg: madam, civic, kayak).

Note: Ignore case sensitiveness (eg: madam or Madam are both Palindrome) 



Copy Code:

#include <iostream>
#include <cctype>
using namespace std;

int main()

{
  int i = 0, len = 0; char ch [50];

  cout << "Enter a Word : ";
  cin >> ch ;

  while (ch [ i ] != '\0')
   {
      len+=1;  i+=1;
    }

  for ( i=0 ; i <= len/2 ; ++i )
  {
     if ( toupper(ch[ i ]) != toupper(ch[ len - i - 1]))
     
        {
            cout << ch << " is Not a Palindrome";
            goto e; 
        }    
   }

  cout << "GREAT !!! " << ch << " is a PALINDROME";
  
  e:   

  return 0;
}




Factorial of a number using functions

Q. 97 Write a C++ program to find the Factorial of a number using functions.



Copy Code:

#include <iostream>
using namespace std;

int main()

{
  int a; int fact( int n );

  cout << " Enter a Number : ";
  cin >> a;
  cout << " Factorial of " << a << " = " << fact (a);
  
  return 0;
}

int fact( int n )
{
   int i, f = 1; 
   for ( i = 1 ; i <= n ; ++i )
        f = f * i ;
   return (f);
}



Check Armstrong Number


Q. 98. Program to accept a number and check if its an Armstrong number (sum of cubes of each digit = that number. Eg: 153, 371, 407).


Copy Code: 

#include <iostream>
using namespace std;

int main()

{
  int i, n, s=0, p, a[ 10 ];
  cout << " Enter a possible Armstrong Number ";  cin >> n;  p = n;
  for ( i=0 ; n>0 ; ++i )
   {
     a[ i ] = n % 10;
     n = n/10;
     s = s + a[ i ] * a[ i ] * a[ i ] ;
   }

  if (s==p)
     cout << p << " is ARMSTRONG ";
  else 
     cout << p << " is NOT ARMSTRONG";

  return 0;
}



Decimal to Binary Conversion

Q. 99. C++ program to convert a Decimal number to its Binary format.


Copy Code:

#include <iostream>
using namespace std;

int main()

{
  int i, n, a[20];
  cout << "Enter a Number : ";
  cin >> n;
  for ( i=0 ; n>0 ; ++i )
  {
     a[i] = n%2;
     n = n/2;
  }

  cout << "Binary Equivalent is : ";
  for ( i = i-1 ; i>=0 ; --i )
       cout << a[i];

  return 0;
}

Decimal to Hexadecimal Conversion

Q. 100. C++ program to convert a Decimal number to its Hexadecimal format.


Copy Code:

#include <iostream>
using namespace std;

int main()

{
  int i, n, m, a;
  char ch[20];
  cout << "Enter a Number : ";
  cin >> n;
  m = n;
  for ( i=0 ; n>0 ; ++i )
  {
     a = n%16;
     n = n/16;
     if (a<10)
     {
       ch[i] = a+48;
      }
      else
        ch[i] = a+55;
  }

  cout << "Hexadecimal Equivalent of " << m << " is : ";

  for ( i = i - 1 ; i >= 0 ; --i )
     cout << ch[i];
    
  return 0;
}

Saturday, August 22, 2015

Change Case of a Word

Q. 101. C++ Program to accept a word and change the Upper case (capital) letters to Lower case (small) and Lower case letters to Upper case.



Copy Code:

#include <iostream>
using namespace std;

int main()

{
  char ch [50]; int i = 0;
  cout << "Enter a Word : ";
  cin >> ch;
  while( ch[ i ] != '\0' )
  {
     if ( ch[ i ] >= 'a'  &&  ch[i] <= 'z' )
         ch [ i ] = ch [ i ] - 32;
     else 
     if ( ch[ i ] >= 'A'  &&  ch[i] <= 'Z' )
         ch [ i ] = ch [ i ] + 32;

     ++i;
   }
  cout << "Word with Changed Case is : "<< ch;
  return 0;
}

Convert Centigrade to Fahrenheit

Q. 104. Program to convert Centigrade into Fahrenheit.

Note: The average body temperature is 98.60F or 370C and formula is:
         F = 9/5 * C + 32




Copy Code:

#include <iostream>
using namespace std;

int main()
{
  float c, f;
  cout << "Enter a value in Centigrade : ";
  cin >> c;
  f = ((9.0/5.0) * c) + 32;
  cout << c <<" Centigrade = " << f << " Fahrenheit";
  return 0;
}


Program to print any Number in Words

Q. 108. Lets accept any positive number and convert it into Words using C++ program

For Example, 1890 must be written as One Thousand Eight Hundred and Ninety.



Copy Code -->>

#include <iostream>
#include <string>
using namespace std;

const string isEMPTY = "";

const string A[] = { isEMPTY, "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ",
"Sixteen ", "Seventeen ", "Eighteen ", "Nineteen " };

const string B[] = { isEMPTY, isEMPTY, "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "};

string number2digit(int n, string append)
{
if (n == 0) {
return isEMPTY;
}

if (n > 19) {
return B[n / 10] + A[n % 10] + append;
}
else {
return A[n] + append;
}
}

string digitToWord(long int n)
{
string res;

res = number2digit((n % 100), "");

if (n > 100 && n % 100) {
res = "and " + res;
}

res = number2digit(((n / 100) % 10), "Hundred ") + res;

res = number2digit(((n / 1000) % 100), "Thousand ") + res;

res = number2digit(((n / 100000) % 100), "Lakh, ") + res;

res = number2digit(((n / 10000000) % 100), "Crore, ") + res;

return res;
}

int main()
{
long int n;
cout <<"\nEnter any Number: ";
cin >> n;

cout << n <<" in words is: "<<digitToWord(n) ;

return 0;
}