Basic Programs in C Language

 

1.       Write a program to find the area of triangle.   Area = sqrt(s*(s-a)*(s-b)*(s-c))

#include<stdio.h>

#include<conio.h>

#include<math.h>

 

void main()

  {

   int a,b,c;

   float s,d,area;

   clrscr();

   printf("Enter the sides of tringle:");

   scanf("%d%d%d",&a,&b,&c);

   s=(a+b+c)/2.0;

   d=(s*(s-a)*(s-b)*(s-c));

   area=sqrt(d);

 

   printf("Area of:%f",area);

 

   getch();

  }

 

2.       Write a program to find the factorial of any number.

#include<stdio.h>

#include<conio.h>

void main()

{

                long int f=1;

                int n,i;

                clrscr();

                printf("Enter the number");

                scanf("%d",&n);

                for(i=1;i<=n;i++)

                {

                                f=f*i;

                }

                printf("The factorial is %ld",f);

                getch();

}

 

3.       Write a program to find the sum of following series:

a.       1 – 1/2 + 1/3 – 1/4 + 1/5 - …… up to n terms.

#include<stdio.h>

#include<conio.h>

void main()

{

int i,sign = +1,n; double s=0.0;

clrscr();

 

                printf("enter number of terms ");

                scanf("%d",&n);

 

                for(i=1;i<=n;i++)

                {

                      s=s+sign * 1.0/i;

                      sign= -1*sign;

                }

                printf("sum of series = \t%lf",s);

getch();

}

 

b.       1! + 2! + 3! + 4! + ….. + n!

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,n; long int fact,s=0;

clrscr();

 

                printf("enter number of terms ");

                scanf("%d",&n);

 

                for(i=1;i<=n;i++)

                {

                fact=1;

                for(j=1;j<=i;j++)

                fact=fact*j;

 

                s=s+fact;

                }

                printf("sum of series = \t%ld",s);

getch();

}

 

c.        S = 2+4+6+8+……………N terms.

#include<stdio.h>

#include<conio.h>

void main()

{

int i,k,n,s=0;

clrscr();

 

                printf("enter number of terms ");

                scanf("%d",&n);

                k=2;

                for(i=1;i<=n;i++)

                {

                s=s+k;

                k=k+2;

                }

                printf("sum of series = \t%ld",s);

getch();

}

d.       S = -13 + 33 - 53 + 73 – 93 + 113 - ……..N terms.

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

int i,k,n,s=0,sign=-1;

clrscr();

 

                printf("enter number of terms ");

                scanf("%d",&n);

                k=1;

                for(i=1;i<=n;i++)

                {

                s=s+sign*pow(k,3);

                k=k+2;

                sign=-1*sign;

                }

                printf("sum of series = \t%d",s);

getch();

}

 

e.        S = 1/1! + 2/2! + 3/3! + ………….. 7 terms.

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

int i,k,n,fact;float s=0;

clrscr();

                printf("enter number of terms "); //remove this line for 7 terms

                scanf("%d",&n); //remove this line for 7 terms

                for(i=1;i<=n;i++)   //replace n by 7 to get sum upto 7 terms

                {

                fact=1;

                for(k=1;k<=i;k++)

                fact=fact*k;

                s=s+(float)i/fact;

                //printf("sum of series = \t%f",s); for step by step check

//getch();

                }

                printf("sum of series = \t%f",s);

getch();

}

f.        S = 14 + 34 + 54 + 74 + …………….. 100 terms

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

int i,k,n;long int s=0;

clrscr();

 

                printf("enter number of terms "); //remove this line for 100 terms

                scanf("%d",&n); //remove this line for 100 terms

                k=1;

                for(i=1;i<=n;i++)   //replace n by 100 to get sum upto 100 terms

                {

                s=s+pow(k,4);

                k=k+2;

                }

                printf("sum of series = \t%ld",s);

getch();

}

4.       Write a program to find number is even or odd.

#include<stdio.h>

#include<conio.h>

 

void main()

  {

   int a;

   clrscr();

   printf("Enter The Number to be checked");

   scanf("%d",&a);

   if(a%2==0)

   {

                printf("Number %d is Even",a);

   }

   else

   {

                printf("Number %d is odd",a);

   }

   getch();

}

5.       Write a program for finding year is leap or not.

#include<stdio.h>

#include<conio.h>

void main( )
{
    int year;
   
   
printf("Enter the year: ");
  scanf("%d",&year);

    if(year%400 ==0 || (year%100 != 0 && year%4 == 0))
    {
       
printf("Year %d is a leap year",year);
    }
    else
    {
       
printf("Year %d is not a leap year",year);
    }
    getch();
}

 

