Course Content
Basic C Programs
Here you can go through basic C Programs and understand how they work.
0/1
C Programming Basics
About Lesson

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 like printf and scanf.
  • 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 variables num1, num2, and sum.
  • num1 = 5; and num2 = 7;: Assign values to num1 and num2.
  • sum = num1 + num2;: Calculate the sum of num1 and num2.
  • printf("Sum: %dn", sum);: Print the result, which is the sum of num1 and num2.

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 variable num.
  • printf("Enter a number: ");: Prompt the user to enter a number.
  • scanf("%d", &num);: Read the user’s input and store it in the variable num.
  • The if-else block checks if num is 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 like printf and scanf.
  • int num1, num2;: Declares two integer variables, num1 and num2.
  • 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 in num1 and num2.
  • The if-else block checks if num1 is greater than num2 and 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 variables n and i and an unsigned long long variable fact.
  • printf("Enter an integer: ");: Prompts the user to enter an integer.
  • scanf("%d", &n);: Reads the integer entered by the user and stores it in n.
  • The program calculates the factorial of n using a for loop.
  • 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 variable i.
  • The for loop runs from 1 to 5 and prints the value of i in 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 variables n and sum.
  • 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 in n.
  • The for loop calculates the sum of the first n natural 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 of num divided 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; and float sum = 0, num;: Declare integer variables n and i and float variables sum and num.
  • 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 for loop to calculate and display the results for the multiplication table from 1 to 10.
0% Complete
This error message is only visible to WordPress admins

Error: No feed found.

Please go to the Instagram Feed settings page to create a feed.

X