C Programming Examples Every Beginner Must Know

C Programming is one of the widely used programming languages of all time. It was developed by Dennis Ritchie between 1969 and 1973 at Bell Labs, and used to re-implement the Unix operating system.

C programming language is almost used in every field like developing operating systems, web development, software development. Speed, stability, and near-universal availability are some reasons for choosing C over interpreted languages. C has directly or indirectly influenced many later languages such as C#, D, Go, Java, JavaScript, Limbo, LPC, Perl, PHP, Python, and Unix’s C shell.

In this article, we are going to share some C programming examples that every C beginner must know. These are basic C programs, that is going to help newbies who just stepped in C programming world. So try them out :

1. C Programming – Hello World

#include<stdio.h>
int main()
{
printf("Hello world");
return 0;
}

Output

Hello world

Check out how to write “Hello, World !” program in different programming languages.

2. C Programming – Performing Arithmetic Operations

#include<stdio.h>
int main()
{
int a,b;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
printf("Sum=%d difference=%d product=%d quotient=%d",a+b,a-b,a*b,a/b);
return 0;
}

Output

Enter two numbers: 6 3
Sum=9 difference=3 product=18 quotient=2

3. C Programming – Find Area Of Circle

#include<stdio.h>
int main() 
{
float radius, area;
printf("\nEnter the radius of Circle : ");
scanf("%d", &radius);
area = 3.14 * radius * radius;
printf("\nArea of Circle : %f", area);
return 0;
}

Output

Enter the radius of Circle : 2.0
Area of Circle : 6.14

4. C Programming – Find Greatest In 3 Numbers

#include<stdio.h>
int main() 
{
int a, b, c;
printf("\nEnter value of a, b & c : ");
scanf("%d %d %d", &a, &b, &c);
if ((a > b) && (a > c))
printf("\na is greatest");
if ((b > c) && (b > a))
printf("\nb is greatest");
if ((c > a) && (c > b))
printf("\nc is greatest");
return 0;
}

Output

Enter value for a,b & c : 15 17 21
c is greatest

Also Read : Top 10 Cloud Programming Languages

5. C Programming – Find Even Or Odd

#include<stdio.h>
int main()
{
int n;
printf("Enter a number:");
scanf("%d",&n);
if(n%2==0)
{
printf("Number is even");
}
else
{
printf("Number is odd");
}
return 0;
}

Output

Enter a number: 4
Number is even

6. C Programming – Display Factors Of A Number

#include <stdio.h>
int main()
{
int n,i;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factors of %d are: ", n);
for(i=1;i<=n;++i)
{
if(n%i==0)
printf("%d ",i);
}
  return 0;
}

Output

Enter a positive integer: 60
Factors of 60 are: 1 2 3 4 5 6 12 15 20 30 60

7. C Programming – Check Prime Number

#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2; i<=n/2; ++i)
{
// condition for nonprime number
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}

Output

Enter a positive integer: 29
29 is a prime number.

Also Read : What Is Programming And What Do Programmers Do?

8. C Programming – Check Leap Year

#include<stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
return 0;
}

Output

Enter a year: 1900
1900 is not a leap year.

9. C Programming – Adding ‘n’ Numbers

#include<stdio.h>
int main()
{
int i,n,sum=0;
printf("Upto how many terms you want to find the sum:");
scanf("%d",&n);
for(i=1;i<=n;i++){
sum = sum + i;
}
printf("Sum is %d",sum);
return 0;
}

Output

Upto how many terms you want to find the sum: 10
Sum is 55

10. C Programming – Factorial Of A Number

#include <stdio.h>
int main()
{
int n, i;
unsigned long long factorial = 1;
printf("Enter an integer: ");
scanf("%d",&n);
// show 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)
{
factorial *= i;              // factorial = factorial*i;
}
printf("Factorial of %d = %llu", n, factorial);
}
return 0;
}

Output

Enter an integer: 10
Factorial of 10 = 3628800

11. C Programming – Generate Multiplication Table

#include <stdio.h>
int main()
{
int n, i;
printf("Enter an integer: ");
scanf("%d",&n);
for(i=1; i<=10; ++i)
{
printf("%d * %d = %d \n", n, i, n*i);
}
return 0;
}

Output

Enter an integer: 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90

12. C Programming – Fibonacci Series

#include <stdio.h>
int main()
{
    int i, n, t1 = 0, t2 = 1, nextTerm = 0;

    printf("Enter the number of terms: ");
    scanf("%d",&n);

    // displays the first two terms which is always 0 and 1
    printf("Fibonacci Series: %d, %d, ", t1, t2);

    // i = 3 because the first two terms are already dislpayed
    for (i=3; i <= n; ++i)
    {
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
        printf("%d, ",nextTerm);
    }
    return 0;
}

Output

Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

