Dive into the world of C programming through these examples of C program basic program!
Explore and learn a variety of basic concepts with tested programs that are guaranteed to work on any platform. Use these examples as your stepping stones to master C programming! Below is the tabular list of basic programs that are covered in this article, going through these programs and practicing them thoroughly will help you to master the C programming language and become a proficient programmer.
C Program Basic Program List |
1. C Program To Print “Hello World”. |
2. C Program To Print An Integer Entered By The User. |
3. C Program To Add Two Integers. |
4. C Program To Multiply Two Floating Point Numbers. |
5. C Program To Compute Quotient And Remainder. |
6. C Program To Swap Two Numbers. |
7. C Program To Check Whether The Number Is Even Odd. |
8. C Program To Check Whether The Character Is Vowel Or Consonant. |
9. C Program To Find A Factorial Of A Number. |
10. C Program To Write Multiplication Tables. |
11. C Program To Find The LCM Of Numbers. |
12. C Program To Print All Alphabets From A To Z Using Loop. |
Prerequisites Before Learning C Program Basic Program
Before going through these programs, it is advised to have basic knowledge of all the concepts written below-
- C Data Types
- C Variables and Constants
- C Input Output (I/O)
- C Programming Operators
We’ve created a detailed article that explains all these topics thoroughly. For a better understanding of the programs, we recommend you to visit and review these concepts once.
1. C Program To Print “Hello World”
Below is a basic program with output, written in the C programming language to print ‘Hello, World!’
C Program Basic Program To Print “Hello World” |
#include <stdio.h>
int main() { printf(“Hello, World!\n”); return 0; } |
Output-
Hello, World! |
2. C Program To Print An Integer Entered By The User
This C program asks the user to enter an integer. Upon receiving the input, it reads the entered integer and prints it to the console.
#include <stdio.h>
int main() { int number; printf(“Enter an integer: “); scanf(“%d”, &number); printf(“You entered: %d\n”, number); return 0; } |
Output-
You Entered: “ ” |
3. C Program to Add Two Integers
This C program takes two integer inputs from the user and adds them together. It asks the user to enter the first integer, reads and stores it, then asks for the second integer and does the same. After that, it calculates the sum of the two integers and displays the result.
C Program Basic Program To Add Two Integers |
#include <stdio.h>
int main() { int num1, num2, sum; // Prompt the user to enter the first integer printf(“Enter first integer: “); scanf(“%d”, &num1); // Prompt the user to enter the second integer printf(“Enter second integer: “); scanf(“%d”, &num2); // Calculate the sum of the two integers sum = num1 + num2; // Print the result printf(“The sum of %d and %d is: %d\n”, num1, num2, sum); return 0; } |
Output-
Enter First integer: “5” Enter Second integer: “7” The sum of 5 and 7 is: “12” |
4. C Program To Multiply Two Floating Point Numbers
This C program multiplies two floating-point numbers entered by the user. It prompts the user to enter the first and second floating-point numbers, stores them in variables `num1` and `num2` respectively, calculates their product, and then prints the result to the console.
C Program Basic Program |
#include <stdio.h>
int main() { float num1, num2, product; // Asks the user to enter the first floating point number printf(“Enter first floating point number: “); scanf(“%f”, &num1); // Asks the user to enter the second floating point number printf(“Enter second floating point number: “); scanf(“%f”, &num2) // Calculate the product of the two floating point numbers product = num1 * num2; // Print the result printf(“The product of %.2f and %.2f is: %.2f\n”, num1, num2, product); return 0; } |
Output-
Enter first floating point number: 3.5 Enter second floating point number: 2.5 The product of 3.50 and 2.50 is: 8.75 |
5. C Program To Compute Quotient And Remainder
This C program is designed to compute the quotient and remainder when dividing two integers. It first asks the user to enter the dividend and then the divisor. After receiving the input, the program calculates the quotient by performing integer division using the formula dividend / divisor. It then computes the remainder using the modulo operator (%) with the expression dividend % divisor. The results, namely the quotient and the remainder, are printed to the console.
C Program Basic Program |
#include <stdio.h>
int main() { int dividend, divisor, quotient, remainder; // Asks the user to enter the dividend printf(“Enter dividend: “); scanf(“%d”, ÷nd); // Asks the user to enter the divisor printf(“Enter divisor: “); scanf(“%d”, &divisor); // Compute the quotient quotient = dividend / divisor; // Compute the remainder remainder = dividend % divisor; // Print the results printf(“Quotient: %d\n”, quotient); printf(“Remainder: %d\n”, remainder); return 0; } |
Sample Output-
Enter dividend: 20 Enter divisor: 3 Quotient: 6 Remainder: 2 |
6. C Program To Swap Two Numbers
C Program Basic Program |
#include <stdio.h>
int main() { int a, b, temp; // Asks the user to enter the first number printf(“Enter first number: “); scanf(“%d”, &a); // Asks the user to enter the second number printf(“Enter second number: “); scanf(“%d”, &b); // Print the original values printf(“Before swapping: a = %d, b = %d\n”, a, b); // Swap the values using a temporary variable temp = a; a = b; b = temp; // Print the swapped values printf(“After swapping: a = %d, b = %d\n”, a, b); return 0; } |
Sample Output-
Enter first number: 5 Enter second number: 10 Before swapping: a = 5, b = 10 After swapping: a = 10, b = 5 |
7. C Program To Check Whether The Number Is Even Odd
This simple C program is designed to determine whether a given integer is even or odd. It begins by asking the user to input an integer. Upon receiving the input, the program checks whether the number is divisible by 2 or not. If the remainder is zero, it concludes that the number is even and prints “The number is even.” Otherwise in all other cases, indicating that the number is not divisible by 2, the program concludes that the number is odd and prints “The number is odd.” This logical assessment is facilitated by an if-else statement.
C Program Basic Program |
#include <stdio.h>
int main() { int number; // Tells the user to enter a number printf(“Enter an integer: “); scanf(“%d”, &number); // Check if the number is even or odd by dividing the number by two. if (number % 2 == 0) { printf(“%d is even.\n”, number); } else { printf(“%d is odd.\n”, number); } return 0; } |
Sample Output-
Enter an integer: 4 4 is even. Enter an integer: 9 9 is odd. |
8. C Program To Check Whether The Character Is Vowel Or Consonant
This C program prompts the user to enter a character. It then checks whether the entered character is a vowel or a consonant. Using a series of logical OR (||) operators, the program compares the entered character with predefined lowercase and uppercase vowels. If the entered character matches any of the vowels, it prints that the character is a vowel. If not, it prints that the character is a consonant.
C Program Basic Program |
#include <stdio.h>
int main() { char ch; // Asks the user to enter a character printf(“Enter a character: “); scanf(” %c”, &ch); // Check if the character is a vowel if (ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’ || ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch == ‘O’ || ch == ‘U’) { printf(“%c is a vowel.\n”, ch); } else { printf(“%c is a consonant.\n”, ch); } return 0; } |
Sample Output-
Enter a character: B B is a consonant. |
9. C Program To Find A Factorial Of A Number
This C program calculates the factorial of a number entered by the user. It first prompts the user to enter a number. If the entered number is negative, it prints an error message since the factorial of a negative number doesn’t exist. Otherwise, it calculates the factorial using a loop. Inside the loop, the program multiplies the current value of the factorial by the loop variable. After the loop, it prints the calculated factorial.
C Program Basic Program |
#include <stdio.h>
int main() { int n, i; unsigned long long fact = 1; printf(“Enter an integer: “); scanf(“%d”, &n); // shows an error if the user enters a negative integer if (n < 0) printf(“Error! Factorial of a negative number doesn’t exist.”); else { for (i = 1; i <= n; ++i) { fact *= i; } printf(“Factorial of %d = %llu”, n, fact); } return 0; } |
Sample Output-
Enter a number: 5 Factorial of 5 = 120 |
10. C Program To Write Multiplication Tables
This C program generates the multiplication table of a number entered by the user. It asks the user to input a number. Then, it displays the multiplication table of that number from 1 to 10. It accomplishes this using a “for loop” that iterates from 1 to 10, printing each multiplication operation.
C Program Basic Program |
#include <stdio.h>
int main() { int number; // Asks the user to enter a number printf(“Enter a number: “); scanf(“%d”, &number); // Display the multiplication table printf(“Multiplication Table of %d:\n”, number); for (int i = 1; i <= 10; ++i) { printf(“%d x %d = %d\n”, number, i, number * i); } return 0; } |
Sample Output-
Enter a number: 5 Multiplication Table of 5: 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50 |
11. C Program To Find The LCM Of Numbers
C Program Basic Program |
#include <stdio.h>
int main() { int n1, n2, max; printf(“Enter two positive integers: “); scanf(“%d %d”, &n1, &n2); // maximum number between n1 and n2 is stored in max max = (n1 > n2) ? n1 : n2; while (1) { if ((max % n1 == 0) && (max % n2 == 0)) { printf(“The LCM of %d and %d is %d.”, n1, n2, max); break; } ++max; } return 0; } |
Sample Output-
Enter two positive integers: 12, 18 The LCM of 18 and 12 is 36. |
12. C Program To Print All Alphabets From A To Z Using Loop
C Program Basic Program |
#include <stdio.h>
int main() { char ch; // Display characters from ‘A’ to ‘Z’ printf(“Characters from A to Z:\n”); for (ch = ‘A’; ch <= ‘Z’; ++ch) { printf(“%c “, ch); } printf(“\n”); return 0; } |
Sample Output-
Characters from A to Z: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z |
Why Choose C Language For Learning?
Choosing C as a language to learn has several advantages:
- Fundamental Concepts: C is a foundational programming language that teaches fundamental programming concepts. Learning C provides a solid understanding of basic programming principles like variables, data types, control structures, functions, and arrays.
- Procedural Programming: C is a procedural programming language, which means it follows a sequential approach to programming. This makes it easier to understand the flow of the program, making it an excellent choice for beginners.
- Portability: C programs are highly portable which means that they can run on different operating systems with minimal or no modifications.
- Widely Used: C is one of the most widely used programming languages in the world. It is used in the development of operating systems, system software, embedded systems, and various applications across different domains.
- Foundation for Other Languages: Many modern programming languages are derived from or influenced by C. Learning C provides a solid foundation for learning other languages such as C++, Java, and C#.
Learn C Program Basic Program With PW Skills
Learning C programming is often the initial step in most people’s programming journey. As the primary programming language, it forms the foundational base for both beginners and professionals.
Physics Wallah offers a comprehensive C++ programming course that starts from the basics and covers all advanced topics. By enrolling in this course, you’ll gain insights from experienced teachers, benefit from regular doubt-clearing sessions, receive daily practice sheets, and access a dedicated PW lab for coding practice to enhance your skills.
Enroll now to avail exciting offers and kickstart your programming journey with confidence!
C Program Basic Program FAQs
What is #include in C Program?
#include is a preprocessor directive that tells the compiler to include the Standard Input Output library before compiling the program. This library provides functions for input and output operations.
What Does int main() in C Program?
Int main() is the starting point of a C program. It is a function where the execution of the program begins. The int before main indicates that the function returns an integer value.
What is printf in a C program and How Does It Work?
printf is a function in the stdio.h library that is used to print text to the screen. For example, printf("Hello, World!\n"); prints "Hello, World!" on the screen.