Simple C Programs


Click Here to download this Page As PDF Format


nCr FACTOR

#include<stdio.h>
#include<conio.h>
int fact(int n)
 {
            int f=1;
            while(n>0)
            {
             f=f*n;
             n=n-1;
            }
  return f;
 }

void main()
 {
            int n,nCr,r;
            clrscr();
            printf("\n\n\t\t\t nCr FACTOR\n");
            printf("\n\t\t\t*************\n");
            printf("\n\t\t Enter the n value:");
            scanf("%d",&n);
            printf("\n\t\t Enter the r value:");
            scanf("%d",&r);
            nCr=fact(n)/(fact(r)*fact(n-r));
            printf("\n\t\t nCr Factor=%d",nCr);
   getch();
 }

=================================================================



OUTPUT
                                                                                 

nCr FACTOR
************
Enter the n value:5                                           
Enter the r value:2                                           
nCr Factor=10                                                       


=====================================================================================








SORTING STRINGS

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int n,i,j;
char name[10][10],temp[10];
clrscr();
printf("\n enter the n value");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter the names");
scanf("%s",&name[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[j]);
strcpy(name[j],name[i]);
strcpy(name[i],temp);
}
}
}
printf("\n\t the string in alphabetical order");
for(i=0;i<n;i++)
{
printf("\n\t%s",name[i]);
printf("\n");
}
getch();
}

=======================================================================

                                          ACROMATIC STRING
#include<stdio.h>
#include<conio.h>
void main()
{
char str[30];
int i=0;
clrscr();
printf("\n\n\n\t\t\t ****** ACROMATIC STRING ****** ");
printf("\n\n\n\t Enter the string = ");
gets(str);
printf("\n\n\n\t%c",str[0]);
while(str[i]!='\0')
{
if(str[i]==' '||str[i]==' ')
{
if(isupper(str[i+1])!=0)
{
printf("\t%c",str[i+1]);
}
}
i++;
}
getch();
}



OUTPUTS:

                         ****** ACROMATIC STRING ******

                              Enter the string = State Bank of India

                     S       B       I


======================================================================

          COMMAND LINE ARGUMENTS
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main(int argc,char *argv[10])
{
int sum=0,i;
float avg;
for(i=1;i<argc;i++)
{
sum=sum+atoi(argv[i]);
}
avg=sum/(argc-1);
printf("%f",avg);
getch();
return(avg);
}

OUTPUT:
                                     
***** COMMAND LINE ARGUMENTS *****                       

C:\TC\BIN>cmdline 10 20 30
20.000000

=======================================================================



EMPLOYEE DETAILS
(STRUCTURE WITHIN STRUCTURE)


#include<stdio.h>
#include<conio.h>
struct address
{
char addr1[20];
char addr2[10];
char city[10];
long int pincode;
}s;
struct employee
{
int empid;
char empname[10];
int bs,hra,da,ta,pf;

struct address s;
}e;
void main()
{

int net_sal;
clrscr();
printf("\n\t\t\t     Employee Details   \n");
printf("\n\t\t\t(Structure with in structure)\n");
printf("\n\t\t  ----------------------------------------\t\n");
printf("\n Enter the Employee id:");
scanf("%d",&e.empid);
printf("\n Enter the  employee name :");
scanf("%s",&e.empname);
printf("\n Enter the address1:");
scanf("%s",&e.s.addr1);
printf("\n Enter the address2:");
scanf("%s",&e.s.addr2);
printf("\n Enter the city:");
scanf("%s",&e.s.city) ;
printf("\n Enter the pincode:");
scanf("%ld",&e.s.pincode);
printf("\n Enter the basic salary");
scanf("%d",&e.bs);
printf("\n Enter the hra");
scanf("%d",&e.hra);
printf("\n Enter the Ta");
scanf("%d",&e.ta);
printf("\n Enter the Da");
scanf("%d",&e.da);
printf("\n Enter the Pf");
scanf("%d",&e.pf);
net_sal=(e.bs+e.hra+e.ta+e.da) -e.pf;
printf("\n\n\t\t\t EMPLOYEE DETAILS\n");
printf("\n\t\t\t ------------------\n");
printf("\n \t Employee id :\t\t%d",e.empid);
printf("\n \t Name :\t\t%s",e.empname);
printf("\n \t Address:\t%s\n\t\t\t%s",e.s.addr1,e.s.addr2);
printf("\n \t city :%s",e.s.city);
printf("\n \t pincode :%ld",e.s.pincode);
printf("\n\n Basic Salary:%d",e.bs);
printf("\n\n HRA:%d",e.hra);
printf("\n\n Da:%d",e.da);
printf("\n\n TA:%d",e.ta);
printf("\n\n PF:%d",e.pf);
printf("\n\n Net Salary:%d",net_sal);
getch();
}


