Program 1: Hello, World!
#include <stdio.h>
int main() {
printf(“Hello, World!n”);
return 0;
}
Explanation:
#include <stdio.h>: This line includes the standard input/output library for using functions likeprintfandscanf.int main(): This is the main function where the program execution starts.printf("Hello, World!n");: This line prints the message “Hello, World!” to the screen.return 0;: This line indicates that the program has executed successfully.
Program 2: Simple Addition
#include <stdio.h>
int main() {
int num1, num2, sum;
num1 = 5;
num2 = 7;
sum = num1 + num2;
printf(“Sum: %dn”, sum);
return 0;
}
Explanation:
int num1, num2, sum;: Declare integer variablesnum1,num2, andsum.num1 = 5;andnum2 = 7;: Assign values tonum1andnum2.sum = num1 + num2;: Calculate the sum ofnum1andnum2.printf("Sum: %dn", sum);: Print the result, which is the sum ofnum1andnum2.
Program 3: Simple If-Else
#include <stdio.h>
int main() {
int num;
printf(“Enter a number: “);
scanf(“%d”, &num);
if (num > 0) {
printf(“Positive numbern”);
} else if (num < 0) {
printf(“Negative numbern”);
} else {
printf(“Zeron”);
}
return 0;
}
Explanation:
int num;: Declare an integer variablenum.printf("Enter a number: ");: Prompt the user to enter a number.scanf("%d", &num);: Read the user’s input and store it in the variablenum.- The
if-elseblock checks ifnumis positive, negative, or zero, and prints the corresponding message.
Program 4: Finding the Maximum of Two Numbers
#include <stdio.h>
int main() {
int num1, num2;
printf(“Enter two numbers: “);
scanf(“%d %d”, &num1, &num2);
if (num1 > num2) {
printf(“Maximum: %dn”, num1);
} else {
printf(“Maximum: %dn”, num2);
}
return 0;
}
Explanation:
#include <stdio.h>: This line includes the standard input/output library for using functions likeprintfandscanf.int num1, num2;: Declares two integer variables,num1andnum2.printf("Enter two numbers: ");: Displays a message prompting the user to enter two numbers.scanf("%d %d", &num1, &num2);: Reads two integer values entered by the user and stores them innum1andnum2.- The
if-elseblock checks ifnum1is greater thannum2and prints the maximum value accordingly.
Program 5: Factorial Calculation
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf(“Enter an integer: “);
scanf(“%d”, &n);
if (n < 0)
printf(“Factorial is not defined for negative numbers.n”);
else {
for (i = 1; i <= n; i++) {
fact *= i;
}
printf(“Factorial of %d = %llun”, n, fact);
}
return 0;
}
Explanation:
int n, i;: Declares integer variablesnandiand an unsigned long long variablefact.printf("Enter an integer: ");: Prompts the user to enter an integer.scanf("%d", &n);: Reads the integer entered by the user and stores it inn.- The program calculates the factorial of
nusing aforloop. - The program handles negative input and displays an appropriate message.
Program 6: Simple For Loop
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
printf(“%dn”, i);
}
return 0;
}
Explanation:
int i;: Declares an integer variablei.- The
forloop runs from 1 to 5 and prints the value ofiin each iteration.
Program 7: Sum of Natural Numbers
#include <stdio.h>
int main() {
int n, sum = 0;
printf(“Enter a positive integer: “);
scanf(“%d”, &n);
for (int i = 1; i <= n; i++) {
sum += i;
}
printf(“Sum of the first %d natural numbers = %dn”, n, sum);
return 0;
}
Explanation:
int n, sum = 0;: Declares integer variablesnandsum.printf("Enter a positive integer: ");: Prompts the user to enter a positive integer.scanf("%d", &n);: Reads the integer entered by the user and stores it inn.- The
forloop calculates the sum of the firstnnatural numbers. - The result is displayed as “Sum of the first n natural numbers = sum.”
Program 8: Check for Even or Odd
#include <stdio.h>
int main() {
int num;
printf(“Enter an integer: “);
scanf(“%d”, &num);
if (num % 2 == 0) {
printf(“%d is even.n”, num);
} else {
printf(“%d is odd.n”, num);
}
return 0;
}
Explanation:
- This program checks if a given integer is even or odd and prints the result.
if (num % 2 == 0): Checks if the remainder ofnumdivided by 2 is equal to 0, which is the condition for an even number.printf("%d is even.n", num);: Prints that the number is even if the condition is true, otherwise, it prints that the number is odd.
Program 9: Calculate the Average of N Numbers
#include <stdio.h>
int main() {
int n, i;
float sum = 0, num;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
printf(“Enter %d numbers:n”, n);
for (i = 0; i < n; i++) {
scanf(“%f”, &num);
sum += num;
}
float average = sum / n;
printf(“Average = %.2fn”, average);
return 0;
}
Explanation:
- This program calculates the average of a set of numbers entered by the user.
int n, i;andfloat sum = 0, num;: Declare integer variablesnandiand float variablessumandnum.- The program reads the number of elements and the numbers from the user and calculates their sum.
- The average is calculated and displayed with two decimal places using
%.2f.
Program 10: Display Multiplication Table
#include <stdio.h>
int main() {
int num, i;
printf(“Enter an integer: “);
scanf(“%d”, &num);
printf(“Multiplication Table for %d:n”, num);
for (i = 1; i <= 10; i++) {
printf(“%d x %d = %dn”, num, i, num * i);
}
return 0;
}
Explanation:
- This program displays the multiplication table for a given integer.
printf("Multiplication Table for %d:n", num);: Prints the title for the multiplication table.- The program uses a
forloop to calculate and display the results for the multiplication table from 1 to 10.
