Skip to main content

Simulation of a Simple Calculator | COMPUTER PROGRAMMING LABORATORY | 21CPL27/17


program

#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("%f",&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");

}


Algorithm

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

Implement structures to read, write and compute average- marks and the students scoring above and below the average marks for a class of N students Program and Algorithm Computer Science Engineering VTU

Structures abov below avg #include<stdio.h> struct student { char name [100]; char usn [20]; float marks; }; void main() { int i,j,k,m,n,aboveavg[100],belowavg[100]; float total=0.0,avg=0.0; struct student s[100]; printf("enter number of students\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter the details of students%d\n",i+1); printf("enter name\n"); scanf("%s",s[i].name); printf("enter usn\n"); scanf("%s",s[i].usn); printf("enter marks\n"); scanf("%f",&s[i].marks); total+=s[i].marks; } printf("enter the details of students\n"); printf("name\t\t usn\t\t marks\n"); for(i=0;i<n;i++) printf("%s\t\t %s\t\t %f\n",s[i].name,s[i].usn,s[i].marks); avg=total/n; printf("avg=%f\n",avg); j=0; k=0; for(i=0;i<n;i++) { if(s[i].marks>avg) abov...