• Articles
• Tutorials
• Interview Questions
• Webinars

# Basic C Programming Examples

Not familiar with C? This blog is intended to assist you! We’ll work through some basic C programs, simplifying ideas with clear illustrations. Prepare to begin coding and quickly gain more self-assurance!

Explore the world of C Programming with this captivating YouTube video—your gateway to a comprehensive learning experience!

## Introduction to Basic C Programming Examples

For those who are new to programming, the exploration of the below basic C programming examples provides a strong basis. Every program, from finding prime numbers to adding two numbers, has given important insights into fundamental programming ideas. Understanding these fundamentals as beginners not only opens the way for exploring a variety of software development domains but also opens the door to understanding advanced algorithms and data structures. As you explore further into the field of programming, you’ll discover that the abilities you’ve gained from these foundational courses are flexible and useful in many settings. The concepts acquired here serve as building blocks for more complex and advanced projects, from creating web applications to creating effective algorithms.

If you want to know more about C programming, you can go through this C Programming Certification Course!

## Program to Add Two Numbers

The following program uses the ‘scanf’ function to take input from the user for two numbers, adds them using the + operator, and then prints the result using the ‘printf’ function.

```#include <stdio.h>
int main() {
// Declare variables to store the two numbers
int num1, num2;
// Prompt the user to enter the first number
printf("Enter the first number: ");
scanf("%d", &num1);
// Prompt the user to enter the second number
printf("Enter the second number: ");
scanf("%d", &num2);
// Calculate the sum of the two numbers
int sum = num1 + num2;
// Display the result
printf("The sum of %d and %d is: %d\n", num1, num2, sum);
return 0;
}
```

Output:

Enter the first number: 5

Enter the second number: 8

The sum of 5 and 8 is: 13

## Program to Find the Greatest of Three Numbers

The below program takes three numbers as input from the user and then uses a series of ‘if’ and ‘else if’ statements to determine and print the greatest among them.

`#include <stdio.h>int main() {    // Declare variables to store three numbers    int num1, num2, num3;    // Prompt the user to enter the three numbers    printf("Enter the first number: ");    scanf("%d", &num1);    printf("Enter the second number: ");    scanf("%d", &num2);    printf("Enter the third number: ");    scanf("%d", &num3);    // Check and find the greatest among the three numbers    if (num1 >= num2 && num1 >= num3)        printf("%d is the greatest.\n", num1);    else if (num2 >= num1 && num2 >= num3)        printf("%d is the greatest.\n", num2);    else        printf("%d is the greatest.\n", num3);    return 0;}`

Output:

Enter the first number: 89

Enter the second number: 45

Enter the third number: 21

89 is the greatest.

Also, check our blog on How to Write C Program for Matrix Multiplication.

## Program to Check Whether a Number is Odd or Even

This program takes a number as input, uses the modulo operator (%) to check if the number is divisible by 2, and then prints whether it is even or odd.

`#include <stdio.h>int main() {    // Declare a variable to store the number    int num;    // Prompt the user to enter the number    printf("Enter a number: ");    scanf("%d", &num);    // Check if the number is divisible by 2 to determine if it's even or odd    if (num % 2 == 0)        printf("%d is an even number.\n", num);    else        printf("%d is an odd number.\n", num);    return 0;}`

Output:

Enter a number: 91

91 is an odd number.

Get ready for high-paying programming jobs with these Top C & Data Structure Interview Questions and Answers!

## Program to Swap Two Numbers

The following program takes two numbers as input from the user, uses a temporary variable to swap their values, and then prints the original and swapped values.

`#include <stdio.h>int main() {    // Declare variables to store two numbers    int num1, num2, temp;    // Prompt the user to enter the first number    printf("Enter the first number: ");    scanf("%d", &num1);    // Prompt the user to enter the second number    printf("Enter the second number: ");    scanf("%d", &num2);    // Display the original values    printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);    // Swap the values using a temporary variable    temp = num1;    num1 = num2;    num2 = temp;    // Display the swapped values    printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);    return 0;}`

Output:

Enter the first number: 45

Enter the second number: 25

Before swapping: num1 = 45, num2 = 25

After swapping: num1 = 25, num2 = 45

## Program to Find Quotient and Remainder

The below program takes two numbers (dividend and divisor) as input from the user, calculates the quotient and remainder using the division and modulo operators, and then prints the results.

`#include <stdio.h>int main() {    // Declare variables to store dividend, divisor, quotient, and remainder    int dividend, divisor, quotient, remainder;    // Prompt the user to enter the dividend    printf("Enter the dividend: ");    scanf("%d", &dividend);    // Prompt the user to enter the divisor    printf("Enter the divisor: ");    scanf("%d", &divisor);    // Calculate quotient and remainder    quotient = dividend / divisor;    remainder = dividend % divisor;    // Display the results    printf("Quotient: %d\n", quotient);    printf("Remainder: %d\n", remainder);    return 0;}`

Output:

Enter the dividend: 48

Enter the divisor: 12

Quotient: 4

Remainder: 0

## Program to Print Fibonacci Series

The below program takes user input for the number of terms in the Fibonacci series, initializes the first two terms as 0 and 1, and then uses a loop to generate and print the rest of the series.

