C Programs

1.Program to Print Upper Triangle in C

#include<stdio.h>
#include<math.h>
int main()
{
int num,i,row,c,d,e;
printf(“Enter the number: \n”);
scanf(“%d”,&num);
for(i=num;i>0;i–)
{
c=num-i;
for(d=c;d>0;d–)
{
printf(” “);
}
for(e=i;e>0;e–)
{
printf(“*”);
}
printf(“\n”);
}
getch();
}

2.Program for Fibonacci Series

#include<stdio.h>
int main()
{
int i=0,j=1,e,k;
for(k=1;k<=50;k++)
{
e=i+j;
printf(“%d,”,e);
i=j;
j=e;
}
getch();
}

3.Program For Coins Denominations in form of Change in C

#include<stdio.h>
int main()
{
int rupees,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12;
printf(“Enter the Amount \n”);
scanf(“%d”,&rupees);//input value//
a1=rupees/10;
a2=a1*10;//calculating denominations of 10//
a3=rupees-a2;

a4=a3/5;
a5=a4*5;//calculating denominations of 5//
a6=a3-a5;

a7=a6/2;
a8=a7*2;//calculating denominations of 2//
a9=a6-a8;

a10=a9/1;
a11=a10*1;//calculating denominations of 1//
a12=a9-a11;
printf(“The Denominations are (10*%d)+(5*%d)+(2*%d)+(1*%d)=%d”,a1,a4,a7,a10,(a1*10)+(a4*5)+(a7*2)+(a10*1));
getch();
}

Other Way

#include<stdio.h>
int main()
{
int rupees,a1,a2,j=10;
printf(“Enter the Amount \n”);
scanf(“%d”,&rupees);//input Amount//
while(rupees>0)
{
a1=rupees/j;//divide with 10//
a2=a1*j;//multiply quotient with 10//
if(a2!=0)
{
printf(“%d*%d=%d,”,a1,j,a2);//display denomination//
}
rupees=rupees-a2;//subtract from amount//
j=j/2;//getting next j value//

getch();
}

4.Program for Cumulative Sum of Numbers in C

int main()
{
int number,i,j,k=0;
printf(“Enter the number”);
scanf(“%d”,&number);//input the number//
for(i=0;i<=number;i++)
{
j=i;//storing i value in j//
k=k+j;//adding the value to previous value//
printf(“%d,”,k); 
}

getch();
}

5.Program for possible ways of sums of numbers in a given series without repetition in C

#include<stdio.h>
int main()
{
int number,i,j,k,l,m;
printf(“Enter the Number \n”);
scanf(“%d”,&number);//input number//
for(i=1;i<=number-1;i++)
{
for(j=i+1;j<=number-1;j++)
{
for(l=j+1;l<=number-1;l++)
{
for(m=l+1;m<=number-1;m++)
{
k=i+j+l+m; 
if(k==number && i!=j && j!=l && i!=l && i!=m && j!=m && i<j && j<l && i<l && i<m && j<m)//condition whether k,i,j,l are satisfying are not//
{
printf(“%d+%d+%d+%d=%d\n”,i,j,l,m,k);//if yes displaying the result//
}
}
k=i+j+l; 
if(k==number && i!=j && j!=l && i!=l && i<j && j<l && i<l)//condition whether k,i,j,l are satisfying are not//
{
printf(“%d+%d+%d=%d\n”,i,j,l,k);//if yes displaying the result//
}
}
k=i+j;
if(k==number && i!=j && i<j)//condition whether k,i,j are satisfying are not//
{
printf(“%d+%d=%d\n”,i,j,k);//if yes displaying result//
}
}
}
getch();//waits until user presses a key//
}

6.Program for Average of Numbers in C using array

#include<stdio.h>
int main()
{
int a[10],sum=0,i,j=0,sum1=0,k=0,res=0,res1=0;
printf(“Enter 10 Values \n”);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<10;i++)
{
if(a[i]%2==0)
{
sum=(sum+a[i]);
j=j+1;
}
else
{
sum1=(sum1+a[i]);
k=k+1;
}
}
res=sum/j;
res1=sum1/k;
printf(“Average of all even numbers in array=%d\n”,res);
printf(“Average of all odd numbers in array=%d”,res1);
getch();
}

7.Program to find Smallest ans Second Smallest Number in C using an array