6.       Admission to a professional course is subject to the following condition math>=60, phy>=50, chem>= 150. Write a program for finding that candidates are eligible or not

#include<stdio.h>

#include<conio.h>

 

void main()

 {

  int math,phy,chem;

  clrscr();

  printf("Enter the marks math:");

  scanf("%c",&math);

  printf("Enter the marks phy:");

  scanf("%c",&phy);

  printf("Enter the marks chem:");

  scanf("%c",&chem);

   

    if(math >= 60 && phy >= 50 && chem >= 40)

       {

        printf("Candidte is eligble");

       }

     else

        {

        printf("Candidte is not eligble");

        }

  getch();

 }

 

7.       Write a program to find gross salary of employee if DA is 40% of basic  Salary and HRA is 20% of basic salary. Basic salary input by keyboard.

#include<stdio.h>

#include<conio.h>

 

void main()

 {

  int bs;

  float da,hra,gross;

  clrscr();

  printf("Enter the basic salary:");

  scanf("%d",&bs);

 

  da = (0.4*bs);

  hra = (0.2*bs);

  gross = bs+da+hra;

  printf("\nDearness allowance: %f",da);

  printf("\nHouse rent allowance: %f",hra);

  printf("\nGross salary: %f",gross);

  getch();

 }

 

8.       Write a program to print given format

*

* *

* * *

* * * *

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j;

clrscr();

                for(i=1;i<=5;i++)

                {

                                for(j=1;j<=i;j++)

                                printf("* ");

 

                                printf("\n");

                }

 

getch();

}

 

 

9.       Write a program to print given format

 

      *

    * * *

  * * * * *

* * * * * * *

#include<stdio.h>

#include<conio.h>

void main()

{

 int i ,j,k;

 clrscr();

 for(i=1;i<=5;i++)

 {

    for(k=0;k<5-i;k++)

    printf(" ");

 

    for(j=1;j<=2*i-1;j++)

    printf("*");

 

    printf("\n");

 }

 getch();

}

 

10.    Write a program for performing all arithmetic operation using switch case.

#include<stdio.h>

#include<conio.h>

void main()

  {

   int a,b,n;

   float c;

   clrscr();

   printf("Enter the number:");

   scanf("%d%d",&a,&b);

   printf("1.Addition\n");

   printf("2.Subtraction\n");

   printf("3.Division\n");

   printf("4.Multiply\n");

   printf("5.Modulo Division\n");

   printf("Enter ur choce:");

   scanf("%d",&n);

 

     switch(n)

       {

                case 1: c=a+b;

                       printf("Add:%f",c);

                       break;

                case 2: c=a-b;

                       printf("Sub:%f",c);

                       break;

                case 3: c=a/b;

                       printf("Div:%f",c);

                       break;

 

                case 4: c=a*b;

                       printf("Muli:%f",c);

                       break;

                case 5: c=a%b;

                       printf("Modulo Div:%f",c);

                       break;

                default:

                   printf("U enter worng choice");

 

                }

   getch();

  }

 

11.    Write a program to print Fibonacci sequence 0               1    1    2     3     5     8    13…… N terms and prints the sum of sequence.

#include<conio.h>

#include<stdio.h>

void main()

{

        int a=0,b=1,n,i,c=1,sum=1;

        clrscr();

        printf("Enter the Last Number of series");

        scanf("%d",&n);

        printf("\n The Fibbonacci Series is ");

        printf(" %d %d ",a,b);

        while(c!=n)

        {

                        c=a+b;

                        if(c>n)

                        {

                        break;

                        }

                        printf(" %d ",c);

                        sum=sum+c;

                        a=b;

                        b=c;

 

        }

        printf("\n The Sum of fibbonacci Series is %d",sum);

        getch();

}

 

12.    Write a program to print all prime numbers <= a given number.

 

#include<conio.h>

#include<stdio.h>

void main()

{

                int i, j, n;

                clrscr();

printf("enter number upto which you want to print prime numbers ");

scanf("%d",&n);

 

for(i=2;i<=n;i++)

{

                for(j=2;j<i;j++)

                {

                                if(i%j==0)

                                break;

                }

                if(j==i)

                printf("\t%d",i);

}

getch();

}

 

13.    Write a program in C to perform Sequential/Linear Search. (OR to Search a number in a given list)

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10],i, search_item;

clrscr();

printf("enter ten no");

for(i=0;i<10;i++)

