C PROGRAMMING EXERCISE
C PROGRAMMING EXERCISE
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
{
printf("%d is even.\n", num);
}
else {
printf("%d is odd.\n", num);
}
return 0;
}
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("%d is positive.\n", num);
} else if (num < 0)
{
printf("%d is negative.\n", num);
} else
{
printf("The number is zero.\n");
}
return 0;
}
3. Find Largest of Three Numbers
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a >c) {
printf("%d is the largest number.\n", a);
} else if (b >a && b >c) {
printf("%d is the largest number.\n", b);
} else {
printf("%d is the largest number.\n", c);
}
return 0;
}
1
4. Check Leap Year or Not
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
#include <stdio.h>
int main() {
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 90) {
printf("Grade A\n");
} else if (marks >= 80) {
printf("Grade B\n");
} else if (marks >= 70) {
printf("Grade C\n");
} else if (marks >= 60) {
printf("Grade D\n");
} else {
printf("Grade F\n");
}
return 0;
}
2
7. Check the input number Divisibility by 3 and 5
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b) {
printf("%d is greater.\n", a);
} else {
printf("%d is greater.\n", b);
}
return 0;
}
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
printf("%c is an alphabet.\n", ch);
} else {
printf("%c is not an alphabet.\n", ch);
}
return 0;
}
int main() {
int num1, num2;
char operator;
printf("Enter operator (+, -, *, /): ");
scanf("%c", &operator);
3
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (operator == '+') {
printf("%d %c %d = %d\n", num1, operator, num2, num1 + num2);
} else if (operator == '-') {
printf("%d %c %d = %d\n", num1, operator, num2, num1 - num2);
} else if (operator == '*') {
printf("%d %c %d = %d\n", num1, operator, num2, num1 * num2);
} else if (operator == '/') {
if (num2 != 0) {
printf("%d %c %d = %.2f\n", num1, operator, num2, (float) num1 / num2);
} else {
printf("Division by zero is not allowed.\n");
}
} else {
printf("Invalid operator.\n");
}
return 0;
}
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 100 == 0) {
printf("%d is a century year.\n", year);
} else {
printf("%d is not a century year.\n", year);
}
return 0;
}
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);