#include<stdio.h>
int main()
{
int a[10],i,j,b[10],k=0;
printf(“enter 10 values \n”);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<10;i++)
{
if(a[i]<a[i+1])
{
b[k]=a[i];
k++;
}
}
printf(“Smallest Element is %d\n”,b[0]);
printf(“second smallest is %d”,b[1]);
getch();
}

8.Program for Matrix Transpose and Matrix Multiplication with that Transpose in C

#include<stdio.h>
int main()
{
int rows,columns,i,j,sum=0,k,a[10][10],b[10][10],c[10][10],m;
printf(“Enter the rows and columns of first matrix \n”);
scanf(“%d%d”,&rows,&columns);
printf(“Enter Elements\n”);
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
scanf(“%d”,&a[i][j]);
}
printf(“\n”);
}
printf(“The matrix before transpose is\n”);
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
printf( “%d”,a[i][j]);
}
printf(“\n”);
}
printf(“The matrix after transpose is\n”);
for(i=0;i<columns;i++)
{
for(j=0;j<rows;j++)
{
b[i][j]=a[j][i];
printf(“%d”,b[i][j]);
}
printf(“\n”);
}
printf(“The multiplication result is \n”);
for(j=0;j<rows;j++)
{
for(m=0;m<columns;m++)
{
sum=0;
for(k=0;k<rows;k++)
{
sum+=a[j][k]*b[k][m];
}
c[j][m]=sum;
}
}
for(j=0;j<rows;j++)
{
for(m=0;m<columns;m++)
{
printf(“%d\t”,c[j][m]);
}
printf(“\n”);
}
getch();
}

9.Program for String Operations using functions in C

#include<stdio.h>
#include<string.h>
void length1();//prototype
void equal();//prototype
void concatenate();//prototype
void lower1();//prototype
void upper1();//prototype
void reverse();//prototype
void indexs();//prototype
void substring1();//prototype
int main()
{
operations();//calling operation function
}
operations()
{
int choice,length;
char string[10],string1[10],string2[10],upper,lower,combine;
printf(“1. Calculate length of given string \n2. Check if two strings are equal \n3. Concatenate two given strings \n4. Lower case to upper case \n5. Upper case to Lower case\n6. Reversing a string\n7. Character at a given index\n8. substring when starting index and length of the substring is given\n”);
printf(“enter the choice (1-4):”);
scanf(“%d”,&choice);//getting input from user
switch(choice)
{
case 1:length1();//if 1 call length1 function
break;
case 2:equal();//if 2 call equal function
break;
case 3:concatenate();//if 3 call concatenate function
break;
case 4:lower1();//if 4 call lower1 function
break;
case 5:upper1();//if 5 call upper1 function
break;
case 6:reverse();//if 6 call reverse function
break;
case 7:indexs();//if 7 call indexs function
break;
case 8:substring1();//if 8 call substring function
break;
default:
printf(“Invalid Input”);
}
}
void length1()//length1 function
{
int length;
char string[10];
printf(“enter string: “);
scanf(“%s”,&string);
length=strlen(string);//calculating length
printf(“The length is: %d”,length);
getch();
}
void equal()//equal function
{
char string1[10],string2[10];
printf(“enter string1: “);
scanf(“%s”,&string1);
printf(“\nenter string2: “);
scanf(“%s”,&string2);
if(strcmp(string1,string2)==0)//comparing strings
{
printf(“Strings are Equal”);
}
else
{
printf(“Strings not Equal”);
}
getch();
}
void concatenate()//concatenate function
{
char string1[10],string2[10];
printf(“enter string1: “);
scanf(“%s”,&string1);
printf(“enter string2: “);
scanf(“%s”,&string2);
printf(“%s”,strcat(string1,string2));//concatenating strings
getch();
}
void lower1()//lower1 function
{
char string[10];
printf(“Enter String: “);
scanf(“%s”,&string);
printf(“The Upper Case is: %s”,strupr(string));//converting to lower case
getch();
}
void upper1()//upper1 function
{
char string[10];
printf(“Enter String: “);
scanf(“%s”,&string);
printf(“The Lower Case is: %s”,strlwr(string));//converting to upper case
getch();
}
void reverse()//reverse function
{
char string[10];
printf(“Enter String: “);
scanf(“%s”,&string);
printf(“The Reverse is: %s”,strrev(string));//reversing string
getch();
}
void indexs()//indexs function
{
int m,i,index;
printf(“enter size of string: “);
scanf(“%d”,&m);
char string[m];
printf(“Enter String: \n”);
scanf(“%s”,&string);
printf(“Enter Index value: “);
scanf(“%d”,&index);
for(i=0;i<=m;i++)
{
if(i==index)//checking if index value matches
{
printf(“The Index Value is: %d\n”,index);
printf(“The output is: %c”,string[i]);
}
}
getch();
}
void substring1()//substring function
{
int m,i,index,size,j,count=1,flag=0;
printf(“enter size of string: “);
scanf(“%d”,&m);
char string[m];
printf(“Enter String: \n”);
scanf(“%s”,&string);
printf(“Enter Starting Index value: “);
scanf(“%d”,&index);
printf(“Enter Size of Substring: “);
scanf(“%d”,&size);
printf(“output: “);
for(i=0;i<m;i++)
{
if(i==index)//checking if index value matches
{
for(j=index;j<m;j++)
{
printf(“%c”,string[j]);//displaying substring
count=count+1;//calculating count
if(count>size)//checking count greater than size
{
flag=1;
break;
}
}
}
if(flag==1)
{
break;
}
}
getch();
}

