In: Computer Science
Problem set
if (digit == 0)
value = 3;
else if (digit == 1)
value = 3;
else if (digit == 2)
value = 6;
else if (digit == 3)
value = 9;
Speed (mph) Fine ($)
65 or less 0
66-70 15.00
71-75 30.00
76-80 75.00
over 80 100.00
switch (jersey) {
case 11:
printf("I. Thomas\n");
break;
case 23:
printf("M. Jordan\n");
break;
case 33:
printf("S. Pippen\n");
break;
default:
printf("Player unknown\n");
}
count = 5;
while (count > 0) {
print(“Woot! ”);
count -= 1;
}
Program:
#include<stdio.h>
int main() {
int digit=2;// Here initailizing the value of digit to 2
if (digit == 0)
printf("value = 3");
else if (digit == 1)
printf("value = 3");
else if (digit == 2)
printf("value = 6");
else if (digit == 3)
printf("value = 9");
}
Output:
Using Equivalent switch statement
Program:
#include<stdio.h>
int main() {
int digit=2;// Here initailizing the value of digit to 2
switch (digit) {
case 0:
printf("value = 3\n");
break;
case 1:
printf("value = 3\n");
break;
case 2:
printf("value = 6\n");
break;
case 3:
printf("value = 9\n");
break;
default:
printf("NULL\n");
}
}
Output:
Program:
#include<stdio.h>
int main() {
int Speed;
double Fine;// Here initailizing the value of digit to 2
printf("Enter Speed in kmph: ");
scanf("%d",&Speed);
if (Speed <= 65)
printf("Fine = 0");
else if (Speed >=66 &&Speed <= 70)
printf("Fine = 15.00");
else if (Speed >=71 &&Speed <= 75)
printf("Fine = 30.00");
else if (Speed >=76 &&Speed <= 80)
printf("Fine = 75.00");
else if (Speed >80)
printf("Fine = 100.00");
}
Output:
Program:
#include<stdio.h>
int main() {
int jersey=11;// Here initailizing the value of jersey to 11
switch (jersey) {
case 11:
printf("I. Thomas\n");
break;
case 23:
printf("M. Jordan\n");
break;
case 33:
printf("S. Pippen\n");
break;
default:
printf("Player unknown\n");
}
}
Output:
Using multiple-alternative if statement.
Program:
#include<stdio.h>
int main() {
int jersey=11;// Here initailizing the value of jersey to 11
if(jersey == 11)
{
printf("I. Thomas\n");
}
else if (jersey == 23)
{
printf("M. Jordan\n");
}
else if (jersey == 33)
{
printf("S. Pippen\n");
}
else
{
printf("Player unknown\n");
}
}
Output:
Program:
#include<stdio.h>
int main() { // Start of main()
int count = 5;// Here initailizing the value of count
while (count > 0) { // Here checking the condition count >
0
printf("Woot! "); // Here this statement is printed if above
condition is True
count -= 1; // Here decrementing the value of count by 1
} // En of while() loop
} // End of main()
Output: