Uploaded by skyler lynch

c programming example

advertisement
Example 1: Checking if a number is even or odd
#include <stdio.h>
int main() {
int num = 5;
if (num % 2 == 0) {
printf("The number is even\n");
} else {
printf("The number is odd\n");
}
return 0;
}
Example 2: Checking if a student passed or failed
#include <stdio.h>
int main() {
int score = 60;
if (score >= 50) {
printf("The student passed\n");
} else {
printf("The student failed\n");
}
return 0;
}
Example 3: Checking if a character is a vowel or consonant
#include <stdio.h>
int main() {
char ch = 'a';
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
printf("The character is a vowel\n");
} else {
printf("The character is a consonant\n");
}
return 0;
}
Example 4: Checking if a number is positive, negative or zero
#include <stdio.h>
int main() {
int num = -5;
if (num > 0) {
printf("The number is positive\n");
} else if (num < 0) {
printf("The number is negative\n");
} else {
printf("The number is zero\n");
}
return 0;
}
Example 5: Checking if a number is divisible by 3 and 5
#include <stdio.h>
int main() {
int num = 15;
if (num % 3 == 0 && num % 5 == 0) {
printf("The number is divisible by 3 and 5\n");
} else {
printf("The number is not divisible by 3 and 5\n");
}
return 0;
}
Download