10.Program for printing the lines in C using functions

int main()
{
int a,b,c;
char d;
printf(“Enter the Character: “);
scanf(“%c”,&d);
printf(“Enter the number of times to print: “);
scanf(“%d”,&a);
printf(“Enter the spaces: “);
scanf(“%d”,&b);
printf(“Enter number of lines: “);
scanf(“%d”,&c);
print_lines(a,b,c,d);//passing values to the function
}
print_lines(e,f,g,h)//function print_line
{
int i,j,k,l;
for(i=0;i<g;i++)
{
for(j=0;j<e;j++)
{
printf(“%c”,h);//printing the character
for(k=0;k<f;k++)
{
printf(” “);//printing spaces
}
}
printf(“\n”);
}
getch();
}

11.Program for proper positive factors in C

void positive();//prototype
int main()
{
int number;
printf(“Enter the number: “);
scanf(“%d”,&number);
positive(number);//passing argument to positive function
}
positive(j)
{
int i,res,sum=0,a[30];
printf(“The Proper Positive Factors are: “);
for(i=1;i<j;i++)
{
res=j%i;
if(res==0)//checking for factors
{
printf(“%d,”,i);//printing factors
sum=sum+i;//calculating sum of factors
}
}
printf(“\nThe sum of proper positive factors is: %d”,sum);//displaying sum
if(sum>j)//checking sum is greater than original number or not
{
printf(“\nOutput:true”);//yes
}
else
{
printf(“\nOutput:False”);//no
}
getch();
}

12.Program for factorial in C using functions

#include<stdio.h>
int main()
{
int number;
printf(“Enter Number to get the factorial: “);
scanf(“%d”,&number);
int res=fact(number);
printf(“The Factorial is %d”,res);
getch();
}
int fact(j)
{
int result;
if(j<2)
{
return 1;//checking for less than 2
}
else
{
result=j*fact(j-1);//recursive function
}
return result;
}

13.Program for Fibonacci in C using functions

int result;
int main()
{
int res,i;
for(i=0;i<25;i++)
{
res=fib(i);//calling the function
printf(“%d:%d\n”,i,res);
}
getch();
return 0;
}
int fib(int a)
{

if(a==0)
{
return 0;
}
if(a==1)
{
return 1;
}
else
return fib(a-1)+fib(a-2);//returning the value
}

14.Program for Reading and Writing Binary File in C

