Skip to main content

Programming in C

 Programs to understand the basic data types and I/O

// Program to show the use of basic datatypes
#include<stdio.h>
void main()
{
            int a;
            float b;
            double c;
            long double sum;
            char d;
            clrscr();
            printf("Enter the values of a,b and c: \n");
            scanf("%d%f%lf",&a,&b,&c);
            sum = a + b + c;
            printf("Sum = %Lf\n",sum);
            printf("Enter a character: ");
            scanf("%s",&d);
            printf("Entered character = %c",d);
            getch();

}

Programs on operator and Expressions

//Program to accept Empcode,Emp Name and Basic Salary
#include<stdio.h>
void main()
{
            int empcode;
            char name;
            float bp,da,hra,ca,ta,gross,ded,pt,it,net;
            clrscr();
            printf("Enter the Employee code\n");
            scanf("%d",empcode);
            printf("Enter the Name of the Employee\n");
            scanf("%s",&name);
            printf("Enter the Basic Pay of the Employee\n");
            scanf("%f",&bp);
            da=10.0/100.0*bp;
            hra=5/100*bp;
            ca=12/100*bp;
            ta=200;
            gross=bp+da+hra+ca+ta;
            pt=5.0/100.0*bp;
            it=10.0/100.0*bp;
            //total deductions are
            ded=pt+it;
            //net salary
            net=gross-ded;
            printf("The Basic Salary is=%f",bp);
            printf("Gross Salary is =%.2f\n",gross);
            printf("Total deduction =%.2f\n",ded);
            printf("Net Salary =%.2f\n",net);
            getch();
}

Programs on Decision Statements


//largest of 3 numbers
#include <stdio.h>

void main()
{
    int num1, num2, num3;

    printf("Enter the values of num1, num2 and num3\n");
    scanf("%d %d %d", &num1, &num2, &num3);
    printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", num1, num2, num3);
    if (num1 > num2)
    {
        if (num1 > num3)
        {
            printf("num1 is the greatest among three \n");
        }
        else
        {
            printf("num3 is the greatest among three \n");
        }
    }
    else if (num2 > num3)
        printf("num2 is the greatest among three \n");
    else
            printf("num3 is the greatest among three \n");
getch();
}

//even or odd
#include <stdio.h>
void main()
{
    int number;
    clrscr();
    printf("Enter an integer: ");
    scanf("%d", &number);

    if(number % 2 == 0)
            printf("%d is even.", number);
    else
            printf("%d is odd.", number);
getch();
}

//leap year
#include <stdio.h>
void main()
{
    int year;
    clrscr();
    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);
getch();
}

//given number is positive or negative
#include <stdio.h>
void main()
{
    int num;
    clrscr();
    printf("Enter a number: \n");
    scanf("%d", &num);
    if (num > 0)
            printf("%d is a positive number \n", num);
    else if (num < 0)
            printf("%d is a negative number \n", num);
    else
            printf("0 is neither positive nor negative");
getch();
}

// To find the roots of a quadratic equation
#include<stdio.h>
#include<math.h>
void main()
{
            int a,b,c;
            float x,x1,x2,disc,disc1;
            clrscr();
            printf("Enter the values of a,b and c\n");
            scanf("%d",&a);
            scanf("%d",&b);
            scanf("%d",&c);
            if(a==0)
            {
                        x = - b / c;
                        printf("x = %.2f \n",x);
            }
            else
            {
            disc =sqrt((b*b)-4*a*c);
            if (disc==0)
            {
                        printf("Roots are real and repeated");
                        x1 = - b / 2 * a;
                        x2 = - b / 2 * a;
                        printf("x1 = %.2f and x2 = %.2f \n",x1,x2);
            }
            else if (disc>0)
            {
                        printf("Roots are real and distinct \n");
                        x1=(- b + sqrt(disc)) / 2 * a;
                        x2=(- b - sqrt(disc)) / 2 * a;
                        printf("x1 = %.2f and x2 = %.2f \n",x1,x2);
            }
            else
            {
                        printf("Roots are complex conjugative roots \n");
                        disc1=abs(disc);
                        x1=(- b + sqrt(disc1)) / 2 * a;
                        x2=(- b - sqrt(disc1)) / 2 * a;
                        printf("x1 = %.2f and x2 = %.2f",x1,x2);
            }
            }
getch();
}

Programs on Looping

//factorial of a given number
#include <stdio.h>
void main()
{
    int n, i;
    unsigned long long factorial = 1;
    clrscr();
    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);
    }

getch();
}

//print right triangle pattern
#include <stdio.h>
int main()
{
    int i, j, n;

    /* Input number of rows from user */
    printf("Enter value of n: ");
    scanf("%d", &n);

    for(i=1; i<=n; i++)
    {
        /* Print i number of stars */
        for(j=1; j<=i; j++)
        {
            printf("*");
        }

        /* Move to next line */
        printf("\n");
    }
    return 0;
}

//given number is prime or not
#include <stdio.h>
void main()
{
    int n, i, flag = 0;
    clrscr();
    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);

