Tuesday, August 30, 2011

Separate Integer from a Decimal Number

Problem: Separate the integer part from a decimal number.


Algorithm:
1. Prompt for a number.
2. Assign the number to a variable with ‘int’ data type.
3. There you go ;)

Source Code:
#include<iostream>
#include<conio.h>
using namespace::std;
int main (void)
{
    double num;
    int integer;
    cout<<"Enter a decimal number: ";
    cin>>num;
    integer=num;        //an int can only save the integer part
    cout<<"Integer part of "<<num<<" is "<<integer;
    getch();
    return 0;
}


This is a little trick I use to separate integers. Do tell me if you have any other ideas.

Monday, August 29, 2011

Greater Number

Problem: Find which number is greater.


Algorithm:
1. Prompt for two numbers.
2. Make a decision of which number is greater.
3. Print the appropriate message.

Source Code:
#include<iostream>
#include<conio.h>
using namespace::std;
int main (void)
{
    int x,y;
    cout<<"Enter two numbers: ";
    cin>>x>>y;
    if
    (x>y)            //if x is greater
    cout<<x<<" is greater than "<<y<<endl;
    else if
    (x<y)            //if y is greater
    cout<<y<<" is greater than "<<x<<endl;
    else if (x==y)    //if both are equal
    cout<<"The numbers you entered are equal "<<endl;
    getch();
    return 0;
}

Thursday, August 18, 2011

Positive or Negative

Problem: Check whether the entered integer is negative, positive or zero.


Algorithm:
1. Prompt for the number.
2. Decide whether it is zero, greater than zero or less than zero.
3. Display the result.

Source Code:
#include<iostream>
#include<conio.h>
using namespace::std;
int main(void)
{
int num;
cout<<"Enter the number: ";
cin>>num;
//Deciding whether number is positive, negative or zero
if(num<0)
cout<<"Given number is negative"<<endl;
else if(num==0)
cout<<"Given number is zero"<<endl;
else if(num>0)
cout<<"Given number is positive"<<endl;
getch();
return 0;
}

Simple enough! There is only a decision to make in this program...


Tuesday, August 16, 2011

Average of three numbers

Problem: Find average of three numbers.


Algorithm:
1. Prompt for three numbers.
2. Calculate average by the formula.
3. Display the result.

Source Code:
#include<iostream>
#include<conio.h>
using namespace::std;
int main (void)
{
int num1;           //first number
int num2;          //second number
int num3;          //third number
int average;      //average
cout<<"Enter the first number ";
cin>>num1;
cout<<"Enter the second number ";
cin>>num2;
cout<<"Enter the third number ";
cin>>num3;
average=(num1+num2+num3)/3;         //formula for finding average
cout<<endl<<"Average of given three numbers is ";
cout<<average<<endl;
getch();
return 0;
}

Monday, August 15, 2011

Fahrenheit to Celsius

Problem: Convert given temperature from Fahrenheit to Celsius.


Algorithm:
1. Prompt for temperature in Fahrenheit.
2. Use appropriate formula to calculate its equivalent in Celsius.
3. Display the result.

Source Code:
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace::std;
int main(void)
{
double F;                    //for Fahrenheit
        double C;                   //for Celsius
cout<<"Enter temperature: ";
cin>>F;
C= 0.56 * ( F-32 );                 //formula for conversion
cout<<showpoint<<fixed;
cout<<setprecision(1);        //setting precision up to 1 decimal place
cout<<F<<"°F is equal to "<<C<<"°C"<<endl;
getch();                             //precaution for blinking screen
return 0;
}

Sunday, August 14, 2011

Round Off

Problem:   Round off a given decimal number to two decimal places.


Algorithm:
1. Input the decimal number.
2. Separate its integer part.
3. Subtract the integer part from the given number to get the fraction.
4. Multiply the fraction by 1000 (we need first 3 digits after the decimal point)
5. Find the third digit of this number by taking mode with 10.
6. If this digit is greater than or equal to 5, add 0.01 to the original decimal number else leave it as it is.
7. Set the precision to show two digits after the decimal point and display the result.

Source Code:
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace::std;
int main(void)
{
int integer;
int num;
double decimal;
double fraction;
cout<<"Enter the decimal number: ";
cin>>decimal; //Input the decimal number
integer=decimal; //Separating the integer part
fraction=decimal-integer;  //Separating the fraction part
num=(fraction*1000); // % operator in next step works only with integers
num=num%10; //Finding the 3rd digit after decimal point
cout<<fixed<<showpoint;
if(fraction>=5)
{
cout<<"Given decimal number rounded to two decimal places is: ";
cout<<setprecision(2)<<decimal+0.01<<endl;
}
else
{
cout<<"Given decimal number rounded to two decimal places is: ";
cout<<setprecision(2)<<decimal<<endl;
}
getch();
return 0;
}


This program is working fine with any kind of input. Let me know if you think something is wrong...



Saturday, August 13, 2011

Getting Started

Welcome Everyone!
I am planning to post C++ code snippets on this blog. I hope these snippets will be helpful for beginners (I'm not a pro myself). So, if you are stuck with a particular problem, you can always contact me and we will solve it together. I will love to help you out...
Enjoy programming, Its fun!

P.S: The snippets I post here are not copyright protected, you can use them anywhere you like!