Hello, World!Write a C program that prints Hello, World!.
main, printf, and standard output.
The program starts with #include <stdio.h>, which allows us to use the printf() function. The main() function is the entry point of the program. Inside it, printf("Hello, World!\n"); prints the message to the screen. The \n moves the cursor to the next line. The return 0; statement ends the program successfully.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Input: No input
Output: Hello, World!
Read an integer and determine whether it is positive, negative, or zero.
scanf, variables, comparisons, and conditional statements.
First, we declare an integer variable named num. Then we use scanf("%d", &num); to read the value entered by the user. After that, we check the value:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num > 0) {
printf("Positive\n");
} else if (num < 0) {
printf("Negative\n");
} else {
printf("Zero\n");
}
return 0;
}
Input: 12
Output: Positive
Read an integer and determine whether it is even or odd.
scanf, %, and conditional statements.
We read an integer using scanf("%d", &num);. Then we use the modulo operator % to find the remainder when the number is divided by 2. If the remainder is 0, the number is even; otherwise, it is odd. This is checked with an if condition.
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("Even\n");
} else {
printf("Odd\n");
}
return 0;
}
Input: 7
Output: Odd