The Basic Programs on C with examples and Outputs

basic programs on C

C language is one of the most basic as well as an important language to start your journey towards programming. Here we present to you, most basic programs on C to get you started with examples and their outputs.

Basic Programs on c

  1. Hello World program in C
  2. How to take input in C
  3. How to increment or decrement a variable in C
  4. Find the sum of two numbers
  5. If – Else in C
  6. Switch in C
  7. For loop in C
  8. While loop in C
  9. Do-While loop in C
  10. Write a program to calculate the Percentage
  11. Find the greatest among two numbers
  12. Write a program to find the greatest among three numbers
  13. Find whether the number is even or odd
  14. Write a program that takes a number and gives the corresponding month.
  15. Write a program to print all the alphabets in small or capital letters
  16. Find whether the given character is vowel or not
  17. Write a program to find a Leap Year
  18. Write a program to print the table of a given number
  19. Find factorial of a given number
  20. Swap two numbers
  21. Find the area of a triangle
  22. All Sorting Algorithms in C
  23. Pattern Programs in C
Hello World Program in c
#include<stdio.h>
void main()
{
     printf("Hello World");
}

OUTPUT

Hello World

How to Take Input in C
#include<stdio.h>
void main()
{
      float a=67,b=9,c=a/b;
      printf("division of two number is %f",c);
      } 

OUTPUT

division of two number is 7.444445

How to increment or decrement a variable in C
#include<stdio.h>
void main()
{
int a=8;
a++;
int b=7;
b--;
int c=a+b;

printf("Value of c = %d",c);
}

OUTPUT

Value of c = 15

