C Notes

What is C?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972.

It is a very popular language, despite being old. The main reason for its popularity is because it is a fundamental language in the field of computer science.

C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


Why Learn C?

  • It is one of the most popular programming languages in the world.
  • If you know C, you will have no problem learning other popular programming languages such as Java, Python, C++, C#, etc, as the syntax is similar.
  • C is very fast, compared to other programming languages, like Java and Python.
  • C is very versatile; it can be used in both applications and technologies

Difference between C and C++

  • C++ was developed as an extension of C, and both languages have almost the same syntax.
  • The main difference between C and C++ is that C++ support classes and objects, while C does not.

Program 1 : C Program to calculate the sum of five numbers and their average.

#include<stdio.h>
#include<conio.h>
void main()
{
 int n1,n2,n3,n4,n5,c,avg;
 clrscr();

 printf("Enter five numbers :");
 scanf("%d %d %d %d %d",&n1,&n2,&n3,&n4,&n5);
 c=n1+n2+n3+n4+n5;
 avg=c/5;
 printf("\nThe sum is %d",c);
 printf("\nThe average is %d",avg);
 getch();
}

Program 2 : C Program to calculate the area of the rectangle.

// area of rectangle
#include<stdio.h>
#include<conio.h>
int main()
{
 float l,b,area;
 clrscr();
 printf("Enter the length and breadth of rectangle :");
 scanf("%f %f",&l,&b);
 area=l*b;
 printf("Area of Rectangle is %.2f",area);
 getch();
 return 0;
}

Program 3 : C Program to print numbers from 1 to 20 using for loop.

#include<stdio.h>
#include<conio.h>
void main()
{
 int i;
 clrscr();

 for(i=1;i<=20;i++)
  printf(" %d",i);
getch();
}

Program 4 : Program to develop a calculator using switch case.

// program to develop a calculator
#include<stdio.h>
#include<conio.h>
void main()
{
  int n1,n2;
  char op;
  clrscr();
  printf("Enter first Number:");
  scanf("%d",&n1);
  printf("Enter operator : +, -,* / ");
  scanf(" %c",&op);
  printf("Enter second number ");
  scanf("%d",&n2);
  switch(op)
  {
    case '+':
      printf("The sum is %d",n1+n2);
      break;
    case '-':
      printf("The subration is %d",n1-n2);
      break;
    case '*':
      printf("The multiplication is %d",n1*n2);
      break;
    case '/':
      printf("The division is %d",n1/n2);
      break;
    default:
     printf("Invaid operator,please enter +,-,*,/");
  }
getch();
}

Program 5 : Even or Odd

#include<stdio.h>
#include<conio.h>
void main()
{
  int num;
  clrscr();
  printf("Enter a number:");
  scanf("%d",&num);
  if(num%2==0)
    printf("Even number");
  else
    printf("Odd number");
getch();
}

Program 6 : C Program to find the grade of a student using if..else if… else

#include<stdio.h>
#include<conio.h>
void main()
{
  float per;
  clrscr();
  printf("Enter the percentage of student:");
  scanf("%f",&per);
  if(per>=75)
    printf("Excellent");
  else if(per>=60)
    printf("Very good");
  else if(per>=33)
    printf("Pass");
  else
    printf("Fail");
  getch();
}

Program 7 : To display the table of given number.

#include<stdio.h>
#include<conio.h>
void main()
{
  int i,num;
  clrscr();
  printf("Enter a number:");
  scanf("%d",&num);
  for(i=1;i<=20;i++)
    printf("\n %d X %d = %d",num,i,num*i);
getch();
}

Program 8 : To display the sum of numbers up to nth term using while loop.

//while loop
#include<stdio.h>
#include<conio.h>
void main()
{
  int i=1,no,sum=0;
  clrscr();
  printf("Enter number :");
  scanf("%d",&no);
  while(i<=no)
  {
    printf("\n %d",i);
    sum=sum+i;
    i++;
  }
printf("\nThe sum is %d",sum);
getch();
}

Program 9 : C program to Check Whether a Number is Positive or Negative or Zero.

#include <stdio.h>
#include <conio.h>  
  
void main()  
{  
    int A;
    clrscr();  
  
    printf("Enter the number A: ");  
    scanf("%d", &A);  
  
    if (A > 0)  
        printf("%d is positive.", A);  
    else if (A < 0)  
        printf("%d is negative.", A);  
    else if (A == 0)  
        printf("%d is zero.", A);  
  
  getch();  
}  

Program 10 : C Program to Print Half Pyramid Pattern of Numbers. (Nested for loop)

#include <stdio.h>
#include<conio.h>
void main()
{
    int rows;
    printf("Number of rows: ");
    scanf("%d", &rows);
 
    
    for (int i = 1; i <= rows; i++) {
 
        for (int j = 1; j <= i; j++) {
            printf("%d ", i);
        }
        printf("\n");
    }
    getch();
}

Program 11 : C Program to Print Inverted Half Pyramid Pattern of Numbers.

// C program to print inverted half pyramid pattern of number
#include <stdio.h>
#include<conio.h>
void main()
{
    int rows;
      printf("Number of rows: ");
      scanf("%d", &rows);
   
    
    for (int i = rows; i >= 1; i--) {
   
        for (int j = 1; j <= i; j++) 
        {
            printf("%d ", i);
        }
        printf("\n");
    }
    getch();
}

Program 12 : C Program to Print Full Pyramid Pattern of Numbers.

#include <stdio.h>
#include<conio.h> 
void main()
{
    int rows;
    printf("Number of rows: ");
    scanf("%d", &rows);
 
    // first loop to print all rows
    for (int i = 1; i <= rows; i++) {
 
        // inner loop 1 to print white spaces
        for (int j = 1; j <= 2 * (rows - i); j++) {
            printf(" ");
        }
 
        // inner loop 2 to print numbers
        for (int k = 1; k < 2 * i; k++) {
            printf("%d ", i);
        }
        printf("\n");
    }
    getch();
}

Leave a Reply