Sunday 13 November 2016

Program that accept a salary and return tax


Write a function that accepts a salary and returns the tax according to the following rules:   No tax for first Rs.1000 , 5% tax for second Rs.1000 , 4% tax for third Rs.1000,  3% for remained untaxed salary

#include<iostream.h>
#include<conio.h>
int TAX(int sal);
void main()
{
      clrscr();
      int salary , tax;
      cout<<"Enter salary: ";
      cin>>salary;
      tax = TAX(salary);
      if(tax == 0)
          cout<<"No Tax";
      else
         cout<<"The Tax is : "<<tax;
      getch();
}
int TAX(int sal)
{
      int tax;
      if(sal > 3000)
         return (5*1000)/100
}
Share:

Friday 3 June 2016

Program that accept two numbers and find power of first number up to second number.

Write a program that declares a function accepting two parameters. The first parameter is floating point number and second number is an integer. The program should multiply the floating point number by itself the number of times indicated by the integer. The function should return the result to the main function. The main function should ask the user for the floating point number and integer.It should then call the function and store the result in the variable and main function should  display it.


#include<iostream.h>
#include<conio.h>
float mutliply (float n1, int n2);
void main()
{
      clrscr();
      float num1, r;
      int num2;
      cout<<"Enter a floating point number and an integer: ";
      cin>>num1>>num2;
      r = multiply(num1, num2);
      cout<<num1<<" multiplied to itself "<<num2<<" times is : "<<r;
      getch();
}

float multiply(float n1, int n2)
{
      float n=1;
      for(int i = 0; i <n2; i++)
            n = n * n1;
      return n;
}

Share:

Write a program that prompts the user for Cartesian coordinates of two points x1, y1, x2, y2 and displays the distance between them. Write a function distance() with input four parameters to compute the distance. The function returns distance to the calling function.

#include<iostream.h>
#include<conio.h>
#include<math.h>
float Distance(int , int , int , int);
void main()
{
      clrscr();
      int cx1, cy1, cx2, cy2;
      float result;
      cout<<"Enter coordinates of  first point: ";
      cin>>cx1>>cy1;
      cout<<"Enter coordinates of second point: ";
      cin>>cx2>>cy2;
      reult = Distance (cx1, cy1,cx2,cy2);
      cout<<"The distance between given point is : "<<result;
      getch();
}

float Distance(int x1,int y1, int x2, int y2)
{
      return sqrt(pow(x2-x1),2)+pow((y2-y1),2));
}
Share:

Write a program that inputs five numbers and passes the to a function one at a time.The function returns true if the integer is even and false otherwise.

#include<iostream.h>
#include<conio.h>
char check (int n);
void main()
{
      clrscr();
      int arr[5];
      char r;
      cout<<"Enter five numbers: "<<endl;
      for(int i=0; i<5; i++)
            cin>>arr[i];
      for(i = 0; i<5; i++)
      {
            r = check(arr[i]);
            if(r == 't')
                cout<<arr[i]<<" is even. "<<endl;
            else
               cout<<arr[i]<<"  is not even. "<<endl;
      }
      getch();
}

char check(int n)
{
      if(n%2 == 0)
            return 't';
      else
            return 'f';
}
Share:

Write a function that returns the smallest of three floating point numbers.

#include<iostream.h>
#include<conio.h>
float smallest (float a, float b , float c);
void main()
{
      clrscr();
      float n1, n2, n3;
      cout<< "Enter three floating point numbers: ";
      cin>>n1>>n2>>n3;
      cout<<"The smallest floating number is: "<<smallest(n1,n2,n3);
      getch();
}

float smallest (float a , float b , float c)
{
      if(a<b && a<c)
            return a;
      else if(b<a && b<c)
            return b;
      else
            return c;
}
Share:

Write a program that calls two functions Draw_Horizontal and Draw_Vertical to construct a rectangle.

#include<iostream.h>
#include<conio.h>
void Draw_Horizontal();
void Draw_Vertical();
void main()
{
        clrscr();
        cout<<"Rectangle : "<<endl;
        Draw_Horizontal();
        Draw_Vertical();
        Draw_Horizontal();
        getch();
}
void Draw_Horizontal()
{
        for(int i= 0; i<14; i++)
          cout<<"-";
        cout<<endl;
}
void  Draw_Vertical()
{
        for(int i = 0; i<5 ; i++)
        {
          for(int j = 0; j<14; j++)
                if(j == 0 || j == 13)
                      cout<<"|";
                else
                      cout<<" ";
                cout<<endl;
        }
}

                                                        OR