Also Read : Top 10 Programming Language That Will Help You to Get Dream Job

13. C Programming – Number Is Positive Or Negative

#include <stdio.h>
int main()
{
    double number;

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

    if (number <= 0.0)
    {
        if (number == 0.0)
            printf("You entered 0.");
        else
            printf("You entered a negative number.");
    }
    else
        printf("You entered a positive number.");
    return 0;
}

Output

Enter a number: 12.3
You entered a positive number.

14. C Programming – Reverse String Without Using Library Function

#include<stdio.h>
#include<string.h>
int main()
{
char str[100],rev[100];
int i,len=0;
printf("Enter a string");
gets(str);
for(i=0;i<=100;i++)
{
if(str[i]=='\0')
{
break;
}
len++;
}
for(i=0;i<=len-1;i++)
{
rev[i] = str[len-i-1];
}
printf("reverse of the string is %s",rev);
return 0;
}

Output

Enter a string Johnson
reverse of the string is nosnhoJ

15. C Programming – Display English Alphabets

#include <stdio.h>
int main()
{
    char c;

    for(c='A'; c<='Z'; ++c)
       printf("%c ",c);
    
    return 0;
}

Output

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

16. C Programming – Palindrome

#include <stdio.h>
int main()
{
    int n, reversedInteger = 0, remainder, originalInteger;

    printf("Enter an integer: ");
    scanf("%d", &n);

    originalInteger = n;

    // reversed integer is stored in variable 
    while( n!=0 )
    {
        remainder = n%10;
        reversedInteger = reversedInteger*10 + remainder;
        n /= 10;
    }

    // palindrome if orignalInteger and reversedInteger is equal
    if(originalInteger == reversedInteger)
        printf("%d is a palindrome.", originalInteger);
    else
        printf("%d is not a palindrome.", originalInteger);
    
    return 0;
}

Output

Enter an integer: 1001
1001 is a palindrome.

17. C Programming – Armstrong Number

#include <stdio.h>
int main()
{
    int number, originalNumber, remainder, result = 0;

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

    originalNumber = number;

    while (originalNumber != 0)
    {
        remainder = originalNumber%10;
        result += remainder*remainder*remainder;
        originalNumber /= 10;
    }

    if(result == number)
        printf("%d is an Armstrong number.",number);
    else
        printf("%d is not an Armstrong number.",number);

    return 0;
}

Output

Enter a three digit integer: 371
371 is an Armstrong number.

18. C Programming – Create Pyramid

#include<stdio.h>
 
int main() {
   int i, j;
   int num;
 
   printf("Enter the number of Digits :");
   scanf("%d", &num);
 
   for (i = 0; i <= num; i++) {
      for (j = 0; j < i; j++) {
         printf("%d ", i);
      }
      printf("\n");
   }
   return 0;
}

Output

Enter the number of Digits : 5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

19. C Programming – Reverse A Number

#include <stdio.h>
int main()
{
    int n, reversedNumber = 0, remainder;

    printf("Enter an integer: ");
    scanf("%d", &n);

    while(n != 0)
    {
        remainder = n%10;
        reversedNumber = reversedNumber*10 + remainder;
        n /= 10;
    }

    printf("Reversed Number = %d",reversedNumber);

    return 0;
}

Output

Enter an integer: 2345
Reversed Number = 5432

20. C Programming – Swap Two Numbers

#include <stdio.h>
int main()
{
      double firstNumber, secondNumber, temporaryVariable;

      printf("Enter first number: ");
      scanf("%lf", &firstNumber);

      printf("Enter second number: ");
      scanf("%lf",&secondNumber);

      // Value of firstNumber is assigned to temporaryVariable
      temporaryVariable = firstNumber;

      // Value of secondNumber is assigned to firstNumber
      firstNumber = secondNumber;

      // Value of temporaryVariable (which contains the initial value of firstNumber) is assigned to secondNumber
      secondNumber = temporaryVariable;

      printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber);
      printf("After swapping, secondNumber = %.2lf", secondNumber);

      return 0;
}

Output

Enter first number: 1.20
Enter second number: 2.45

After swapping, firstNumber = 2.45
After swapping, secondNumber = 1.20

These are some of the C programming examples that will help beginners in their coding journey. If you need more C programming examples please comment below and also share your doubts.

Also Read : Important Programming Languages for Hackers

Sabarinathhttps://techlog360.com
Sabarinath is the tech-savvy founder and Editor-in-Chief of TechLog360. With years of experience in the tech industry and a computer science background, he's an authority on the latest tech news, business insights, and app reviews. Trusted for his expertise and hands-on tips for Android and iOS users, Sabarinath leads TechLog360 with a commitment to accuracy and helpfulness. When not immersed in the digital world, he's exploring new gadgets or sharing knowledge with fellow tech enthusiasts.

3 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Exit mobile version