PROGRAM – 1
Simulation of a Simple Calculator.
#include<stdio.h>
void main( )
{
int a, b, sum, dif, pro, quo, rem;
char operator;
printf("Enter an arithmetic expression\n");
scanf("%d%c%d", &a, &operator, &b); ‘
switch(operator)
{
case ‘+’: sum = a+b;
printf("The sum is=%d\n", sum);
break;
case ‘-’ : dif = a-b;
printf("The difference is=%d\n", dif);
break;
case ‘*’: pro = a*b;
printf("The product is=%d\n", pro);
break;
case ‘/ ’: if(b!=0)
{
quo = a/b;
printf("The quotient is=%d\n", quo);
}
else
{
printf("Divide by zero error\n");
}
break;
case ‘%’: rem = a%b;
printf("The remainder is=%d\n", rem);
break;
default : printf("Invalid expression\n");
}
}
Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 1
, Principles of Programming Using C (BPOPS103)
OUTPUT:
Compilation : cc program1.c
Execution : ./a.out
Enter an arithmetic expression
2+3
The sum is=5
Enter an arithmetic expression
10-5
The difference is=5
Enter an arithmetic expression
3*4
The product is=12
Enter an arithmetic expression
6/3
The quotient is=2
Enter an arithmetic expression
7/0
Divide by zero error
Enter an arithmetic expression
10%3
The remainder is=1
Enter an arithmetic expression
7$8
Invalid expression
Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 2
, Principles of Programming Using C (BPOPS103)
PROGRAM - 2
Compute the roots of a quadratic equation by accepting the
coefficients. Print appropriate messages.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void main( )
{
int a, b, c, d;
float r1, r2;
printf("Enter the coefficients\n");
scanf("%d %d %d", &a, &b, &c);
d = b*b - 4*a*c;
if (a*b*c == 0 )
{
printf("Invalid input\n");
exit( 0 );
}
if(d == 0)
{
printf("Roots are real and equal\n");
r1=r2=-b/(2*a);
printf("Root1=%f\n", r1);
printf("Root2=%f\n", r2);
}
if ( d > 0 )
{
printf("Roots are real and distinct\n");
r1= (-b + sqrt(d)) / (2*a);
r2= (-b – sqrt(d)) / (2*a);
printf("Root1=%f\n", r1);
printf("Root2=%f\n", r2);
}
if ( d < 0 )
{
printf("Roots are imaginary\n");
Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 3
, Principles of Programming Using C (BPOPS103)
r1= -b/(2*a);
r2= sqrt(-d)/(2*a);
printf("Root1 = %f + i %f\n", r1, r2);
printf("Root2 = %f – i %f\n", r1, r2);
}
}
Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 4