{

printf("enter the %d element of the array",i);

scanf("%d",&a[i]);

}

printf("\n enter the element to be searched");

scanf("%d",&search_item);

for(i=0;i<10;i++)

{

    if(a[i]= =search_item)

    {

      printf("%d is present in the list",search_item);

      break;

    }

}

getch();

}

 

 

14.    Write a program to find product, sum, average, max and min from a list of n numbers.

#include<stdio.h>

#include<conio.h>

 

void main()

{

                int a[20],i,n,prod=1,sum=0,max,min;

                float average;

                clrscr();

                /* Determination of Size */

                printf("Enter the size of List i.e. n not more than 20");

                scanf("%d",&n);

 

                /* Entry the values into Array  */

                printf("\nEnter the %d values into array \n",n);

                for(i=0;i<n;i++)

                {

                                                printf("Enter the Number a[%d]",i);

                                                scanf("%d",&a[i]);

                }

 

                /* Product of Numbers  */

                for(i=0;i<n;i++)

                {

                                prod=prod*a[i];

                }

 

                /* Summation of Numbers  */

                for(i=0;i<n;i++)

                {

                                sum=sum+a[i];

                }

                average=sum/n;

                max=a[0];

                min=a[0];

                for(i=1;i<n;i++)

                {

                                if(a[i]>max)

                                                max=a[i];

                }

                for(i=1;i<n;i++)

                {

                                if(a[i]<min)

                                                min=a[i];

                }

                printf("\nThe Product of %d Numbers=%d",n,prod);

                printf("\nThe Sum of %d Numbers=%d",n,sum);

                printf("\nThe Average of %d Numbers=%f",n,average);

                printf("\nThe Maximum of %d Numbers=%d",n,max);

                printf("\nThe Minimum of %d Numbers=%d",n,min);

                getch();

}

15.    Write a ‘C’ program to swap two numbers: using third variable and without using third variable.

With Third Variable:

#include<stdio.h>

#include<conio.h>

 

void main()

  {

   int a,b,t;

   clrscr();

   printf("Enter The Numbers to be swapped");

   scanf("%d%d",&a,&b);

   printf("\n Numbers before swap are a=%d b=%d",a,b);

 

   /* Logic for Swap using third Variable */

   t=a;

   a=b;

   b=t;

    printf("\n Numbers After swap are a=%d b=%d",a,b);

   getch();

}

 

/* Without Third Variable: */

 

#include<stdio.h>

#include<conio.h>

 

void main()

  {

   int a,b;

   clrscr();

   printf("Enter The Numbers to be swapped");

   scanf("%d%d",&a,&b);

   printf("\n Numbers before swap are a=%d b=%d",a,b);

 

   /* Logic for Swap without using third Variable */

   a=a+b;

   b=a-b;

   a=a-b;

    printf("\n Numbers After swap are a=%d b=%d",a,b);

   getch();

}

 

16.    WAP to find the reverse of a number and check whether it is palindrome or not.

#include<stdio.h>

#include<conio.h>

 

void main()

  {

   int n,num,rev=0,temp;

   clrscr();

   printf("Enter The Number");

   scanf("%d",&n);

                num =n;

   while(n!=0)

   {

                temp=n%10;

                rev=rev*10+temp;

                n=n/10;

   }

   printf("\nreverse of given number = %d",rev);

   if(rev==num)

   printf("\n%d is palindrom",num);

   else

   printf("\n%d is not palindrom",num);

   getch();

}

 

17.    Write a program to accept three numbers and smallest number among them.

Smallest Number:

 

#include<stdio.h>

#include<conio.h>

void main()

{

   int a,b,c;

   printf(“enter 3 nos”);

   scanf(“%d%d%d”,&a,&b,&c);

   if((a<b)&&(a<c))

          printf(“smallest no among given three no is %d”,a);

   else

  {     if(b<c)

               printf(“smallest no among given three no is %d”,b);

        else

               printf(“smallest no among given three no is %d”,c);}

  }

getch();

}

 

Largest Number:

 

#include<stdio.h>

#include<conio.h>

void main()

{

   int a,b,c;

   printf(“enter 3 nos”);

   scanf(“%d%d%d”,&a,&b,&c);

   if((a>b)&&(a>c))

          printf(“Largest no among given three no is %d”,a);

   else

  {     if(b>c)

               printf(“Largest no among given three no is %d”,b);

        else

               printf(“Largest no among given three no is %d”,c);}

  }

getch();

}

 

18.    What is pointer? Write a C program to swap the values of two variables making use of pointers.

