Hello Readers, in this article you will learn different types of C programs important for students of classes 8, 9, 10, 11, 12. These programs include from basic to advance (structure to an array). You may also want to check out the following articles before continuing the programs.
- Introduction to Programming in C and Structured Programming || Basic Overview
- Structure of C language
- Easy Steps for Compiling a C Program
- C Programming Logic and 5 examples of logical programs
- Selection Or Decision Structure (Control Statements) in C
- Looping Or Iteration Structure (Looping Structure and Nested Loop) in C
- Array in C language (Learn in just 10 minutes)
- 7 String Functions in C (Examples Included) In Easy Language
- 2 Major Functions used in C programming (Everyone Should Know)
- Return and Void Statements of Function in C Programming.
- File Handling in C Programming (Everything You Need To Know Till Class 12)
Contents
Structure of C Program
// This is the basic structure of C programming #include <stdio.h> int main(){ // Block of Statement return 0;}
Add two numbers
#include <stdio.h> int main(){ int a=7, b=5; printf("The sum is %d",a+b); return 0; }
Sum of two numbers
#include <stdio.h> int main(){ int a,b; printf("Enter two numbersn"); scanf("%d%d", &a, &b); printf("Sum = %d", a+b); return 0; }
Average of three numbers
#include <stdio.h> int main(){ float a,b,c; printf("Enter three numbersn"); scanf("%f%f%f", &a,&b,&c); printf("The average of three numbers is %0.2f", (a+b+c)/3); return 0; }
Volume of Cylinder
#include <stdio.h> int main(){ float r,h; printf("Enter radius and heightn"); scanf("%f%f", &r,&h); printf("Volume of cylinder is %0.2f", 3.14*r*r*h); return 0; }
Area and Circumference of Circle
#include <stdio.h> int main(){ float r, pi=3.14; printf("Enter radiusn"); scanf("%f", &r); printf("Circumference of Circle is %0.2fn", (2*pi*r)); printf("Area of circle is %0.2fn", (pi*r*r)); return 0; }
Convert Days to respective years, months, and days
#include <stdio.h> int main(){ int days, year, month, rem; printf("Enter daysn"); scanf("%d", &days); year = days/365; rem = days%365; month = rem /30; days = rem%30; printf("%d Years %d Months %d Daysn", year, month, days); return 0; }
Greatest among two numbers
#include <stdio.h> int main(){ int a,b; printf("Enter two numbersn"); scanf("%d%d", &a,&b); if(a>b){ printf("%d is greatern", a); } else{ printf("%d is greatern", b); } return 0; }
Odd or Even
#include<stdio.h> int main(){ int a; printf("Enter a numbern"); scanf("%d", &a); if (a%2 == 0){ printf("The given number is evenn"); } else{ printf("The given number is oddn"); } return 0; }
Positive, Negative, or Zero
#includeGreatest among three numbers
#include <stdio.h> int main(){ int a,b,c; printf("Enter three numbersn"); scanf("%d%d%d", &a,&b,&c); if(a>b && a>c){ printf("%d is greatern", a); } else if(b>a && b>c){ printf("%d is greatern", b); } else{ printf("%d is greatern", c); } return 0; }
Classify marks into division
#include <stdio.h> int main(){ int marks; printf("Enter marksn"); scanf("%d", &marks); if(marks>=80 && marks<=100){ printf("Distinctionn"); } else if(marks>=60 && marks<80){ printf("First Divisionn"); } else if(marks>=50 && marks<60){ printf("Second Divisionn"); } else if(marks>=40 && marks<50){ printf("Third Divisionn"); } else{ printf("Failn"); } return 0; }
Menu-based program to find the area of circles, rectangles, and triangle
#include <stdio.h> int main(){ int ch; printf("Press 1 for area of reactanglenPress 2 for area of circlenPress 3 for area of trianglen"); scanf("%d", &ch); int l,br; float pi=3.14, r; float b,h; switch(ch){ case 1: printf("Enter length and breadthn"); scanf("%d%d", &l, &br); printf("Area is %d", (l*br)); break; case 2: printf("Enter radiusn"); scanf("%f", &r); printf("Area is %0.2f", (pi*r*r)); break; case 3: printf("Enter base and heightn"); scanf("%f%f", &b,&h); printf("Area is %0.2fn", (0.5*b*h)); break; default: printf("Wrong Choicen"); break; } return 0; }
Fibonacci Series
#include <stdio.h> int main(){ int a=0, b=1,c; printf("%dn", a); printf("%dn", b); for(int i=1; i<9; i++){ c=a+b; printf("%dn", c); a=b; b=c; } return 0; }
Factorial of a given number
#include <stdio.h> int main(){ int a, p=1; printf("Enter a number\n"); scanf("%d", &a); for(int i=1; i<=a; i++){ p=p*i; } printf("Factorial is %d", p); return 0; }
Number palindrome
#include<stdio.h> int main(){ int a, b, rem, s=0; printf("Enter a number\n"); scanf("%d", &a); b=a; while(a != 0){ rem = a%10; s = s*10+rem; a = a/10; } if(b == s){ printf("Pallindrome"); } else{ printf("Not pallindrome"); } return 0; }
Array Example
#include <stdio.h> int main(){ int a[10]; printf("Enter 10 numbersn"); for(int i=0; i<10; i++){ scanf("%d", &a[i]); } printf("You Enteredn"); for(int i=0; i<10; i++){ printf("%dt", a[i]); } return 0; }
Greatest among ten numbers
#include <stdio.h> int main(){ int a[10]; printf("Enter 10 numbersn"); for(int i=0; i<10; i++){ scanf("%d", &a[i]); } int greatest =a[0]; for(int i=0; i<10; i++){ if(a[i] > greatest){ greatest = a[i]; } } printf("Greatest is %d", greatest); return 0; }
Input and Display 3*3 matrix
#include <stdio.h> int main(){ int a[3][3]; printf("Enter elements of matrixn"); for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ scanf("%d", &a[i][j]); } } for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ printf("%dt", a[i][j]); } printf("n"); } return 0; }
strcat example
#include <stdio.h> #include <string.h> int main(){ char a[] = "Hello", b[] = "World"; printf("%s", strcat(a,b)); return 0; }
strcmp example
#include <stdio.h> #include <string.h> int main(){ char a[] = "hi", b[] = "bro"; if(strcmp(a,b) == 0){ printf("Equal"); } else{ printf("Not Equal"); } return 0; }
strcpy example
#include <stdio.h> #include <string.h> int main(){ char a[] = "hello", b[10]; strcpy(b,a); printf("%s", b); return 0; }
strlen example
#include <stdio.h> #include <string.h> int main(){ char a[] = "Hello"; printf("%d", strlen(a)); return 0; }
strlwr example
#include <stdio.h> #include <string.h> int main(){ char a[] = "HELLO"; printf("%s", strlwr(a)); return 0; }
strrev example
#include <stdio.h> #include <string.h> int main(){ char a[] = "hello"; printf("%s", strrev(a)); return 0; }
strupr example
#include <stdio.h> #include <string.h> int main(){ char a[] = "hello"; printf("%s", strupr(a)); return 0; }