getch();
}

//sum of n natural numbers
#include <stdio.h>
void main()
{
    int i, num, sum = 0;
    clrscr();
    printf("Enter an integer number \n");
    scanf ("%d", &num);
    for (i = 1; i <= num; i++)
    {
            sum = sum + i;
    }
    printf ("Sum of first %d natural numbers = %d\n", num, sum);
getch();
}

//find Fibonacci sequence
#include <stdio.h>
void main()
{
    int i, n, t1 = 0, t2 = 1, nextTerm;
    clrscr();
    printf("Enter the number of terms: ");
    scanf("%d", &n);

    printf("Fibonacci Series: ");

    for (i = 1; i <= n; ++i)
    {
            printf("%d, ", t1);
            nextTerm = t1 + t2;
            t1 = t2;
            t2 = nextTerm;
    }
getch();
}

//find enter no. is palindrome or not
#include <stdio.h>
void main()
{
    int n, reversedInteger = 0, remainder, originalInteger;
    clrscr();
    printf("Enter an integer: ");
    scanf("%d", &n);

    originalInteger = n;

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

    if (originalInteger == reversedInteger)
            printf("%d is a palindrome.", originalInteger);
    else
            printf("%d is not a palindrome.", originalInteger);

getch();
}

//largest of n numbers
#include <stdio.h>
int main()
{
    int i, n;
    float arr[100];

    printf("Enter total number of elements(1 to 100): ");
    scanf("%d", &n);
    printf("\n");

    // Stores number entered by the user
    for(i = 0; i < n; ++i)
    {
       printf("Enter Number %d: ", i+1);
       scanf("%f", &arr[i]);
    }

    for(i = 1; i < n; ++i)
    {
       // Change < to > if you want to find the smallest element
       if(arr[0] < arr[i])
           arr[0] = arr[i];
    }
    printf("Largest element = %.2f", arr[0]);

    return 0;
}

//arrange no. in ascending orders
#include <stdio.h>             
#include <conio.h>             

int main()                       
{
            int a[10] = { 3,4,7,6,5,1,2,8,10,9 };          
            int n = 10;                                    
            printf("\n\nArray Data : ");                   
            for (int i = 0; i < n; i++)                    
            {
                        printf(" %d ", a[i]);                  
            }
            for (int i = 0; i < n; i++)                    
                        for (int j = 0; j < n; j++)            
                        {
                                    if (a[j] > a[i])               
                                    {
                                                int tmp = a[i];        
                                                a[i] = a[j];           
                                                a[j] = tmp;            
                                    } 
                        }
            }
            printf("\n\nAscending : ");                    
            for (int i = 0; i < n; i++)                    
            {
                        printf(" %d ", a[i]);
            }
            for (int i = 0; i < n; i++)                     //Loop for descending ordering
            {
                        for (int j = 0; j < n; j++)             //Loop for comparing other values
                        {
                                    if (a[j] < a[i])                //Comparing other array elements
                                    {
                                                int tmp = a[i];        
                                                a[i] = a[j];           
                                                a[j] = tmp;            
                                    }
                        }
            }
            printf("\n\nDescending : ");                   
            for (int i = 0; i < n; i++)                     
            {
                        printf(" %d ", a[i]);                  
            }
            getch();                                      
            return 0;
}

Programs on arrays

//find sum of two matrix
#include <stdio.h>
int main()
{
   int m, n, c, d, first[10][10], second[10][10], difference[10][10];

   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);
   printf("Enter the elements of first matrix\n");

   for (c = 0; c < m; c++)
     for (d = 0 ; d < n; d++)
       scanf("%d", &first[c][d]);

   printf("Enter the elements of second matrix\n");

   for (c = 0; c < m; c++)
     for (d = 0; d < n; d++)
         scanf("%d", &second[c][d]);

   printf("Difference of entered matrices:-\n");

   for (c = 0; c < m; c++) {
     for (d = 0; d < n; d++) {
       difference[c][d] = first[c][d] - second[c][d];
       printf("%d\t",difference[c][d]);
     }
     printf("\n");
   }
   return 0;
}

//subtraction of two matrix
#include <stdio.h>
int main()
{
   int m, n, c, d, first[10][10], second[10][10], difference[10][10];
   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);
   printf("Enter the elements of first matrix\n");
   for (c = 0; c < m; c++)
     for (d = 0 ; d < n; d++)
       scanf("%d", &first[c][d]);
   printf("Enter the elements of second matrix\n");

   for (c = 0; c < m; c++)
     for (d = 0; d < n; d++)
         scanf("%d", &second[c][d]);
   printf("Difference of entered matrices:-\n");
   for (c = 0; c < m; c++) {
     for (d = 0; d < n; d++) {
       difference[c][d] = first[c][d] - second[c][d];
       printf("%d\t",difference[c][d]);
     }
     printf("\n");
   }
   return 0;
}