Answer: Pointer: In general, Pointer is something, which points to something specific. With reference to C languahe, A pointer is a special variable, which holds the address of another variable.

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b;

printf(“enter 2 values”);

scanf(“%d%d”,&a,&b);

swap(a,b);

printf("%d%d",b,a);

getch();

}

swap(int *x,int *y)

{

int t;

t=*x;

*x=*y;

*y=t;

printf("%d%d",x,y);

}

 

19.    Write a program to calculate the factorial of a number without using function, using function and using recursion

Without function

#include<stdio.h>

#include<conio.h>

void main()

{

     int n,i;

     long int f=1;

     clrscr();

     printf("Enter the number");

     scanf("%d",&n);

     for(i=1;i<=n;i++)

     {

    f=f*i;

     }

     printf("The factorial is %ld",f);

     getch();

}

 Using function

'#include<stdio.h>

#include<conio.h>

long int fact(int n);

void main()

{

     int n,i;

     long int f;

     clrscr();

     printf("Enter the number");

     scanf("%d",&n);

     f=fact(n);

     printf("The factorial is %ld",f);

     getch();

}

long int fact(int n)

{

     long int f=1;

     int i;

     for(i=1;i<=n;i++)

     {

              f=f*i;

     }

     return(f);

}

Using Recursion

#include<stdio.h>

#include<conio.h>

long int fact(int n);

void main()

{

     int n,i;

     long int f;

     clrscr();

     printf("Enter the number");

     scanf("%d",&n);

     f=fact(n);

     printf("The factorial is %ld",f);

     getch();

}

long int fact(int n)

{

     long int f;

     if(n==1)

                  return(1);

     else

                 f=n*fact(n-1);

     return(f);

}

                

 

20.    Write a program to find out the product/Multiplication of two matrices and print the product matrix.

#include<stdio.h>

#include<conio.h>

 

void main()

{

                int a[10][10],b[10][10],c[10][10],i,j,k,n;

                clrscr();

                /* Determination of Size */

                printf("Enter the size of matrix n (nxn) not nore than 10");

                scanf("%d",&n);

 

                /* Entry of Matrix A */

                printf("\nEnter the values in matrix A\n");

                for(i=0;i<n;i++)

                {

                                for(j=0;j<n;j++)

                                {

                                                printf("Enter the Number a[%d][%d]",i,j);

                                                scanf("%d",&a[i][j]);

                                 }

                }

                /* Entry of Matrix B */

                printf("\nEnter the values in matrix B\n");

                for(i=0;i<n;i++)

                {

                                for(j=0;j<n;j++)

                                {

                                                printf("Enter the Number b[%d][%d]",i,j);

                                                scanf("%d",&b[i][j]);

                                 }

                }

 

                /* Initialization of Matrix C */

                for(i=0;i<n;i++)

                {

                                for(j=0;j<n;j++)

                                {

                                                c[i][j]=0;

                                }

                }

                /* Multiplication of Matrix A & B */

                for(i=0;i<n;i++)

                {

                                for(j=0;j<n;j++)

                                {

                                                for(k=0;k<n;k++)

                                                {

                                                                c[i][j]=c[i][j]+a[i][k]*b[k][j];

                                                }

                                }

                }

                /* Printing of Matrix C */

                printf("\nThe Matrix C\n");

                for(i=0;i<n;i++)

                {

                                for(j=0;j<n;j++)

                                {

                                                printf("\t%d",c[i][j]);

                                }

                                printf("\n");

                }

       getch();

}

 

21.    Write a program to accept two matrices of the same order find out the sum of these matrices and print the sum of matrices.

#include<stdio.h>

#include<conio.h>

 

void main()

{

                int a[10][10],b[10][10],c[10][10],i,j,n;

                clrscr();

                /* Determination of Size */

                printf("Enter the size of matrix n (nxn) not nore than 10");

                scanf("%d",&n);

 

                /* Entry of Matrix A */

                printf("\nEnter the values in matrix A\n");

                for(i=0;i<n;i++)

                {

                                for(j=0;j<n;j++)

                                {

                                                printf("Enter the Number a[%d][%d]",i,j);

                                                scanf("%d",&a[i][j]);

                                 }

                }

                /* Entry of Matrix B */

                printf("\nEnter the values in matrix A\n");

                for(i=0;i<n;i++)

                {

                                for(j=0;j<n;j++)

                                {

                                                printf("Enter the Number b[%d][%d]",i,j);

                                                scanf("%d",&b[i][j]);

                                 }

                }

 

                /* Addition of Matrix A & B */

                for(i=0;i<n;i++)

                {

                                for(j=0;j<n;j++)

                                {

                                                c[i][j]=a[i][j]+a[i][j];

                                }

                }

                /* Printing of Matrix C */

                printf("\nThe Matrix C\n");

                for(i=0;i<n;i++)

                {

                                for(j=0;j<n;j++)

                                {

                                                printf("\t%d",c[i][j]);

                                }

                                printf("\n");

                }

                getch();

}

 