#include<stdio.h>
#include<stdlib.h>
struct employee//creating structure
{
char ename[10];
char dob[10];
char phn[10];
int age;
int salary;
};
main()
{
struct employee b[5],c[5];//structure variable
int i;
//reading values
for(i=1;i<=5;i++)
{
printf(“\nEnter The Details of Employee%d\n”,i);
printf(“\nEnter Employee Name: “);
scanf(“%s”,&b[i].ename);
fflush(stdin);
printf(“Enter Date Of Birth(DD/MM/YY): “);
fflush(stdin);
scanf(“%s”,&b[i].dob);
printf(“Enter Phone Number: “);
fflush(stdin);
scanf(“%s”,&b[i].phn);
printf(“Enter Age: “);
fflush(stdin);
scanf(“%d”,&b[i].age);
printf(“Enter Salary: “);
fflush(stdin);
scanf(“%d”,&b[i].salary);
}
FILE *fp;//file pointer
fp=fopen(“data.bin”,”wb”);//writing mode for binary file
for(i=0;i<5;i++)
{
fwrite(&b[i],sizeof(struct employee),i,fp);//writing operation using fwrite
}
printf(“Writing Successful\n”);
fclose(fp);

printf(“\n———————————-\n”);
fp=fopen(“data.bin”,”rb”);//displaying data from binary file
for(i=1;i<=5;i++)
{
fread(&c[i],sizeof(struct employee),i,fp);
printf(“\nName: %s”,c[i].ename);
printf(“\nDOB: %s”,c[i].dob);
printf(“\nPhone: %s”,c[i].phn);
printf(“\nAge:%d”,c[i].age);
printf(“\nSalary: %d\n”,c[i].salary);
}
fclose(fp);
getch();
}

15.Program for copying data from binary file to text file in C

#include<stdio.h>
#include<stdlib.h>
struct employee//creating structure
{
char ename[10];
char dob[10];
char phn[10];
int age;
int salary;
};
main()
{
struct employee b[5],c,ch;//structure variable
int i;
//reading values
for(i=1;i<=5;i++)
{
printf(“\nEnter The Details of Employee%d\n”,i);
printf(“\nEnter Employee Name: “);
scanf(“%s”,&b[i].ename);
fflush(stdin);
printf(“Enter Date Of Birth(DD/MM/YY): “);
fflush(stdin);
scanf(“%s”,&b[i].dob);
printf(“Enter Phone Number: “);
fflush(stdin);
scanf(“%s”,&b[i].phn);
printf(“Enter Age: “);
fflush(stdin);
scanf(“%d”,&b[i].age);
printf(“Enter Salary: “);
fflush(stdin);
scanf(“%d”,&b[i].salary);
}
FILE *fp,*fq;//file pointers
fp=fopen(“data2.bin”,”wb+”);//writing mode for binary file
for(i=0;i<5;i++)
{
fwrite(&b[i],sizeof(struct employee),i,fp);//writing operation
}
printf(“Successful creation of binary file\n”);
fclose(fp);

fp=fopen(“data2.bin”,”rb”);
fq = fopen( “copy.txt”, “w” );//copying to text file
for(i=1;i<=5;i++)
{
fread(&c,sizeof(struct employee),i,fp);
fwrite(&b[i],sizeof(struct employee),i,fq);//copying file
}
fclose( fp );
fclose( fq );
printf(“file read and write operations to text file successfully completed…\n”);

printf(“The Contents in text file are:”);
fq=fopen(“copy.txt”,”r”);//displaying contents in text file
for(i=1;i<=5;i++)
{
fread(&c,sizeof(struct employee),i,fp);
printf(“\nName: %s”,c.ename);
printf(“\nDOB: %s”,c.dob);
printf(“\nPhone: %s”,c.phn);
printf(“\nAge:%d”,c.age);
printf(“\nSalary: %d\n”,c.salary);
}
getch();
}

16.Source code for reading contents in a directory using C

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
/* limits.h defines “PATH_MAX”. */
#include <limits.h>
void listdir (const char *path){
DIR *pdir = NULL;
pdir = opendir (path);
struct dirent *pent = NULL;
if (pdir == NULL)
{
return; // exit the function
} // end if
while (pent = readdir (pdir))
{
if (pent == NULL)
{
return;
}
printf (“%s\n”, pent->d_name);
if (strcmp (pent->d_name, “..”) != 0 &&
strcmp (pent->d_name, “.”) != 0)
{
int path_length;
char newpath[PATH_MAX];
path_length = snprintf (newpath, PATH_MAX, “%s/%s”, path, pent->d_name);
printf (“%s\n”, newpath);
if (path_length >= PATH_MAX)
{

fprintf (stderr, “Path length has got too long.\n”);
exit (EXIT_FAILURE);
}
listdir(newpath);
}
}
closedir (pdir);
}
int main(int argc, char *argv[])
{
listdir (“C:\\Documents and Settings\\admin\\Desktop\\Sravan JNTUK02”);
system(“PAUSE”);
return 0;
}


2 thoughts on “C Programs

    Mvk Chaitanya said:
    September 13, 2012 at 5:21 pm

    mama add some more programs!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s