=========================================================================

 
STUDENT DETAILS
(STRUCTURE WITH ARRAY)
include<stdio.h>
#include<conio.h>
struct stud
{
   int rollno;
   char name[10];
   int marks[10];
   int total;
   float avg;
}s[10];
void main()
{
   int j,i,n;
   clrscr();

   printf("\t\t\t   Students Details  \n");
   printf("\n \t\t\t(Structure with array)\n");
   printf("\n\t\t\t***********************\n");
   printf("\n Enter the student number:");
   scanf("%d",&n);

   for(i=1;i<=n;i++)
   {
     printf("\n\t\t enter the student details\t\t\t");
     printf("\n\t\t -------------------------\t");
     printf("\n Enter the rollno:");
     scanf("%d",&s[i].rollno);
     printf("\n Enter the name:");
     scanf("%s",&s[i].name);
     printf("\n Enter the marks for subjects:");
     s[i].total=0;
     for(j=0;j<=2;j++)
       {
                 scanf("%d",&s[i].marks[j]);
                 s[i].total=s[i].total+s[i].marks[j];
       }
     s[i].avg=s[i].total/3;
   }
 printf("\n\t\t\t   STUDENT DETAILS\n");
 printf("\n\t\t\t   ***************\t\t\t\n");
 printf("\n\t\t SL.NO\tName\tROLLNO\tMarks\tTotal\tAverage");
 printf("\n\t\t ----------------------------------------------\n");
 for(i=1;i<=n;i++)
  {
   printf("\n\t\t %d",i);
   printf("\t%s",s[i].name);
   printf("\t%d\t",s[i].rollno);
    for(j=0;j<=2;j++)
      {
       printf("%d\n\t\t\t\t\t",s[i].marks[j]);
      }
   printf("\t%d",s[i].total);
   printf("\t%f",s[i].avg);
  }
 printf("\n\t\t -----------------------------------------------");
 getch();
}

======================================================================

CALCULATE NCR USING FUNCTION

#include<stdio.h>
#include<conio.h>

int fact(int n)
{
int f=1;
while(n>0)
{
f=f*n;
n=n-1;
}
return(f);
}
void main()
{
int n,r,ncr;
clrscr();
printf("\n enter the n value:");
scanf("%d",&n) ;
printf("\n enter the r value:");
scanf("%d",&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("\n ncr=%d",ncr);
getch();
}


=========================================================================



MEAN AND STANDARD DEVIATION

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,a[10],n;
float sum1=0,sum2=0;
 float mean,sd,sd1;
 clrscr();
 printf("\n enter the limit");
 scanf("%d",&n);
 printf("\n enter the elements");
 for(i=0;i<n;i++)
 {
 scanf("%d",&a[i]);
 }
 for(i=0;i<n;i++)
 {
 sum1=sum1+a[i];
 sum2=sum2+a[i]*a[i];
 }
 mean=sum1/n;
 sd1=sum2/n;
 sd=sqrt(sd1-mean*mean);
 printf("\n the mean =%f",mean);
 printf("\n the standard deviation =%f",sd);
 getch();
 }


========================================================================

 
FACTORIAL  USING  RECURSION
#include<stdio.h>
#include<conio.h>
int fact(int n);
void main()
{
int n;
clrscr();
printf("\n\n\n\t\t\t ****** FACTORIAL USING RECURSION ****** ");
printf("\n\n\n\t\t Enter the number =  ");
scanf("%d",&n);
printf("\n\n\t\t%d",fact(n));
getch();
}
int fact(int n)
{
if(n==1)
{
return(n);
}
else
{
n=n*fact(n-1);
return(n);
}
}





OUTPUT:

****** FACTORIAL USING RECURSION ******

Enter the number =  5
120


======================================================================


PALINDROME  STRING

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
              char s1[20],s2[20];
              clrscr();
              printf("\n\n \t================================");
              printf("\n\t\t PALINDROME  STRING ");
              printf("\n\t==================================");
              printf("\n\tEnter the String:");
              gets(s1);
              strcpy(s2,s1);
  strrev(s2);
              if( strcmp(s1,s2)==0)
              printf("\n\n\t%s  is a palindrome",s1);
              else
              printf("\n\t%s is not a Palindrome",s1);
              getch();
            }




            OUTPUT :

             ===================================
              PALINDROME  STRING           
 ===================================
              Enter the String: malayalam
             malayalam is a palindrome


