Intro to C
  • Introduction to Programming in C
  • Session 1
    • session1.c
  • Session 2
    • session2.c
  • Session 3
    • session3.c
    • veggies.c
  • Session 4
    • session4.c
  • Session 5
    • session5.c
    • session5p.c
  • Session 6
    • session6.c
    • session6p.c
  • Session 7
    • session7.c
    • session7p.c
    • tictactoe.c
  • Session 8
    • session8.c
    • session8p.c
  • Session 9
    • session9.c
    • session9p.c
    • data3.txt
    • problem4.txt
    • problem5.txt
    • problem6.txt
    • hello.txt
    • sentences.txt
    • people.csv
    • students.csv
    • powerball.csv
  • Session 10
    • session10.c
    • Linked List Code
    • Double Linked List Code
Powered by GitBook
On this page
  1. Session 3

veggies.c

#include <stdio.h>

int main() {
     // Menu
     printf("Menu\n");
     printf("1 - apple\n");
     printf("2 - banana\n");
     printf("3 - cherry\n");
     printf("4 - asparagus\n");
     printf("5 - broccoli\n");
     printf("6 - carrot\n");

     // get choice from user
     printf("Choice: ");
     int choice;
     scanf("%d", &choice);

     // rewrite code below using switch
     if (choice == 1) {
         printf("fruit\n");
         printf("sugary\n");
         printf("yummy\n");
     } else if (choice == 2) {
         printf("fruit\n");
         printf("sugary\n");
         printf("yummy\n");
     } else if (choice == 3) {
         printf("fruit\n");
         printf("sugary\n");
         printf("yummy\n");
     } else if (choice == 4) {
         printf("veggie\n");
         printf("healthy\n");
         printf("green\n");
     } else if (choice == 5) {
         printf("veggie\n");
         printf("healthy\n");
         printf("green\n");
    } else if (choice == 6) {
         printf("veggie\n");
         printf("healthy\n");
         printf("orange\n");
    } else {
         printf("Invalid\n");
    }
}
Previoussession3.cNextsession4.c

Last updated 1 year ago