Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
program in c with switch command about two numbers and character

Write a program that asks for two numbers and a character respectively with the operation he will perform with the two numbers. If any other character is typed then the message is displayed "Wrong act". To be implemented using the switch-case command.

    #include <stdio.h>
main ()
{
int selection;
printf("Enter 2 numbers \n");
scanf("%d",&selection);
switch(selection)
{
case 1:
printf("You entered 1");
break;
case 2:
printf("You entered 2");
break;
case 3:
printf("You entered 3");
break;
default:
printf("Out of range, Try again");
}
}

Can anyone give me some advice about what to do in the switch funtcion on this particular problem?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Some Tips:

  • Every function in C must have a data type like type func() { }
  • formatting of your source code is very important
  • printf("Enter 2 numbers \n");, but in the following line you only take a single input
  • Well, C implicitly return 0, but you should write it (it may differ from compiler to compiler)
  • Must check whether scanf() input was successful or not, by checking its return value
  • Use puts() to print non-formatted text on terminal (just a block of text), and it will also print a new line [GCC and CLang automatically does that if optimization flags are used]

Final Code:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int selection;
    puts("Enter a number:");
    if(scanf("%d", &selection) !=  1)
    {
        perror("bad input");
        return EXIT_FAILURE;
    }
    switch (selection) {
    case 1:
        puts("You entered 1");
        break;
    case 2:
        puts("You entered 2");
        break;
    case 3:
        puts("You entered 3");
        break;
    default:
        puts("Out of range, Try again");
    }
    return EXIT_SUCCESS;
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!