Skip to main content

Simulation of a Simple Calculator Program and Algorithm Computer Science Engineering VTU

simple calculator
#include<stdio.h>
#include<stdlib.h>
void main()
{
    float a,b,c;
    char op;
    printf("enter the two operands\n");
    scanf("%f%f",&a,&b);
    printf("enter an operator\n");
    scanf(" %c",&op);
    switch (op)
{
    case '+': c=a+b;
    printf("%f+%f=%f\n",a,b,c);
    break;
    case '-': c=a-b;
    printf("%f-%f=%f\n",a,b,c);
    break;
    case '*': c=a*b;
    printf("%f*%f=%f\n",a,b,c);
    break;
    case '/': if(b!=0)
{
    c=a/b;
    printf("%f/%f=%f\n",a,b,c);
    exit(0);
}
    else
    printf("division by 0 error");
    break;
    default:
    printf("enter valid operation");
}
} 
simple calculator

Start 

Read 2 numbers as a and b

Read operator (+ or – or * or /) as op

If op is + then display a + b

Else If op is - then display a – b 

Else If op is * then display a * b 

Else If operator is / then 

 check if b is not equal to zero then display a / b 

 Otherwise display “Can not divide by 0” 

Stop


Comments

Popular posts from this blog

Raju is a Civil Engineer. He is writing software to automate his work. As a part of his requirement, he wants to calculate the circle diameter, circumference, and area. Help Raju to complete his task. Get radius as input.

OUTPUT

Write functions to implement string operations such as compare, concatenate, string length. Convince the parameter passing techniques Program and Algorithm Computer Science Engineering VTU

STRINGconcatenation #include<stdio.h> #include<stdlib.h> int length (char str[]); int compare (char str1[],char str2[]); void concatenate (char str1[],char str2[]); void main() { char str1[30],str2[30]; int choice,a,i,j; printf("enter 1-string comparision\n"); printf("enter 2-string length\n"); printf("enter 3-string concatenation\n"); printf("enter 4-exit\n"); scanf("%d",&choice); switch(choice) { case 1:printf("enter string 1\n"); scanf("%s",str1); printf("enter string 2\n"); scanf("%s",str2); a=compare(str1,str2); if(a==0) { printf("%s and%s are identical\n",str1,str2); } else { printf("%s and%s are not identical\n",str1,str2); } break; ...