====================================================================




SUM OF EVEN  AND ODD  DIGITS

#include<stdio.h>
#include<conio.h>
void main()
{
  int sum1=0,sum2=0,n,q,r;
  clrscr();
  printf(“\n\t================================\n”);
  printf(“\n\t SUM OF EVEN  AND ODD  DIGITS \n”);
  printf(“\n\t================================\n”);
  printf("\n Enter the number n:");
  scanf("%d",&n);
  while(n>0)
  {
  q=n/10;
  r=n%10;
  if(r%2==0)
  {
   sum1=sum1+r;
  }
  else
  {
  sum2=sum2+r;
  }
  n=q;
  }
  printf("\n The sum of even numbers is:%d",sum1);
  printf("\n The sum of odd numberis:%d",sum2);
  getch();
}


=================================================================


CIRCULAR RIGHT SHIFT

#include<stdio.h>
#include<conio.h>
void main()
{
  int a[10],i,n,t1,t2;
  clrscr();
  printf("\n\t=================================================\t\n");
  printf("\n\t CIRCULAR RIGHT SHIFT\t\n");
  printf("\printf("\n\t=================================================\t\n");
  printf("\n\n\tEnter the size of an array:");
  scanf("%d",&n);
  printf("\n\n\tEnter the elements of an array:");
  for(i=0;i<n;i++)
  scanf("%d",&a[i]);
  printf("\t\t\n\n\tThe original array is:\n\n\n");
  printf("\t");
  for(i=0;i<n;i++)
  printf("%d\t",a[i]);
  t1=a[n-1];
  t2=a[n-2];
  for(i=n-1;i>0;i--)
  {


  a[i]=a[i-2];
  if(i==1)
  {
  a[i]=t1;
  a[i-1]=t2;
  }
  }
  printf("\n\n\n");
  printf("\t The circular right shift array is:\n\n\n");
  printf("\t");
  for(i=0;i<n;i++)
  printf("%d\t",a[i]);
  getch();
  }        


 


          OUTPUT :

            =================================================                      
                                                                               
                        CIRCULAR RIGHT SHIFT                                                  
                                                                                
             =================================================                      
                                                                               
                                                                                
             Enter the size of an array:  7                                           

            Enter the elements of an array:  14   5   6   7   8   9  15                         

            The original array is:                                                 

            14      5       6       7       8       9       15                     

             The circular right shift array is:
           
9       15      14      5       6       7       8


 ====================================================================


 
    CONVERSION OF BINARY TO DECIMAL AND VICE VERSA

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long int q,r,i=0,j=1,sum1=0,sum2=0,n,ch;
clrscr();
printf("\t\nBinary & Decimal Convertion");
printf("\t\n");
a:printf("\t\nIf you want binary convertion press (1) for decimal press (2) : ");
scanf("%ld",&ch);
switch(ch)
{
                        case 1:
                                    printf("\t\nPlease enter the binary value : ");
                                    scanf("%ld",&n);
                                                while(n>0)
                                                            {
                                                                        r=n%10;
                                                                        q=n/10;
                                                                        sum1=sum1+r*pow(2,i);
                                                                        i=i+1;
                                                                        n=q;
                                                            }
                                    Printf("\t\nThe equalent decimal value=%ld",sum1);
                                    break;
                        case 2:
                                    printf("\t\nPlease enter the decimal value : ");
                                    scanf("%ld",&n);
                                                while(n>0)
                                                            {
                                                                        r=n%2;
                                                                        q=n/2;
                                                                        sum2=sum2+r*j;
                                                                        j=j*10;
                                                                        n=q;
                                                            }
                                    printf("\t\nThe equalent binary value = %ld",sum2);
                                    break;
                        default:
                                    printf("\n\tPlease enter the correct value :-( ");
                                    goto a;
                        }
                        getch();
                        }







Output:

Binary & Decimal Conversion

If you want binary conversion press (1) for decimal press (2) : 2

Please enter the decimal value : 7

The equalent binary value = 111



Binary & Decimal Conversion

If you want binary conversion press (1) for decimal press (2) : 1

Please enter the binary value : 1101

The equalent decimal value=13


=====================================================================





MATRIX MULTIPLICATION

