You are on page 1of 5

For loop statements

1. Write a C program to accept two integers and check whether they


are equal or not.
Test number : 15, 15
Sample Output : Number1 and Number2 are equal
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Using a for loop to repeat the process 1 time for
(int i = 1; i <= 1; i++) {
if (num1 == num2) {
printf("The numbers are equal.\n");
} else {
printf("The numbers are not equal.\n");
}
}
return 0;
}
2. Write a C program to check whether a given number is even or odd.
Test number : 15
Sample Output : 15 is an odd integer
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
// Using a for loop to repeat the process 1 time for
(int i = 1; i <= 1; i++) {
if (number % 2 == 0) {
printf("%d is an even number.\n", number);
} else {
printf("%d is an odd number.\n", number);
}
}
return 0;
}
3. Write a C program to check whether a given number is positive or
negative.
Test number : 15
Sample Output : 15 is a positive number
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
// Using a for loop to repeat the process 1 time for
(int i = 1; i <= 1; i++) {
if (number > 0) {
printf("%d is a positive number.\n", number);
} else if (number < 0) {
printf("%d is a negative number.\n", number);
} else {
printf("The number is zero.\n");
}
}
return 0;
}
4. Write a C program to find whether a given year is a leap year or not.
Test year: 2016
Sample Output : 2016 is a leap year.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// Using a for loop to repeat the process 1 time for
(int i = 1; i <= 1; i++) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
}
return 0;
}
5. Write a C program to read the age of a candidate and determine
whether he is eligible to cast his/her own vote.
Test age : 21
Sample Output : Congratulation! You are eligible for casting your vote.
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
// Using a for loop to repeat the process 1 time for
(int i = 1; i <= 1; i++) {
if (age >= 18) {
printf("You are eligible to cast your vote.\n");
} else {
printf("You are not eligible to cast your vote.\n");
}
}
return 0;
}
6. Write a C program to read the value of an integer m and display the
value of n is 1 when m is larger than 0, 0 when m is 0 and -1 when m is
less than 0.
Test Data : -5
Sample Output :
The value of n = -1
#include <stdio.h>
int main() {
int m;
printf("Enter the value of m: ");
scanf("%d", &m);
// Using a for loop to repeat the process 1 time for
(int i = 1; i <= 1; i++) {
int n;
if (m > 0) {
n = 1;
} else if (m == 0) {
n = 0;
} else {
n = -1;
}
printf("The value of n is: %d\n", n);
}
return 0;
}
7. Write a C program to accept the height of a person in
centimeters and categorize the person according to their height. Test height
: 135
Sample Output : The person is Dwarf.
#include <stdio.h>
int main() {
int height;
printf("Enter the height in centimeters: ");
scanf("%d", &height);
// Using a for loop to repeat the process 1 time for
(int i = 1; i <= 1; i++) {
if (height < 150) {
printf("Short stature.\n");
} else if (height >= 150 && height < 180) {
printf("Average height.\n");
} else {
printf("Tall stature.\n");
}
}
return 0;
}
8. Write a C program to accept a coordinate point in an XY coordinate
system and determine in which quadrant the coordinate point lies.
Test Data : 7, 9
Sample Output : The coordinate point (7,9) lies in the First
quadrant.
#include <stdio.h>
int main() {
int x, y;
printf("Enter the X-coordinate: ");
scanf("%d", &x);
printf("Enter the Y-coordinate: ");
scanf("%d", &y);
// Using a for loop to repeat the process 1 time for
(int i = 1; i <= 1; i++) {
if (x > 0 && y > 0) {
printf("The point (%d, %d) lies in the first quadrant.\n", x, y);
} else if (x < 0 && y > 0) {
printf("The point (%d, %d) lies in the second quadrant.\n", x,
y);
} else if (x < 0 && y < 0) {
printf("The point (%d, %d) lies in the third quadrant.\n", x, y);
} else if (x > 0 && y < 0) {
printf("The point (%d, %d) lies in the fourth quadrant.\n", x,
y);
} else {
printf("The point (%d, %d) lies on the axis.\n", x, y);
}
}
return 0;
}

You might also like