Friday, 3 June 2016

Write a program that inputs a number in main() function and passes it to a function. The function displays whether number is prime or not.

#include<iostream.h>
#include<conio.h>
void prime(int n);
void main()
{
        clrscr();
        int num;
        cout<<"Enter a number: ";
        cin>> num;
        prime(num);
        getch();
}
void prime(int n)
{
        int p =1;
        for(int j=2; j<n; j++)
                if(n%j == 0)
                {
                        p = 0;
                        break;
                }
        if(p == 1)
                cout<<"The number is prime";
        else
                cout<<"The number is not prime";
}
Share:

Write a program that prompts the user to enter a number and reverse it. Write a function Reverse() to reverse the number. The function should accept the number as the parameter and return the reverse number.

#include<iostream.h>
#include<conio.h>
int Reverse(int num);
void main()
{
        clrscr();
        int n, r;
        cout<<"Enter a number: ";
        cin>>n;
        r = Reverse(n);
        cout<<"The reverse number is : "<<r;
        getch();
}

int Reverse(int num)
{
        int rev_num = 0;
        while(num > 0)
        {
                rev_num = rev_num * 10 + num % 10;
                num = num / 10;
        }
        return rev_num;
}
Share:

Write a program that inputs two numbers in main function and passes them to a function. The function displays first number raised to the power of second.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void power(int n1, int n2);
void main()
{
        clrscr();
        int num1, num2;
        cout<<"Enter two numbers: ";
        cin>>num1>>num2;
        power(num1, num2);
        getch();
}
void power(int n1, int n2)
{
        cout<<n1<<" raised to the power of "<<n2<<" is : "<<pow(n1,n2);
}
Share:

Wednesday, 1 June 2016

Write a Program that uses a function EQ() to find whether four integers a, b, c and d satisfy the equation a^3+b^3+c^3=d^3 or not. The function returns 0 if the equation is satisfied and return -1 otherwise.

#include<iostream.h>
#include<conio.h>
#include<math.h>
int EQ(int a, int b, int c, int d);
void main()
{
        clrscr();
        int n1, n2, n3, n4, r;
        cout<<"Enter four integers: ";
        cin>.n1>>n2>>n3>>n4;
        r = EQ(n1,n2,n3,n4);
        if(r == 0)
                cout<<"The values of a, b, c and d satisfies the equation";
        if(r== -1)
                cout<<"The values a, b, c and d does not satisfied the equation";
        getch();
}
int EQ(int a, int b, int c, int d)
{
        if(pow(a,3)+pow(b,3)+pow(c,3) == pow(d,3))
                return 0;
        else
                return -1;
}

Share:

Write a program that inputs two numbers in main function and passes them to a function. The function displays first number raised to the power of second.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void power(int n1, int n2);
void main()
{
        clrscr();
        int num1,  num2;
        cout<<"Enter two numbers: ";
        cin>>num1>>num2;
        power(num1, num2);
        getch();
}
void power(int n1, int n2)
{
        cout<<n1<<" raised to the power of "<<n2<<" is : "<<pow(n1,n2);
}

Share:

Write a program that accept two integers.Create a function that tells whether or not the first integer is multiple of second.

#include<iostream.h>
#include<conio.h>
void check_multiple(int n1, int n2);
void main()
{
       clrscr();
       int int1, int2;
       cout<<"Enter 1st integer: ";
       cin>>int1;
       cout<<"Enter 2nd Integer: ";
       cin>>int2;
       check_multiple(int1, int2);
       getch();
}
void check_multiple(int n1, int n2)
{
       if(n2 % n1 == 0)
              cout<<"First integer " <<n1<<" is the multiple of second integer "<<n2;
       else
              cout<<"First integer "<<n1<<" is not the multiple of second integer "<<n2;
}

Share:

Write a program that enters and integer form the user and displays the Fibonacci numbers.

#include<iostream.h>
#include<conio.h>
void fibonacci(int n);
void main()
{
       int num;
       clrscr();
       cout<<"Enter number upto which fibonacci numbers are required: ";
       cin>>num;
       cout<<"Fibonacci  numbers upto "<<num<<" are: "<<endl;
       fibonacci(num);
       getch();
}
void fibonacci(int n)
{
       int a , b, next;
       a = 0;
       b = 1;
       cout<<a<<" "<<b;
       next = a+b;
       while(next <= n)
       {
              cout<<" "<<next;
              a = b ;
              b = next;
              next = a+b;
       }
}

Share:

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages

About Me

My photo
A group of two, with a little hope of helping beginners to learn how to code :)
Powered by Blogger.

Comments

Facebook

Ad Home

Follow Us

Random Posts

Sponsor

Recent Posts

Header Ads

Popular Posts

Copyright © Functions in C++ | Powered by Blogger

Design by ThemePacific | Blogger Theme by NewBloggerThemes.com | Distributed By Blogger Templates20