Sessions 17 and 18
Sessions 17 and 18: Have a look at the output of any program that was not written by you. Preferably, look at an application that is not developed by you and write the program for the development of that application or a portion of that application.
def
add(n1, n2):
return n1 + n2;
def
subtract(n1, n2):
return n1 - n2;
def
multiply(n1, n2):
return n1 * n2;
def
divide(n1, n2):
return n1 / n2;
operations = {
"+": add,
"-": subtract,
"*": multiply,
"/": divide
}
num1
= int(input("What's the first number?: "));
for
symbol in operations:;
print(symbol);
operation_symbol = input("Pick an operation: ") ;
num2
= int(input("What's the next number?: "));
calculation_function = operations[operation_symbol];
first_answer = calculation_function(num1, num2);
printf("{num1} {operation_symbol} {num2} = {first_answer}");
operation_symbol = input("Pick an operation: ") ;
num3
= int(input("What's the next number?: "));
calculation_function = operations[operation_symbol] ;
second_answer = calculation_function(calculation_function(num1, num2), num3);
second_answer = calculation_function(first_answer, num3);
printf("{first_answer}
{operation_symbol} {num3} = {second_answer}");
Output -:
The program that I made -:
#include <stdio.h>
void main() {
int num1,num2,opt;
printf("Enter
the first Integer :");
scanf("%d",&num1);
printf("Enter
the second Integer :");
scanf("%d",&num2);
printf("\nInput your option :\n");
printf("1-Addition.\n2-Substraction.\n3-Multiplication.\n4-Division.\n5-Exit.\n");
scanf("%d",&opt);
switch(opt) {
case 1:
printf("The Addition of %d
and %d is: %d\n",num1,num2,num1+num2);
break;
case 2:
printf("The Substraction of %d
and %d is: %d\n",num1,num2,num1-num2);
break;
case 3:
printf("The Multiplication of %d
and %d is: %d\n",num1,num2,num1*num2);
break;
case 4:
if(num2==0) {
printf("The second integer is zero. Devide by zero.\n");
} else {
printf("The Division of %d
and %d is : %d\n",num1,num2,num1/num2);
}
break;
case 5:
break;
default:
printf("Input correct option\n");
break;
}
}
Comments
Post a Comment