#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,n,m,p,q;
clrscr();
a:printf("\nPlease enter the Range of matrix A Row:\n");
printf("***************************************\n");
scanf("%d",&n);
printf("\nPlease enter the Range of  matrix A Col:\n");
printf("*************************************\n");
scanf("%d",&m);
printf("\nPlease enter the Range of matrix B Row:\n");
printf("*************************************\n");
scanf("%d",&p);
printf("\nPlease enter the Range of matrix B Col:\n");
printf("************************************\n");
scanf("%d",&q);
if(m!=p)
{
printf("\nWe can't multiply this matrix");
goto a;
}
else
{
printf("\nPlease enter the elements mat a :\n\n");
printf("**********************************\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nPlease enter the elements mat b : \n\n");
printf("**********************************\n");
for(i=1;i<=p;i++)
{
for(j=1;j<=q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
c[i][j]=0;
for(k=1;k<=m;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
}
clrscr();
printf("\nThe original Matrix A:\n\n");
printf("*************************\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n\n");
}
printf("\nThe original Matrix B:\n\n");
printf("************************\n");
for(i=1;i<=p;i++)
{
for(j=1;j<=q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n\n");
}
printf("\nThe Result Matrix is : \n\n");                    
printf("***************************\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n\n");
}
getch();
}

















Output:
Please enter the Range of matrix A Row:
***********************************
3
Please enter the Range of  matrix A Col:
*************************************
3
Please enter the Range of matrix B Row:
***********************************
3
Please enter the Range of matrix B Col:
**********************************
3
Please enter the elements mat a :
****************************
1
2
3
4
5
6
7
8
9

Please enter the elements mat b :
*****************************
1
2
3
4
5
6
7
8
9
The original Matrix A:
********************
1       2       3

4       5       6

7       8       9
The original Matrix B:
*******************
1       2       3

4       5       6

7       8       9

The Result Matrix is :
******************
30      36      42

66      81      96

102     126     150


================================================================



FIBONACCI SERIES

#include<stdio.h>
void main()
{
int f1=0,f2=1,f3=1,n;
clrscr();
printf("Please enter the value (upto) to print the fibonacci series:");
printf("\n*************************************************\n");
scanf("%d",&n);
printf("\n%d",f1);
printf("\n%d",f2);
while(f3<n)
{
f3=f1+f2;
if(f3<n)
{
printf("\n%d",f3);
}
f1=f2;
f2=f3;
}
getch();
}



Output:
Please enter the value (Upto) to print the Fibonacci series:
************************************************
10

0
1
1
2
3
5
8


===============================================================


 

BITWISE MANIPULATION

#include<stdio.h>
#include<conio.h>
void main()
{
unsigned char a,s1,s2,s3,s4,s5,s6,s7;
unsigned char b;
clrscr();
printf("\nEnter the Digit a ");
scanf("%d",&a);
printf("\nEnter the Digit b ");
scanf("%d",&b);
printf("\n\nThe Bitwise Operation Results:");
printf("\n--------------------------------");
s1=~a;
printf("\n\n\t~a = %d",s1);
s2=~b;
printf("\n\n\t~b = %d",s2);
s3=a<<3;
printf("\n\n\ta<<3 = %d",s3);
s4=b>>2;
printf("\n\n\tb>>2 = %d",s4);
s5=a&b;
printf("\n\n\ta&b = %d",s5);
s6=a|b;
printf("\n\n\ta|b = %d",s6);
s7=a^b;
printf("\n\n\ta^b = %d",s7);
getch();
}




OUTPUT:

Enter the Digit a : 15

Enter the Digit b  : 197

The Bitwise Operation Results:
-----------------------------------------

        ~a = 240

        ~b = 58

        a<<3 = 120

        b>>2 = 49

        a&b = 5

        a|b = 207

        a^b = 202



======================================================================

 

FACTORIAL USING RECURSION

#include<stdio.h>
#include<conio.h>
int fact(int n);
void main()
{
int n;
clrscr();
printf("\n\t\tFactorial Using Recursion");
printf("\n\t\t-------------------------");
printf("\n\nPlease enter the number   : ");
scanf("%d",&n);
printf("\n\nFactorial of given number : %d",fact(n));
getch();
}

int fact (int n)
{
if(n==1)
{
return(n);
}
else
{
n=n*fact(n-1);
return(n);
}
}




 


OUTPUT :

F:\TC\BIN>cmdline 10 20 30 40 50
30.000000

 =========================================================================



No comments:

Post a Comment