Simulate a Simple Calculator
#include
#include
void main()
{
char operator;
float num1, num2, result;
clrscr();
printf("Simulation of a Simple Calculator\n");
printf("*********************************\n");
printf("Enter two numbers \n");
scanf("%f %f", &num1, &num2);
fflush(stdin);
printf("Enter the operator [+,-,*,/] \n");
scanf("%s", &operator);
switch(operator)
{
case '+': result = num1 + num2;
break;
case '-': result = num1 - num2;
break;
case '*': result = num1 * num2;
break;
case '/': result = num1 / num2;
break;
default : printf("Error in operationn");
break;
}
printf("\n %5.2f %c %5.2f = %5.2f\n", num1, operator, num2, result);
getch();
}
Output
Simulation of a Simple Calculator
*********************************
Enter two numbers
2 3
Enter the operator [+,-,*,/]
+
2.00 + 3.00 = 5.00
Simulation of a Simple Calculator
*********************************
Enter two numbers
50 40
Enter the operator [+,-,*,/]
*
50.00 * 40.00 = 2000.00
Simulation of a Simple Calculator
*********************************
Enter two numbers
500 17
Enter the operator [+,-,*,/]
/
500.00 / 17.00 = 29.41
Simulation of a Simple Calculator
*********************************
Enter two numbers
65000 4700
Enter the operator [+,-,*,/]
-
65000.00 - 4700.00 = 60300.00
Find the Sum of First N Natural
Numbers
#include
#include
void main()
{
int i, num, sum = 0;
clrscr();
printf("Enter an integer number \n");
scanf ("%d", &num);
for (i = 1; i <= num; i++)
{
sum = sum + i;
}
printf ("Sum of first %d natural numbers = %d\n", num, sum);
getch();
}
Output
Enter an integer number
1300
Sum of first 1300 natural numbers = 845650
Enter an integer number
15
Sum of first 15 natural numbers = 120
Find First N Fibonacci Numbers
#include
#include
void main()
{
int fib1 = 0, fib2 = 1, fib3, num, count = 0;
clrscr();
printf("Enter the value of num \n");
scanf("%d", &num);
printf("First %d FIBONACCI numbers are ...\n", num);
printf("%d\n", fib1);
printf("%d\n", fib2);
count = 2; /* fib1 and fib2 are already used */
while (count < num)
{
fib3 = fib1 + fib2;
count++;
printf("%d\n", fib3);
fib1 = fib2;
fib2 = fib3;
}
getch();
}
Output
Enter the value of num
15
First 15 FIBONACCI numbers are ...
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
No comments:
Post a Comment