Programming with C Language
Tutorial 05 – Writing switch condition, while, do while & for loops
Switch
Input two numbers and display the outputs of the basic mathematic operations. The output screen
should be displayed as follows;
Enter two numbers ____ ____
1. +
2. –
3. *
4. /
Please enter your Choice ___
#include <stdio.h>
int main()
{
double num1, num2;
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Modulo\n");
printf("Enter your choice: ");
int choice;
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Result of addition: %.2lf\n", num1 + num2);
break;
case 2:
printf("Result of subtraction: %.2lf\n", num1 - num2);
break;
case 3:
printf("Result of multiplication: %.2lf\n", num1 * num2);
break;
case 4:
if (num2 != 0)
{
printf("Result of division: %.2lf\n", num1 / num2);
}
else
{
printf("Cannot divide by zero!\n");
}
break;
case 5:
printf("Result of modulo: %.2lf\n", fmod(num1, num2));
break;
default:
printf("Invalid choice!\n");
break;
}
return 0;
}
While loop
1. Input 10 numbers and display the total count of odd & even numbers in the entered number
series.
#include <stdio.h>
int main()
{
int number;
int count_odd = 0;
int count_even = 0;
printf("Enter 10 numbers:\n");
int i = 1;
while (i <= 10)
{
printf("Number %d: ", i);
scanf("%d", &number);
if (number % 2 == 0)
{
count_even++;
}
else
{
count_odd++;
}
i++;
}
printf("Total count of odd numbers: %d\n", count_odd);
printf("Total count of even numbers: %d\n", count_even);
return 0;
}
2. Modify the above program in to enter series of numbers terminates when the user enter -99
and display the same expected output.
#include <stdio.h>
int main()
{
int number;
int count_odd = 0;
int count_even = 0;
printf("Enter a series of numbers (-99 to terminate):\n");
while (1)
{
printf("Number: ");
scanf("%d", &number);
if (number == -99)
{
break;
}
if (number % 2 == 0)
{
count_even++;
}
else
{
count_odd++; }
}
printf("Total count of odd numbers: %d\n", count_odd);
printf("Total count of even numbers: %d\n", count_even);
return 0;
}
Do while loop
Rewrite the programs for the above while loop question 1 & 2 using do while loop
#include <stdio.h>
int main()
{
int number;
int count_odd = 0;
int count_even = 0;
printf("Enter a series of numbers (-99 to terminate):\n");
do
{
printf("Number: ");
scanf("%d", &number);
if (number == -99)
{
break;
}
if (number % 2 == 0)
{
count_even++;
}
else
{
count_odd++;
}
}
while (1);
printf("Total count of odd numbers: %d\n", count_odd);
printf("Total count of even numbers: %d\n", count_even);
return 0;
}
For loop
1. Input 10 numbers and display the average value using the for loop
#include <stdio.h>
int main()
{
int number;
int sum = 0;
float average;
printf("Enter 10 numbers:\n");
for (int i = 1; i <= 10; i++)
{
printf("Number %d: ", i);
scanf("%d", &number);
sum += number;
}
average = (float)sum / 10;
printf("The average value is: %.2f\n", average);
return 0;
}
2. Display the following output using the for loop
*
** #include <stdio.h>
*** int main()
**** {
***** for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}