C Language Programs Source Codes

on 04 February 2011
This are the source code of all the programs which are mentioned in the previous article: 45 C Language Programs List for Exam Preparation. You need to compile this programs using compiler like TURBO C so that you can run the program.


//1. Program to find area and circumference of circle.
//Visit: jabroo.blogspot.com for computer tips and tricks.
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
float pi=3.14,area,ci;
clrscr();
printf("enter radius of circle: ");
scanf("%d",&r);
area=pi*r*r;
printf("area of circle=%f",area);
ci=2*pi*r;
printf("circumference=%f",ci);
getch();
}


//2. Program to find the simple interest.
#include<stdio.h>
#include<conio.h>


void main()
{
int p,r,n,si;
clrscr();
printf("Enter principle, Rate of interest & time to find simple interest: ");
scanf("%d%d%d",&p,&r,&n);
si=(p*r*n)/100;
printf("simple intrest= %d",si);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//3. Program to convert temperature from degree centigrade to Fahrenheit.
#include<stdio.h>
#include<conio.h>


void main()
{
float c,f;
clrscr();
printf("Enter temp in centigrade: ");
scanf("%f",&c);
f=(1.8*c) 32;
printf("Temp in Fahrenheit=%f",f);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//4.Program to calculate sum of 5 subjects & find percentage.
#include<stdio.h>
#include<conio.h>


void main()
{
int s1,s2,s3,s4,s5,sum,total=500;
float per;
clrscr();
printf("enter marks of 5 subjects: ");
scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5);
sum=s1+s2+s3+s4+s5;
printf("sum=%d",sum);
per=(sum*100)/total;
printf("percentage=%f",per);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//5. Program to show swap of two no's without using third variable.
#include<stdio.h>
#include<conio.h>


void main()
{
int a,b;
clrscr();
printf("enter value for a & b: ");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("after swapping the value of a & b: %d %d",a,b);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//6. Program to reverse a given number.
#include<stdio.h>
#include<conio.h>


void main()
{
int n,a,r=0;
clrscr();
printf("enter any no to get its reverse: ");
scanf("%d",&n);
while(n>=1)
{
a=n%10;
r=r*10 a;
n=n/10;
}
printf("reverse=%d",r);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//7. Program to find gross salary.
#include<stdio.h>
#include<conio.h>


void main()
{
int gs,bs,da,ta;
clrscr();
printf("enter basic salary: ");
scanf("%d",&bs);
da=(10*bs)/100;
ta=(12*bs)/100;
gs=bs da ta;
printf("gross salary=%d",gs);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//8. Program to print a table of any number.
#include<stdio.h>
#include<conio.h>


void main()
{
int gs,bs,da,ta;
clrscr();
printf("enter basic salary: ");
scanf("%d",&bs);
da=(10*bs)/100;
ta=(12*bs)/100;
gs=bs+da+ta;
printf("gross salary=%d",gs);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//9. Program to find greatest in 3 numbers.
#include<stdio.h>
#include<conio.h>


void main()
{
int a,b,c;
clrscr();
printf("enter value of a, b & c: ");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
printf("a is greatest");
if((b>c)&&(b>a))
printf("b is greatest");
if((c>a)&&(c>b))
printf("c is greatest");
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//10. Program to show the use of conditional operator.
#include<stdio.h>
#include<conio.h>


void main()
{
clrscr();
printf("enter value for a & b: ");
scanf("%d%d",&a,&b);
(a>b)?printf("a is greater"):printf("b is greater");
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//11. Program to find that entered year is leap year or not.
#include<stdio.h> 
#include<conio.h>
void main() 
{
    int year;
    printf("Enter the Year that you want to check : ");
    scanf("%d", &year);
  
    if(year % 400 == 0)
        printf("%d is a Leap Year.", year);
   else
        if(year % 100 == 0)
            printf("%d is not a Leap Year.", year); 
       else
            if(year % 4 == 0)
                 printf("%d is a Leap Year.", year);
            else
                 printf("%d is not a Leap Year", year);
   getch();
}

//12. Program to find whether given no is even or odd.
#include<stdio.h>
#include<conio.h>


void main()
{
int n;
clrscr();
printf("enter any no: ");
scanf("%d",&n);
if(n%2==0)
printf("no is even");
else
printf("no is odd");
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//13. Program to shift inputed data by two bits to the left.
#include<stdio.h>
#include<conio.h>


void main()
{
int x,y;
clrscr();
printf("Read the integer from keyboard :- ");
scanf("%d",&x);
x<<=3;
y=x;
printf("\nThe left shifted data is = %d ",y);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//14. Program to use switch statement. Display Monday to Sunday.
#include<stdio.h>
#include<conio.h>


void main()
{
char ch;
clrscr();
printf("enter m for Monday\nt for Tuesday\nw for Wednesday\nh for Thursday\nf for Friday\ns for Saturday\nu for Sunday);
scanf("%c",&ch);
switch(ch)
{
case 'm':
case 'M':
printf("monday");
break;
case 't':
case 'T':
printf("tuesday");
break;
case 'w':
case 'W':
printf("wednesday");
break;
case 'h':
case 'H':
printf("thusday");
break;
case 'f':
case 'F':
printf("friday");
break;
case 's':
case 'S':
printf("saturday");
break;
case 'u':
case 'U':
printf("sunday");
break;
default :
printf("wrong input");
break;
}
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//15. Program to display arithmetic operator using switch case.
#include<stdio.h>
#include<conio.h>


void main()
{
int a,b,n,s,m,su,d;
clrscr();
printf("enter two no's : ");
scanf("%d%d",&a,&b);
printf("enter 1 for sum\n2 for multiply\n3for subtraction\n4 for division: ");
scanf("%d",&n);
switch(n)
{
case 1:
s=a b;
printf("sum=%d",s);
break;
case 2:
m=a*b;
printf("multiply=%d",m);
break;
case 3:
su=a-b;
printf("subtraction=%d",su);
break;
case 4:
d=a/b;
printf("divission=%d",d);
break;
default:
printf("wrong input");
break;
}
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//16. Program to display first 10 natural no & their sum.
#include<stdio.h>
#include<conio.h>


void main()
{
int i,sum=0;
clrscr();
for(i=1;i<=10;i  )
{
printf("%d no is= %d\n",i,I);
sum=sum i;
}
printf("sum =%d",sum);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//17. Program to print stars Sequence 1.
#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();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//18. Program to print stars Sequence 2.
#include<stdio.h>
#include<conio.h>


void main()
{
int i,j,k;
clrscr();
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
printf(" ");
for(k=1;k<=i;k++)
printf("*");
printf("\n");
}
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//19. Program to print star Sequence 3.
#include<stdio.h>
#include<conio.h>


void main()
{
int i,j,k;
clrscr();
for(i=1;i<=3;i++)
{
for(j=3;j>=i;j--)
printf(" ");
{
for(k=1;k<=i*2-1;k++)
printf("*");
}
printf("\n");
}
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//20. Program to print Fibonacci series up to 100.
#include<stdio.h>
#include<conio.h>


void main()
{
int a=1,b=1,c=0,i;
clrscr();
printf("%d\t%d\t",a,b);
for(i=0;i<=10;i++)
{
c=a b;
if(c<100)
{
printf("%d\t",c);
}
a=b;
b=c;
}
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//21. Program to find factorial of a number.
#include<stdio.h>
#include<conio.h>


void main()
{
int n,i,fact=1;
clrscr();
printf("Enter any no: ");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
fact=fact*i;
}
printf("Factorial=%d",fact);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//22. Program to find whether given no is a prime no or not.
#include<stdio.h>
#include<conio.h>


void main()
{
int i,n,r=0;
clrscr();
printf("Enter any no: ");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
r=1;
break;
}
if(r==0)
printf("prime no");
else
printf("Not prime");
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//23. Program to display sum of series 1+1/2+1/3+.......+ 1/n.
#include<stdio.h>
#include<conio.h>


void main()
{
int n,i,sum=0;
clrscr();
printf("Enter any no: ");
scanf("%d",&n);
printf("1");
for(i=2;i<=n-1;i++)
printf(" 1/%d  ",i);
for(i=1;i<=n;i++)
sum=sum+i;
printf(" 1/%d",n);
printf("\nSum=1/%d",sum 1/n);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//24. Program to display series and find sum of 1+3+5+........+n.
#include<stdio.h>
#include<conio.h>


void main()
{
int n,i,sum=0;
clrscr();
printf("Enter any no: ");
scanf("%d",&n);
for(i=1;i<n;i=i 2)
{
printf("%d ",i);
sum=sum i;
}
printf("%d",n);
printf("\nsum=%d",sum n);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//25. Program to use bitwise AND operator between the two integers.
#include<stdio.h>
#include<conio.h>


void main()
{
int a,b,c;
clrscr();
printf("Read the integers from keyboard:- ");
scanf("%d %d",&a,&b);
c=a&b;
printf("\nThe Answer after ANDing is:  %d ",c);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//26. Program to add two number using pointer.
#include<stdio.h>
#include<conio.h>


void main()
{
int *p1,*p2,sum;
clrscr();
printf("enter two no's: ");
scanf("%d%d",&*p1,&*p2);
sum=*p1 *p2;
printf("sum=%d",sum);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//27. Program to show sum of 10 elements of array & show the average.
#include<stdio.h>
#include<conio.h>


void main()
{


int a[10],i,sum=0;
float av;
clrscr();
printf("enter elements of an aaray: ");


for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i  )
sum=sum a[i];


printf("sum=%d",sum);
av=sum/10;
printf(%u201Caverage=%.2f%u201D,av);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//28. Program to find the maximum no in an array.
#include<stdio.h>
#include<conio.h>


void main()
{
int a[5],max,i;
clrscr();
printf("enter element for the array: ");
for(i=0;i<5;i  )
scanf("%d",&a[i]);
max=a[0];
for(i=1;i<5;i  )
{
if(max<a[i])
max=a[i];
}
printf("maximum no= %d",max);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//29. Program to display matrix.
#include<stdio.h>
#include<conio.h>


void main()
{
int a[3][2],b[3][2],i,j;
clrscr();
printf("enter value for a matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
printf("enter value for b matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
}
printf("\na matrix is\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",a[i][j]);
}
printf("\n");
}


printf("\nb matrix is\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",b[i][j]);
}
printf("\n");
}
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//30. Program to find sum of two matrices.
#include<stdio.h>
#include<conio.h>


void main()
{
int a[3][2],b[3][2],c[3][2],i,j;
clrscr();
printf("Enter value for 1 matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
printf("Enter value for 2 matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
c[i][j]=a[i][j] b[i][j];
}
printf("Sum of matrix is\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//31. Program to find subtraction of two matrices.
#include<stdio.h>
#include<conio.h>


void main()
{
int a[5],b[5],c[5],i;
clrscr();
printf("enter value for array a ");
for(i=0;i<5;i  )
scanf("%d",&a[i]);
printf("enter value for array b ");
for(i=0;i<5;i  )
scanf("%d",&b[i]);
for(i=0;i<5;i  )
c[i]=a[i]-b[i];
printf("subtraction");
for(i=0;i<5;i  )
printf(" %d ",c[i]);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//32. Program to multiplication , addition & subtraction of two matrices.

#include <stdio.h>
#include <windows.h>
#include <conio.h>
void add();
void sub();
void mul();
void getmatrix();
int operation();
void display();

int a[3][3],b[3][3],c[3][3];
int i,j;
int main()
{
    int ch;
    getmatrix();
do
{
    ch=operation();
switch (ch)
{
 case 1:
        add();
        printf("The matrix after addition is : \n");
        display();
        break;

 case 2:
        sub();
        printf("The matrix after substraction is : \n");
        display();
        break;

 case 3:
        mul();
        printf("The matrix after multiplication is : \n");
        display();
         break;

  case 4:
       display();
       break;
  
  case 5:
       getmatrix();
       break;
       
  case 6:
       break;
       
  default:
          printf("\nYou entered invalid input!");
          break;
  
}
printf("\n");
getch();
}
while(ch!=6);
free (a);
return 0;
}

void add()
{
    for(i=0; i<=2; i++)
    {
        for(j=0; j<=2; j++)
        {
        c[i][j]=a[i][j]+b[i][j];
        }
    }
}

void sub()
{
    for(i=0; i<=2; i++)
    {
        for(j=0; j<=2; j++)
        {
        c[i][j]=a[i][j]-b[i][j];
        }
    }
}

void mul()
{
int m;
    for(i=0; i<=2; i++)
    {
        for(j=0; j<=2; j++)
        {
                m=0;
            for(int n=0; n<=2; n++)
            {
             m+=a[i][n]*b[n][j];
             }
             c[i][j]=m;
        }
    }
}

void getmatrix()
{
     printf("Input Values for First Matrix...\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter value of a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n\n");
printf("Input Values for Second Matrix...\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter value of b[%d][%d]:",i,j);
scanf("%d",&b[i][j]);
}
}
}

void display()
{
     for(i=0; i<=2; i++)
    {
        for(j=0; j<=2; j++)
        {
        printf("%d\t",c[i][j]);
        }
         printf("\n");
    }
     
}

int operation()
{
    int ch;
    system("cls");
    printf("\n*********************************************************\n");
    printf("Press 1 for matrix addition\nPress 2 for matrix substration\nPress 3 for matrix multiplication\nPress 4 to display\nPress 5 to enter new matrices\nPress 6 for exit\n");
    printf("*********************************************************\n\n");
    scanf("%d",&ch);
    return ch;
}




//33. Program to find transpose of a matrix.
#include<stdio.h>
#include<conio.h>


void main()
{
int a[3][2],b[2][3],i,j;
clrscr();
printf("Enter value for matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
printf("Matrix:\n");
for(i=0;i<3;i  )
{
for(j=0;j<2;j  )
printf(" %d ",a[i][j]);
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
b[j][i]=a[i][j];
}
printf("Transpose matrix:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf(" %d ",b[i][j]);
printf("\n");
}
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//34. Program to find the maximum number in array using pointer.
#include<stdio.h>
#include<conio.h>


void main()
{
int max,i,*a[5];
clrscr();
printf("enter element for the array: ");
for(i=0;i<5;i++)
scanf("%d",&*a[i]);
max=*a[0];
for(i=1;i<5;i++)
{
if(max<*a[i])
max=*a[i];
}
printf("maximum no= %d",max);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//35. Program to reverse a number using pointer.
#include<stdio.h>
#include<conio.h>


void main()
{
int *n,a,r=0;
clrscr();
printf("enter any no to get its reverse: ");
scanf("%d",&*n);
while(*n>=1)
{
a=*n%10;
r=r*10 a;
*n=*n/10;
}
printf("reverse=%d",r);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//36. Program to show input and output of a string.
#include<stdio.h>
#include<conio.h>


void main()
{
char a[50];
clrscr();
printf("enter any string: ");
gets(a);
puts(a);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}


//37. Program to find square of a number using functions.
#include<stdio.h>
#include<conio.h>


void main()
{
int rev(int);
int r,a;
clrscr();
printf("enter any no: ");
scanf("%d",&a);
r=rev(a);
printf("square is : %d",r);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}
int rev(int x)
{
return(x*x);
}


//38. Program to swap two numbers using functions.
#include<stdio.h>
#include<conio.h>


void main()
{
void swap(int,int);
int a,b,r;
clrscr();
printf("enter value for a&b: ");
scanf("%d%d",&a,&b);
swap(a,b);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("after swapping the value for a & b is : %d %d",a,b);
}


//39. Program to find factorial of a number using functions.
#include<stdio.h>
#include<conio.h>


void main()
{
int a,f;
int fact(int);
clrscr();
printf("enter a no: ");
scanf("%d",&a);
f=fact(a);
printf("factorial= %d",f);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}
int fact(int x)
{
int fac=1,i;
for(i=x;i>=1;i--)
fac=fac*i;
return(fac);
}


//40. Program to show table of a number using functions.
#include<stdio.h>
#include<conio.h>


void main()
{
void table();
clrscr();


table();
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}
void table()
{
int n,i,r;
printf("enter a no to know table: ");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
r=n*i;
printf("%d*%d=%d\n",n,i,r);
}
}


//41. Program to show call by value.
#include<stdio.h>
#include<conio.h>


void main()
{
int a,b,swap();
clrscr();
a=5;
b=10;
printf("value of a=%d & value of b=%d before swap ",a,b);
swap(a,b);
printf("\nvalue of a =%d & b=%d after swap",a,b);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}
int swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
}


//42. Program to show call by reference.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,*aa,*bb,swap();
clrscr();
a=5;
b=10;
aa=&a;
bb=&b;
printf("value of a= %d & value of b=%d before swap",a,b);
swap(aa,bb);
printf("\nvalue of a=%d & b=%d after swap",a,b);
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}
int swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}


//43. Program to find largest of two numbers using functions.
#include<stdio.h>
#include<conio.h>


void main()
{
void max();
clrscr();
max();
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}
void max()
{
int a[5],max,n,i;
printf("How many no's you want to enter: ");
scanf("%d",&n);
printf("Enter element for the array: ");
for(i=0;i<n;i  )
scanf("%d",&a[i]);
max=a[0];
for(i=1;i<5;i  )
{
if(max<a[i])
max=a[i];
}
printf("maximum no= %d",max);
}


//44. Program to find factorial of a number using recursion.
#include<stdio.h>
#include<conio.h>


void main()
{
int n;
clrscr();
printf("enter number: ");
scanf("%d",&n);
if(n<0)
printf("invalid number");
else
printf("%d!=%d",n,fact(n));
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}
int fact(int x)
{
if(x==0)
return 1;
else
return(x*fact(x-1));
}


//45. Program to find whether a string is palindrome or not.
#include<stdio.h>
#include<conio.h>


void main()
{
char s1[20],s2[20];
clrscr();
printf("enter a string: ");
scanf("%s",s1);
strcpy(s2,s1);
strrev(s2);
if(strcmp(s1,s2)==0)
printf("string is a palindrome");
else
printf("not a palindrome string");
getch();
//Visit: jabroo.blogspot.com for computer tips and tricks.
}

9 comments:

Rohan said...

program 32 logic is wrong i think!!1

Anonymous said...

These r the basic programs to be learnt.Thanks for providing these.But the multiplication of matrices is a wrong one.

Akash said...

Thanks for informing. Now I have replaced that program with the correct one.

Anonymous said...

I need some other programs too such as armstrong no.,combination using function,gcd using recursion,lower case to upper case..help me out plz!!

Unknown said...

Thank you for the programs

Akash said...

Due to some problem the '+' (plus symbol) was replaced by space. We have tried to solve this problem as far as possible. Although if you notice any errors while compiling any of the above programs please comment here with the program number. You can also comment for any queries related to other C Programs not mentioned above.

manoj said...

leap year logic is wrong ex:1700 is not a leap year

Akash said...

@manoj Thanks. I have updated the program now.

Unknown said...

this programs are helping a lot to me in learning c language













Post a Comment