Find the sum of two numbers
#include<stdio.h>
void main()
{
      int a,b,sum=0;
      printf("enter two number");
      scanf("%d%d",&a,&b);
      sum=a+b;
      printf("Sum = "%d",sum);
      }

OUTPUT

Enter two number 5 6
Sum = 11

If – Else in C
#include<stdio.h>
void main()
{
      int x;
      printf("Enter x : ");
      scanf("%d",&x);
      if(x==1)
      printf("x is 1");
      else
      printf("x is not 1");
}

OUTPUT

Enter x : 1
x is 1

Switch in C
#include<stdio.h>
void main()
{
      int a;
      printf("Enter 500 or 600: ");
      scanf("%d",&a);
      switch(a){
              case 500:printf("You have entered 500");
	      break;
	      case 600:printf("You have entered 600");
	      break;
              default:printf("You have entered wrong no.");
             }
}

OUTPUT

Enter 500 or 600: 500
You have entered 500

For loop in C
#include<stdio.h>
 void main()
 {
       int a;
       printf("value of a is - \n");
       for(a=1;a<11;a++){
           printf("%d ",a);
	}
 }
                         

OUTPUT

value of a is - 
1 2 3 4 5 6 7 8 9 10

While Loop in C
#include<stdio.h>
void main()
{
      int a;
      a=11;
      printf("Value of a is : ");
      while(a<=20){
	    printf("%d ",a);
            a++;
       }
}

OUTPUT

Value of a is : 11 12 13 14 15 16 17 18 19 20

Do-While loop in C
#include<stdio.h>
void main()
{
      int a;
      a=21;
      printf("Value of a is: ");
      do{
	printf("%d ",a);
        a++;
	}
      while(a<=30);
}

OUTPUT

Value of a is: 21 22 23 24 25 26 27 28 29 30

Write a program to calculate the percentage
#include<stdio.h>
void main()
{
      float maths,eng,hindi,percentage;
      printf("Marks of Maths :\n");
      scanf("%f",&maths);
      printf("Marks of English :\n");
      scanf("%f",&eng);
      printf("Marks of Hindi :\n");
      scanf("%f",&hindi);
      percentage=((maths+eng+hindi)*100)/300;
      printf("percent = %f",percentage);
}

OUTPUT

Marks of Maths :
83
Marks of English :
94
Marks of Hindi :
72
percent = 83.000000

write a program to find the greatest among two numbers
#include<stdio.h>
void main(){
    int a,b ;
    printf("Enter 2 numbers a,b : ");
    scanf("%d%d",&a,&b);
    if(a > b)
    printf("a is greater than b");
    else if(b > a)
    printf("b is greater than a");
    else
    printf("Both are equal");
}

OUTPUT

Enter 2 numbers a,b : 5 7
a is greater than b

Write A Program To Find The Greatest Among Three Numbers
#include<stdio.h>
main()
{
      float a,b,c;
      printf("Enter 3 no. a,b,c : ");
      scanf("%f%f%f",&a,&b,&c);
      if(a==b==c)
      printf("a,b,c are equal");
      else if (a>b && a>c)
      printf("a is the greatest"); 
      else if (b>c)
      printf("b is the greatest");
      else
      printf("c is the greatest");
}

OUTPUT

Enter 3 no. a,b,c : 1 2 3
c is the greatest

Write a program to find whether the number is even or odd
#include<stdio.h>
main()
{
int a;
printf("Enter a number: ");
scanf("%d",&a);
if(a%2==1)
    printf("Number is Odd");
else 
    printf("Number is Even");
}

OUTPUT

Enter a number: 4
Number is Even

write a program that takes a number and gives the corresponding month.
#include<stdio.h>
void main()
{
	int a;
	printf("Enter 1 to 12 and find months name: ");
	scanf("%d",&a);
	switch(a){
              case 1:printf("January");
	      break;
              case 2:printf("February");
              break;
              case 3:printf("March");
              break;
              case 4:printf("April");
              break;
              case 5:printf("May");
              break;
              case 6:printf("June");
              break;
              case 7:printf("July");
              break;
              case 8:printf("August");
              break;
              case 9:printf("September");
              break;
              case 10:printf("October");
              break;
              case 11:printf("November");
              break;
              case 12:printf("December");
              break; 
              default:printf("Wrong number");
		}
}

OUTPUT

Enter 1 to 12 and find months name: 9
September

Write a program to print all the alphabets in small Or capital letters
#include<stdio.h>
void main()
{
  char a;
  printf("Enter 's' for Small Letters or 'c' for Capital Letters: ");
  scanf("%c",&a);
  if(a=='s'|| a=='S'){
	  for(a='a';a<='z';a++)
      	printf("%c ",a);
	}
  else if(a=='c'||a=='C'){
     for(a='A';a<='Z';a++)
        	printf("%c ",a);
	}
  else
     printf("wrong inpurt");
}

OUTPUT

Enter 's' for Small Letters or 'c'for Capital Letters: s
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

Write a program to find whether the given character is vowel or not
#include<stdio.h>
void main()
{
      char a;
      printf("Enter any alphabet: ");
      scanf("%c",&a);
      if(a=='a'||a=='A'||a=='e'||a=='E'||a=='i'||a=='I'||a=='o'||a=='O'||a=='u'||a=='U')
      printf("Vowel");
      else
      printf("Not Vowel");
}

OUTPUT

Enter any alphabet: a
Vowel

Write a program to find a leap year
#include<stdio.h>
void main()
{
      int a;
      printf("Enter any year:");
      scanf("%d",&a);
      if(a%4==0)
      printf("Leap year");
      else
      printf("Non Leap year");
}

OUTPUT

Enter any year: 2016
Leap year

Write a program to print the table of the given number
#include<stdio.h>
void main()
{
      int n,i;
      printf("Enter any number: ");
      scanf("%d",&n);
      for(i=1;i<=10;i++){
      printf("%d*%d=%d \n",n,i,n*i);}
}

OUTPUT

Enter any number: 4
4*1=4 
4*2=8 
4*3=12 
4*4=16 
4*5=20 
4*6=24 
4*7=28 
4*8=32 
4*9=36 
4*10=40

Write a program to find the factorial of the given number
#include<stdio.h>
void main()
{
      int n,i,fact=1;
      printf("Enter any no.");
      scanf("%d",&n);
      for(i=1;i<=n;i++){
	  fact=fact*i;
       }
      printf("Factorial of %d = %d",n,fact);
}

OUTPUT

Enter any no. 4
Factorial of 4 = 24

Write a program to swap two numbers
#include<stdio.h>
void main()
{
      int x,y,temp;
      printf("Enter two numbers x,y: ");
      scanf("%d%d",&x,&y);
      printf("Before Swapping\nx=%d\ny=%d\n",x,y);
      temp=x;
      x=y;
      y=temp;
      printf("After Swapping\nx=%d\ny=%d\n",x,y);
}

OUTPUT

Enter two numbers x,y: 4 5
Before Swapping
x=4
y=5
After Swapping
x=5
y=4

Write a program to find the area of the triangle
#include<stdio.h>
void main()
{
      int a,b,c,j,area;
      printf("Enter Sides:");
      scanf("%d%d%d",&a,&b,&c);
      j=(a+b+c)/2;
      area=j*(j+a)*(j+b)*(j+c);
      printf("Area = %d",area);
}

OUTPUT

Enter Sides: 4 6 8
Area = 29835

You may also like:-

Best Programming Languages in Learn

Leave a Comment

Your email address will not be published. Required fields are marked *