on Leave a Comment

C++ Program to Convert Celsius to Fahrenheit

In this article you will learn about how to convert Celsius to Fahrenheit in c++ 

Formula, F = C * 9/5 + 32

C++ Program to Convert Celsius to Fahrenheit

#include<iostream>
using namespace std;
int main()
{
        float cen, fah;
        cout<<"Enter temperature in Celsius : ";
        cin>>cen;
        fah=(cen * 9/5) + 32;
        cout<<"\nTemperature in Fahrenheit : "<<fah;
        return 0;
}

OUTPUT:

Enter temperature in Celsius : 39

Temperature in Fahrenheit : 102.2

on Leave a Comment

C++ Program to Find GCD

GCD stands for Greatest Common Divisor.

GCD or HCF of two integer is greatest common number that can divide both integer.

C++ Program to Find GCD

#include <iostream>
using namespace std;
int main()
{
    int a,b,g;
    cout<<"Enter two numbers ";
    cin>>a>>b;
    for(g=a>b?a:b; g>=1; g--)
        if(a%g==0 && b%g==0)
            break;
    cout<<"\nGreatest Common Factor(GCD) of "<<a<<" and "<<b<<" is "<<g;
    return 0;
}

OUTPUT 1:

Enter two numbers 69
72

Greatest Common Factor(GCD) of 69 and 72 is 3


OUTPUT 2:

Enter two numbers 15
20

Greatest Common Factor(GCD) of 15 and 20 is 5

See also:




on Leave a Comment

C++ Program to Find LCM of Two Numbers

LCM of two numbers is least common multiple of both number.

C++ Program to Find LCM of Two Numbers

#include <iostream>
using namespace std;
int main()
{
    int a,b,x;
    cout<<"Enter two numbers ";
    cin>>a>>b;
    for(x=1;x<=a*b;x++)
        if(x%a==0 && x%b==0)
            break;
    cout<<"\nLeast Common Multiple(LCM) of "<<a<<" and "<<b<<" is "<<x;
    return 0;
}

OUTPUT:

Enter two numbers 3
9

Least Common Multiple(LCM) of 3 and 9 is 9

Example 2: C++ Program to Find LCM of Two Numbers

#include <iostream>
using namespace std;
int main()
{
    int a,b,x;
    cout<<"Enter two numbers ";
    cin>>a>>b;
    for(x=a>b?a:b;x<=a*b;x++)
        if(x%a==0 && x%b==0)
            break;
    cout<<"\nLeast Common Multiple(LCM) of "<<a<<" and "<<b<<" is "<<x;
    return 0;
}

OUTPUT:

Enter two numbers 4
6

Least Common Multiple(LCM) of 4 and 6 is 12

Example 3: C++ Program to Find LCM of Two Numbers

#include <iostream>
using namespace std;
int main()
{
    int a,b,x;
    cout<<"Enter two numbers ";
    cin>>a>>b;
    for(x=a>b?a:b;x<=a*b;x=x+(a>b?a:b))
        if(x%a==0 && x%b==0)
            break;
    cout<<"\nLeast Common Multiple(LCM) of "<<a<<" and "<<b<<" is "<<x;
    return 0;
}

OUTPUT:

Enter two numbers 12
18

Least Common Multiple(LCM) of 12 and 18 is 36

See also:




on Leave a Comment

C++ Program to Convert Binary to Decimal

This article contains C++ Program to Convert Binary Number to Decimal Number.

C++ Program to Convert Binary Number to Decimal Number

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    int bin,i=0,decnum=0,rem;
    cout<<"Enter a binary number: ";
    cin >>bin;
    while(bin!=0)
    {
        rem=bin%10;
        bin/=10;
        decnum=decnum+rem*pow(2,i);
        ++i;
    }
    cout<<"\nDecimal equivalent is "<<decnum;
    return 0;
}

OUTPUT:

Enter a binary number: 10001

Decimal equivalent is 17


on Leave a Comment

Friend Function in C++

This article contains Friend Function in C++.

Friend Function in C++

We know that private members of class cannot be accessed outside the class. But when there is need to access the private member outside class, then friend comes in use. A friend function is friend of class, but not a member function.

The friend function is declared inside the class using friend keyword. The friend function can be defined anywhere in the program. The friend function definition does not use either friend keyword or scope resolution operator.

Declaration of Friend Function

Declaration of friend function must be written inside the class using friend keyword. A friend function can be friend of more than one class, so its declaration is written to every class.

Syntax:

class class_name
{
          ...
          ...
public:
          ...
          friend return_type function_name(list of arguments);
}; 

Example:

class A
{
        int a;
public:
        friend void setdata(A,int);
};

Definition of Friend Function

A friend function is defined outside the class. It is defined outside the class without using friend keyword and scope resolution operator.

Example:

class A
{
        int a;
public:
        friend void setdata(A,int);
};
void setdata(A.obj, int x)
{
        obj.a=x;
}

Accessing of Friend Function

Friend function is not a member function of  a class, so it can not be accessed by object of that class. A friend function can indirectly access private members of class.

Friend function can be called like a simple function.

Example:

#include<iostream>
using namespace std;
class A
{
    int a;
public:
    friend void setdata(A &,int);
    void putdata()
    {
        cout<<"\na = "<<a;
    }
};
void setdata(A &obj, int x)
{
    obj.a=x;
}
int main()
{
    A o1;
    setdata(o1,10);
    o1.putdata();
    return 0;
}

OUTPUT:

a=10;