Computer Science, asked by mjsarmap2g9e9, 9 months ago

) Request the user for two integers and outputs them and their sum. b) Request the user for two integers and outputs their remainder after division. c) Request the user for two floats and outputs their product. d) Request the user for a word and prints it twice on the same row. Write a C (main) program to provide the above functions as options to the user using switch statement and performs the functions accordingly.

Answers

Answered by poojan
5

C program to perform the given functionalities :

#include <stdio.h>

void main() {

   int a,b;

   float c,d;

   char n[10];

   char operator;

   printf("Enter an operator (Addition: +, Division: /,Multiplication: *, String printing: $ ) : ");

   scanf("%c", &operator);

   

   switch (operator) {

   case '+':

       printf("enter two integer values for addition :");

       scanf("%d %d",&a,&b);

       printf("addition :%d",(a+b),"\n");

       break;

   case '/':

       printf("enter two integer values for division : ");

       scanf("%d %d", &a,&b);

       printf("division :%f",(a/(float)b),"\n");

       break;

   case '*':

       printf("enter two float values for multiplication : ");

       scanf("%f %f",&c,&d);

       printf("multiplication: %f" ,(c*d),"\n");

       break;

   case '$':

       printf("enter the word to be printed for twice : ");

       scanf("%s",n);

       printf("word printing for twice : %s %s",n,n);

       break;

       

   default:

       printf("Error! operator is not correct");

   }

}

Inputs and outputs for all the cases :

Input 1 :

Enter an operator (Addition: +, Division: /,Multiplication: *, String printing: $ ) : +

enter two integer values for addition : 98 72

Output 1:

addition :170

Input 2 :

Enter an operator (Addition: +, Division: /,Multiplication: *, String printing: $ ) : /

enter two integer values for division : 72 13

Output 2:

division: 5.538462

Input 3:

Enter an operator (Addition: +, Division: /,Multiplication: *, String printing: $ ) : *

enter two float values for multiplication : 7.2 99.81

Output 3:

multiplication: 718.631958

Input 4 :

Enter an operator (Addition: +, Division: /,Multiplication: *, String printing: $ ) : $

enter the word to be printed for twice : hello

Output 4:

word printing for twice : hello hello

Input 5:

Enter an operator (Addition: +, Division: /,Multiplication: *, String printing: $ ) : %

Output 5:

Error! operator is not correct

Explanation :

  • Initialize the needed variables.

  • Write a print statement asking to choose a computation to be performed, from menu.

  • Take the input of user's choice and pass it through switch statement.

  • If the choice is '+', compiler takes two integers as input from the user , adds them and prints the result.

  • If the choice is '/', compiler takes two integers as input from the user , makes division operation and prints the result.

  • If the choice is '*', compiler takes two floats as input from the user, multiplies them and prints the result.

  • If the choice is '$', compiler takes a string or a word as an input from the user and prints it twice in a single row.

  • If user inputs any operator or value which isn't in the menu provided, the screen displays an error message.

Learn more :

1) C Program to generate prime numbers present in an interval.

https://brainly.in/question/12768056

2) C program to count the no.of lines present in a file.

https://brainly.in/question/9996665

Similar questions