Skip to content
The Programming School

The Programming School

  • Home
  • About
  • Contact Us
  • Toggle search form

Some Random Examples in C Language

Here are some random examples in C language

Hello Programming School!:

#include <stdio.h>

int main() {

printf("Hello Programming School!");

return 0;

}

This program is printing "Hello Programming School".
It introduces the basic structure of a C program. It includes the '#include <stdio.h>' line, which is a preprocessor directive to include the standard input/output library. The 'main' function is the entry point of the program, and 'printf' is used to print the "Hello Programming School!" message to the console.

Variables and Input:

#include <stdio.h>

int main() {

int age;


printf("Enter your age: ");
scanf("%d", &age);


printf("You entered: %d", age);

return 0;
}

This program demonstrate the use of variables and input/output. It declares an integer variable ' age ' , prompts the user to enter their age, reads the input using ' scanf ' , and then prints the entered age using ' printf ' .

Arithmetic Operations:

#include <stdio.h>

int main() {

int a = 2, b = 4;

printf("Sum : %d\n", a + b);

printf("Difference : %d\n", a - b);

printf("Product : %d\n", a * b);

printf("Quotient : %d\n", a / b);

return 0;
}

The program showcases basic arithmetic operations. It declares two integers ('a' and 'b'), performs addition, subtraction, multiplication, and division, and prints the results using 'printf'.

Conditional Statements:

#include <stdio.h>

int main() {

int number;

printf("Enter a number : ");
scanf("%d", &number);

if (number > 0) {

printf("The number is positive.");

} else if (number < 0) {

printf("The number is negative.");

} else {

printf("The number is zero.");

}

return 0;

}

This program illustrates the use of conditional statements (' if ' , ' else if ' , ' else '). It prompts the user to enter a number, reads the input, and checks whether the number is positive, negative, or zero. The results are printed accordingly.

For Loop:

#include <stdio.h>

int main() {

for (int i = 1; i <= 5; i++) {

printf("%d", i);

}

printf("\n");

return 0;

}

This example demonstrates a 'for' loop. It prints the numbers 1 through 5 using a loop structure, showcasing the basic syntax of a 'for' loop.

While Loop:

#include <stdio.h>

int main() {

int i = 1;

while (i <= 5) {

printf("%d", i);

i++;

}

printf("\n");

return 0;

}

The program uses a ' while ' loop to achive the same result as the previous examples, printing numbers 1 through 5. It shows an alternative loop structure.

Function:

#include <stdio.h>

int add(int a, int b) {

return a + b;

}

int main() {

int result = add(3, 4);

printf("Sum : %d\n", result);

return 0;

}

This program introduce functions in C. It declares a function 'add' that takes two integer parameters and returns their sum. In the 'main' function, it calls the 'add' function with arguments 3 and 4, stores the result in a variable, and then prints the sum.

  • Home
  • About
  • Contact Us

Copyright © 2025 The Programming School.

Powered by PressBook Masonry Dark