Monday, March 31, 2014

Calculator Program using Switch Statement and Functions without using built-in library functions. Sum , Subtraction, Product , Division , Remainder, Power , Factorial

#include<stdio.h>
#include<conio.h>
void lo()
{
int i;
    printf("\n");
for(i=1;i<=35;i++)
    printf("=");
}
void header()
{
printf("\n");
printf("Built and Designed by Arslan-ud-Din-Shafiq\n\nReg. No .DDP-SP14-BSE-005");
printf("\n");
}
void line()
{
    int u;
    for(u=1;u<=48;u++)
    printf("-");
}
int sum(int a,int b)
{
    return a+b;
}
int subtract(int c , int d)
{
    return c-d;
}
int multiply(int e,int f)
{
    return e*f;
}
int divide(int g,int h)
{
    return g/h;
}
int mod(int i,int j)
{
    return i%j;
}
int fact(int k)
{
    int i,p=1;
    for(i=1;i<=k;i++)
    p=p*i;
    return p;
}
int pwr(int n , int r)
{
    int k, number=1;
    for(k=0;k<r;k++)
    number*=n;
    return number;
}

int main()
{
    lo();
    header();
    lo();
    int x,y;
    char m;
    while(m!=-1 && m!=-1)
       {
        printf("\nEnter 1st Number or (-1 to Exit from Program)= ");
        scanf("%d",&x);
        if(x==-1)
        {
            line();
            printf("\nYou are getting out of this program.....Press Enter.\n");
            line();
            break;
        }
        printf("\nEnter 2nd Number =  ");
        scanf("%d",&y);
        printf("\nYou can use following Arithmatic Operations (+,-,*,/,%,!)=  ");
        m=getche();
        printf("\n");
        switch(m)
    {
        case'+':
        line();
        printf("\nSum of %d and %d is = %d\n\n",x,y,sum(x,y));
        line();
        break;
        case'-':
        line();
        printf("\nSubtraction of %d and %d is = %d\n",x,y,subtract(x,y));
        line();
        break;
        case'*':
        line();
        printf("\nProduct of %d and %d is = %d\n",x,y,multiply(x,y));
        line();
        break;
        case'/':
        line();
        printf("\nDivision of %d and %d is = %d\n",x,y,divide(x,y));
        line();
        break;
        case'%':
        line();
        printf("\nModulus of %d and %d is = %d\n",x,y,mod(x,y));
        line();
        break;
        case'!':
        line();
        printf("\nFactorial of %d is = %d\n",x,fact(x));
        line();
        printf("\nFactorial of %d is = %d\n",y,fact(y));
        line();
        break;
        case'^':
        line();
        printf("\n%d raised to power %d is = %d\n",x,y,pwr(x,y));
        line();
        break;
        default:
        line();
        printf("\nInvalid Input\n");
        line();
    }

       }
return 0;
}


No comments:

Post a Comment