//matrix multiplication
#include <stdio.h>
int main()
{
    int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k; 
    printf("Enter rows and column for first matrix: ");
    scanf("%d %d", &r1, &c1);
    printf("Enter rows and column for second matrix: ");
    scanf("%d %d",&r2, &c2);

    while (c1 != r2)
    {
        printf("Error! column of first matrix not equal to row of second.\n\n");
        printf("Enter rows and column for first matrix: ");
        scanf("%d %d", &r1, &c1);
        printf("Enter rows and column for second matrix: ");
        scanf("%d %d",&r2, &c2);
    }
    printf("\nEnter elements of matrix 1:\n");
    for(i=0; i<r1; ++i)
        for(j=0; j<c1; ++j)
        {
            printf("Enter elements a%d%d: ",i+1, j+1);
            scanf("%d", &a[i][j]);
        }
    printf("\nEnter elements of matrix 2:\n");
    for(i=0; i<r2; ++i)
        for(j=0; j<c2; ++j)
        {
            printf("Enter elements b%d%d: ",i+1, j+1);
            scanf("%d",&b[i][j]);
        }

    for(i=0; i<r1; ++i)
        for(j=0; j<c2; ++j)
        {
            result[i][j] = 0;
        }

    for(i=0; i<r1; ++i)
        for(j=0; j<c2; ++j)
            for(k=0; k<c1; ++k)
            {
                result[i][j]+=a[i][k]*b[k][j];
            }
    printf("\nOutput Matrix:\n");
    for(i=0; i<r1; ++i)
        for(j=0; j<c2; ++j)
        {
            printf("%d  ", result[i][j]);
            if(j == c2-1)
                printf("\n\n");
        }
    return 0;
}

Program on function

//find factorial of a number
#include <stdio.h> 
int factorial(int);
int main()
{
    int num;
    int result;
    printf("Enter a number to find it's Factorial: ");
    scanf("%d", &num);
    if (num < 0)
    {
        printf("Factorial of negative number not possible\n");
    }
    else
    {
        result = factorial(num);
        printf("The Factorial of %d is %d.\n", num, result);
    }
    return 0;
}
int factorial(int num)
{
    if (num == 0 || num == 1)
    {
        return 1;
    }
    else
    {
        return(num * factorial(num - 1));}

// Program to find a factorial of a given number using function.
#include<stdio.h>
void main()
{
            int fact,n;
            int fact1(int n1);
            clrscr();
            printf("Enter the number\n");
            scanf("%d",&n);
            fact = fact1(n);
            printf("Factorial of %d is %d",n,fact);
            getch();
}
int fact1(int n1)
{
            int f1 = 1,i;
            for(i=1;i<=n1;i++)
            f1 = f1 * i;
return f1;
}

Program on structure ,unions 

//create a structure employee
#include <stdio.h>

struct employee{
    char    name[30];
    int     empId;
    float   salary;
};

int main()
{
    struct employee emp;
    
    printf("\nEnter details :\n");
    printf("Name ?:");          gets(emp.name);
    printf("ID ?:");            scanf("%d",&emp.empId);
    printf("Salary ?:");        scanf("%f",&emp.salary);
    
    printf("\nEntered detail is:");
    printf("Name: %s"   ,emp.name);
    printf("Id: %d"     ,emp.empId);
    printf("Salary: %f\n",emp.salary);
    return 0;
}

Program on Pointers

//addition and subtraction of two numbers
#include <stdio.h>

int main()
{
   int first, second, *p, *q, sum, sub;

   printf("Enter two integers \n");
   scanf("%d%d", &first, &second);

   p = &first;
   q = &second;

   sum = *p + *q;
   sub = *p + *q;

   printf("Sum of entered numbers = %d\n",sum);

   return 0;
}

Programs on String Manipulation

// Program on string operations
#include<stdio.h>
#include<string.h>
void main()
{
            int res;
            char str1[20],str2[20];
            clrscr();
            printf("Enter the first string\n");
            gets(str1);
            printf("Enter the second string\n");
            gets(str2);
            if(strcmp(str1,str2)==0)
            printf("Strings are equal\n");
            else
            printf("Strings are not equal\n");
            strcat(str1,str2);
            printf("Concatenated strings are %s \n",str1);
            res = strlen(str1);
            printf("The length of the string is %d\n",res);
            strlwr(str1);
            puts(str1);
            printf("\n");
            strupr(str1);
            puts(str1);
            strcpy(str2,str1);
            puts(str2);
            printf("%s",strchr(str1,'Y'));
            getch();
}

Programs to read and write files

//read the contents on to the file
#include<stdio.h>
void main()
{
FILE *fopen(),*fp;
int c;
clrscr();
fp=fopen("dev.txt","r");
c=getc(fp);
while(c!=EOF)
{
putchar(c);
c=getc(fp);
}
fclose(fp);
getch();
}

//append the contents on to the file
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen("dave.txt","a");
fprintf(fp,"%s","\nTHIS is an exampleof apending C FILE EXAMPLE");
fclose(fp);
}

//write the contents from to the file
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen("dave.txt","w");
fprintf(fp,"%s","\nTHIS is an exampleof apending C FILE EXAMPLE");
fclose(fp);
}




Comments