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:

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