`#include <stdio.h>int main() {    // Declare variables    int n, first = 0, second = 1, next;    // Prompt the user to enter the number of terms    printf("Enter the number of terms in the Fibonacci series: ");    scanf("%d", &n);    // Display the first two terms of the Fibonacci series    printf("Fibonacci Series: %d, %d, ", first, second);    // Generate and print the rest of the series    for (int i = 3; i <= n; i++) {        next = first + second;        printf("%d, ", next);        first = second;        second = next;    }    printf("\n");    return 0;}`

Output:

Enter the number of terms in the Fibonacci series: 6

Fibonacci Series: 0, 1, 1, 2, 3, 5,

## Program to Print the Factorial of a Number

The given program takes a number as input from the user, checks if it’s negative (as factorial is not defined for negative numbers), and then calculates and prints the factorial using a loop.

`#include <stdio.h>int main() {    // Declare variables    int num;    long long factorial = 1;    // Prompt the user to enter the number    printf("Enter a number: ");    scanf("%d", &num);    // Check if the number is negative    if (num < 0) {        printf("Factorial is not defined for negative numbers.\n");    } else {        // Calculate factorial        for (int i = 1; i <= num; ++i) {            factorial *= I;        }        // Display the result        printf("Factorial of %d = %lld\n", num, factorial);    }    return 0;}`

Output:

Enter a number: 4

Factorial of 4 = 24

## Program to Check Whether a Number is Prime or Not

The following program defines a function “isPrime” that checks whether a given number is prime. The “main” function takes a number as input from the user, calls the “isPrime” function, and then prints whether the number is prime or not.

`#include <stdio.h>#include <stdbool.h>// Function to check whether a number is primebool isPrime(int num) {    if (num <= 1) {        return false;    }    for (int i = 2; i * i <= num; ++i) {        if (num % i == 0) {            return false;        }    }    return true;}int main() {    // Declare a variable to store the number    int num;    // Prompt the user to enter the number    printf("Enter a number: ");    scanf("%d", &num);    // Check if the number is prime and display the result    if (isPrime(num)) {        printf("%d is a prime number.\n", num);    } else {        printf("%d is not a prime number.\n", num);    }    return 0;}`

Output:

Enter a number: 59

59 is a prime number.

## Program to Check Whether a Number is a Palindrome or Not

This program defines a function called “isPalindrome” that checks whether a given number is a palindrome. The “main” function takes a number as input from the user, calls the “isPalindrome” function, and then prints whether the number is a palindrome or not.

`#include <stdio.h>// Function to check whether a number is palindromeint isPalindrome(int num) {    int reversedNum = 0, originalNum = num;    while (num > 0) {        int digit = num % 10;        reversedNum = reversedNum * 10 + digit;        num /= 10;    }    if (originalNum == reversedNum) {        return 1; // Palindrome    } else {        return 0; // Not a palindrome    }}int main() {    // Declare a variable to store the number    int num;    // Prompt the user to enter the number    printf("Enter a number: ");    scanf("%d", &num);    // Check if the number is palindrome and display the result    if (isPalindrome(num)) {        printf("%d is a palindrome number.\n", num);    } else {        printf("%d is not a palindrome number.\n", num);    }    return 0;}`

Output:

Enter a number: 845

845 is not a palindrome number.

## Program to Check Whether a Number is Armstrong or Not

This program defines a function called “isArmstrong” that checks whether a given number is an Armstrong number. The “main” function takes a number as input from the user, calls the “isArmstrong” function, and then prints whether the number is an Armstrong number or not.

`#include <stdio.h>#include <math.h>// Function to check whether a number is Armstrongint isArmstrong(int num) {    int originalNum, remainder, n = 0, result = 0;    originalNum = num;    // Count the number of digits    while (originalNum != 0) {        originalNum /= 10;        ++n;    }    originalNum = num;    // Calculate the result    while (originalNum != 0) {        remainder = originalNum % 10;        result += pow(remainder, n);        originalNum /= 10;    }    if (result == num) {        return 1; // Armstrong number    } else {        return 0; // Not an Armstrong number    }}int main() {    // Declare a variable to store the number    int num;    // Prompt the user to enter the number    printf("Enter a number: ");    scanf("%d", &num);    // Check if the number is Armstrong and display the result    if (isArmstrong(num)) {        printf("%d is an Armstrong number.\n", num);    } else {        printf("%d is not an Armstrong number.\n", num);    }    return 0;}`

Output:

Enter a number: 424

424 is not an Armstrong number.

## Wrap Up

In conclusion, these basic C programming examples serve as a solid foundation for programmers. From fundamental concepts like variables and loops to more advanced topics like functions and arrays, each example provides hands-on experience to sharpen coding skills. As you explore the complexities of C, remember that mastery of these basics lays the groundwork for tackling more complex programming challenges. Embrace the learning journey, practice consistently, and watch as your proficiency in C programming steadily grows.

Do you still have doubts about C programming? Clear your doubts and queries with our experts in our C Programming Community!

Course Schedule

Name Date Details
Python Course 22 Jun 2024(Sat-Sun) Weekend Batch
View Details
Python Course 29 Jun 2024(Sat-Sun) Weekend Batch
View Details
Python Course 06 Jul 2024(Sat-Sun) Weekend Batch
View Details