22.    WAP in C that accepts N*N matrix as input and print transpose of this matrix.

#include<stdio.h>

#include<conio.h>

 

void main()

{

                int a[10][10],b[10][10],i,j,n;

                clrscr();

                /* Determination of Size */

                printf("Enter the size of matrix n (nxn) not nore than 10");

                scanf("%d",&n);

 

                /* Entry of Matrix A */

                printf("\nEnter the values in matrix A\n");

                for(i=0;i<n;i++)

                {

                                for(j=0;j<n;j++)

                                {

                                                printf("Enter the Number a[%d][%d]",i,j);

                                                scanf("%d",&a[i][j]);

                                 }

                }

 

                /* Transpose of Matrix A */

                for(i=0;i<n;i++)

                {

                                for(j=0;j<n;j++)

                                {

                                                b[i][j]=a[j][i];

                                }

                }

                /* Printing of Matrix B */

                printf("\nThe Matrix C\n");

                for(i=0;i<n;i++)

                {

                                for(j=0;j<n;j++)

                                {

                                                printf("\t%d",b[i][j]);

                                }

                                printf("\n");

                }

                getch();

}

 

23.    Write a program in C to accept an integer numbers and find sum of digits.

#include<stdio.h>

#include<conio.h>

void main()

  {

   int n,x,sum=0;

   clrscr();

   printf("Enter The Number");

   scanf("%d",&n);

   while(n!=0)

   {

                x=n%10;

                sum=sum+x;

                n=n/10;

   }

   printf("\nThe Sum of Digits is=%d",sum);

   getch();

}

 

24.    Write a program to copy the contents of one file to another file.

#include<stdio.h>

#include<conio.h>  

#include<stdlib.h>

void main()  

{  

    FILE *fs,*ft;  

   char ch;  

    clrscr();    

    fs = fopen(“Source.c”,"r");  

  if(fs==NULL)  

    {  

    printf("Cannot open source file ! Press key to exit.");  

    getch();  

    exit(0);  

   }  

  

   ft = fopen(“Destination.c”,"w");  

    if(ft==NULL)  

    {  

    printf("Cannot copy file ! Press key to exit.");  

    fclose(fs);  

  getch();  

    exit(0);  

    }  

  

    while(1)  

    {  

    ch = getc(fs);  

    if(ch==EOF)  

    {  

   break;  

    }  

    else  

    putc(ch,ft);  

    }  

  

    printf("File copied succesfully!");  

    fclose(fs);  

    fclose(ft);  

}  

 

25.    What do you mean by structure? Declare a structure name student containing members name, roll_no, marks. Create an array of 30 such students. Write a program to read and display the contents of array.

Answer: Structure: A structure is basically a logical group of multiple items of different data types such as int, float etc.

 

#include<stdio.h>

#include<conio.h>

struct student

{

char name[20];

int roll;

char branch[10];

};

struct student std[100];

void main()

{

clrscr();

int i;

for(i=0;i<100;i++)

{

printf("enter student detail");

scanf("%s%d%s",std[i].name,&std[i].roll,std[i].branch);

}

for(i=0;i<100;i++)

printf("%s%d%s",std[i].name,std[i].roll,std[i].branch);

getch();

}

 

26.    Write a simple database program in C which stores personal details of 100 persons such as Name, Date of Birth, Address, Phone number etc.

#include<stdio.h>

#include<conio.h>

struct info

{

char name[20];

int pn;

char dob[10];

char add[10];

};

struct info std[100];

void main()

{

clrscr();

int i;

for(i=0;i<100;i++)

{

printf("enter student detail");

scanf("%s%d%s%s",std[i].name,&std[i].pn,std[i].dob,std[i].add);

}

for(i=0;i<100;i++)

printf("%s%d%s%s",std[i].name,std[i].pn,std[i].dob,std[i].add);

getch();

}

 

 

 

Comments

Popular posts from this blog

The python program allows the user to enter the Sales amount and Actual cost of a Product. Next, Python calculates the Loss Amount or profit Amount based on those two values using if-elif-else Statement

Mealy and Moore Machine

Question Bank of Theory of Automata & Formal Languages