#include<iostream.h>
#include<conio.h>
void Draw_Horizontal(int l);
void Draw_Vertical(int w, int l);
void main()
{
        clrscr();
        int len, wid;
        cout<<"Enter length of rectangle: ";
        cin>>len;
        cout<<"Enter width of rectangle: ";
        cin>>wid;
        cout<<"Rectangle : "<<endl;
        Draw_Horizontal(len);
        Draw_Vertical(wid, len);
        Draw_Horizontal(len);
        getch();
}
void Draw_Horizontal(int l)
{
        for(int i=0; i<1; i++)
                cout<<"*";
        cout<<endl;
}
void Draw_Vertical(int w, int l)
{
        for(int i=0; i<w-2; i++)
        {
         for(int j= 0; j<1; j++)
                if(j == 0 || j == 1-1)
                        cout<<"*";
                else
                        cout<<" ";
                cout<<endl;
        }
}

Share:

Write a function LCM() that receives two integer arguments and return LCM

#include<iostream.h>
#include<conio.h>
int LCM(int n1, int n2);
void main()
{
        int num1, num2, lcm;
        do
        {
                clrscr();
                cout<<"Enter two integers: ";
                cin>>num1>>num2;
        }
        while (num1 <= 0 || num2 <= 0)
        lcm= LCM(num1, num2);
        cout<<"\n LCM of  "<<num1<<" and "<<num2<<" = "<<lcm;
        getch();
}
int LCM (int n1, int n2)
{
        int prod, temp, gcd;
        prod = n1 * n2;
        while(n2 != 0)
        {
                if((n1 % n2) == 0)
                        break;
                else
                {
                        temp = n1 % n2;
                        n1 = n2;
                        n2 = temp;
                 }
        }
        gcd = n2;
        return prod / gcd;
}
Share:

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:

Tuesday 31 May 2016

Write a program that prints a triangle of stars

#include<iostream.h>
#include<conio.h>
void star_triangle(int height);
void main()
{
      clrscr();
      int n;
      cout<<"Enter height of triangle: ";
      cin>>n;
      star_triangle(n);
      getch();
}
void star_triangle(int height)
{
      for(int i=0; i<height; i++)
      {
            for(int j=0; j<= i; j++)
                  cout<<"*";
            cout<<endl;
      }
}

Share:

Write a program that counts the number of zeros, odd and even numbers.

#include<iostream.h>
#include<conio.h>
void Count(int arr[], int num);
void main()
{
     clrscr();
     int n, num[50];
     cout<<"How many numbers you want to enter: ";
     cin>>n;
     for(int i=0; i<n; i++)
     {
        cout<<"Enter number: ";
        cin>>num[i];
     }
     Count(num , n);
     getch();
}
void Count (int arr[], int num)
{
      int even, odd, zero;
      even = odd = zero = 0;
      for(int i= 0; i<num; i++)
      {
            if(arr[i]%2 == 0 && arr[i]!= 0)
                  even++;
            if(arr[i] %2 != 0)
                  odd++;
            if(arr[i] == 0)
                  zero++;
      }
      cout<<"Total numbers of zeros entered: "<<zero<<endl;
      cout<<"Total numbers of odds entered: "<<odd<<endl;
      cout<<"Total numbers of evens entered: "<<even;

}

Share:

Write a function that convert binary to decimal

#include <iostream.h>
#include<conio.h>
void main()
{
      clrscr();
      int a,b,c,f,g,d[32],e[32]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
      unsigned long int deci;
      char ch;
      cout<<"Enter binary number(maximum 32 digits):";
      c = 31;
      b = 0;
      while ((ch = getch()) != '\r')
      {
          d[b] = int(ch)-48;
          b++;
      }
      for(int w=(b-1); w>=0; w--)
      {
          e[c] = d[w];
          c--;
       }
       deci =0;
       g = 31;
       for(f=0; f<32; f++)
       {
           deci += (pow(2,g)*e[f]);
           g--;
       }
       cout<<"\n Decimal equivalent of binary number is : "<<deci;
       getch();
}

Share:

Write a Program that inputs a decimal number and convert it to binary digits.

#include<iostream.h>
#include<conio.h>
void binary(unsigned long int decimal)
{
      int remainder; 
      if(decimal <= 1)
      {
       cout<<decimal;
       return; 
      }
      remainder = decimal % 2; 
      binary(decimal >>1);
      cout<< reminder; 
}
void main()
{
      unsigned long int num; 
      clrscr(); 
      cout<<"Enter decimal"; 
      cin>> num; 
      cout<<endl<<"Binary of "<<num<<" is ";
      binary(num);
      getch();
}

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