Saturday, May 21, 2011

C Program to implement telephone billing System

/*Code to accept 10 telephone customers’ data into a structure
object.
The fields are:
Customer name
Telephone number
Units to be billed
Calculate Bill Amount, which is Rent of Rs. 250 plus Re 0.80 for each
unit. Prepare the Bill and print it.*/


int main()
{
int n,i;
struct bill
{
char name[20];
int num;
int units;
}a[50];


printf("\nEnter the no of customers");
scanf("%d",&n);
for(i=0;i {
printf("\nEnter the customer's name");
scanf("%s",&a[i].name);
printf("\nEnter the customer's number");
scanf("%d",&a[i].num);
printf("\nEnter the no of units");
scanf("%d",&a[i].units);
}
for(i=0;i {
printf("\nname %s",a[i].name);
printf("\nnumber %d",a[i].num);
printf("\nBill is Rs %f",((a[i].units)*0.8)+250);



}
system("pause");
return 0;
}

C program to explain use of Structures

/*Code a program to do the following with a structure:
a. Declare the structure with the following members:
Name char 20
Age int
Gender char 1
Marks float
b. Initialize the members with your values.
c. Print the memory space occupied by the structure object.
d. Print the formatted structure data.*/

int main()
{
int n,i;
struct marks
{
char name[20];
int age;
char gen;
float marks;
}a[50];


printf("\nEnter the no of entries");
scanf("%d",&n);
for(i=0;i {
printf("\nEnter the name");
scanf("%s",&a[i].name);
printf("\nEnter the age");
scanf("%d",&a[i].age);
printf("\nEnter the gender");
scanf("%c",&a[i].gen);
printf("\nEnter the no of marks");
scanf("%f",&a[i].marks);
}
for(i=0;i {
printf("size of object a%d is %d",i,sizeof(a[i]));
}
for(i=0;i {
printf("\nname %s",a[i].name);
printf("\nage %d",a[i].age);
printf("\n%c",a[i].gen);
printf("\n%f",a[i].marks);
}
system("pause");
return 0;
}

C Program to find sum of digits in number using recursion

int sumd(int);

int sumd(int x)
{
if(x==0)
return 0;
else
return (x%10)+sumd(x/10);
}

int main()
{
int a;
printf("Enter a number :");
scanf("%d",&a);
int b=sumd(a);
printf("\nSum of digits= %d",b);
getch();
}

C program with user defined functions

/*Code a program to accept an integer value. Pass this value to
two functions - one to compute square value, and the other to
compute cube value. Print the results from the functions.*/


#include
#include
int square(int);
int cube(int);

int main()
{
int a;
printf("Enter a number :");
scanf("%d",&a);
int b=square(a);
printf("\nSquare= %d",b);
b=cube(a);
printf("\nCube= %d",b);
getch();
}

int square(int x)
{
return x*x;
}

int cube(int x)
{
return x*x*x;
}

C program to reverse digits of number using function


Simple C Program reverse digits of number using function

int reversedigit(int);

int main()
{
int a;
printf("Enter a number :");
scanf("%d",&a);
int b=reversedigit(a);
printf("\nreverse= %d",b);
getch();
}
reversedigit(int x)
{
int y=0;
while(x)
{
y=y*10+x%10;
x=x/10;
}
return y;
}

C program to explain use of function

#include
#include
void print(void);
int main()
{
int i;
for(i=0;i<5;i++)
{
print();
printf("\n");
}

getch();
}
void print()
{
printf("Welcome to c");
}

C Program to compute transpose of matrix

#include
#include
int main()
{
int a[20][20],b[20][20],i,j,m,n,k=0;
printf("\t\tenter rows and columns of matrix -A \n");
scanf("%d%d",&m,&n);
printf("\n\nInput Matrix A\n");
for(i=0;i {
for(j=0;j {
printf("\nEnter element ");
scanf("%d",&a[i][j]);
}
}
printf("\n matrix A is:\n ");
for(i=0;i {
printf("\n\n");
for(j=0;j printf("\t%d",a[i][j]);
}
for(i=0;i { printf("\n\n");
for(j=0;j b[j][i]=a[i][j];
}
printf("\n\ntranspose of matrix is :\n\n ");
for(i=0;i {
printf("\n\n");
for(j=0;j printf("\t%d",b[i][j]);
}
getch();
}

C program for Addition of matrix

int main()
{
int a[20][20],b[20][20],t[20][20],i,j,k,l,m,n,o,p,c,d,e,f;


printf("\nEnter the no of rows in the array 1");
scanf("%d",&c);
printf("\nEnter the no of columns in the array 1");
scanf("%d",&d);

printf("Enter the matrix");
for(i=0;i {
for(j=0;j scanf("%d",&a[i][j]);
}

printf("\nEnter the no of rows in the array 2");
scanf("%d",&e);
printf("\nEnter the no of columns in the array 2");
scanf("%d",&f);
printf("Enter the matrix");
for(k=0;k {
for(l=0;l scanf("%d",&b[k][l]);
}
if(c==e||d==f)
{
for(m=0;m {
for(n=0;n t[m][n]=a[m][n]+b[m][n];
}
}
else printf("The summation is not possible because no of rows and columns of the two arrays is not same");
printf("The matrix after addition is");
for(o=0;o { printf("\n");
for(p=0;p printf("%d ",t[o][p]);
}
system("pause");
return 0;
}

C Program to produce a substring from given string

/*Write a program to copy m consecutive characters from String S1
beginning at position n into another string S2.*/

int main()
{
int m,n,i;
char s2[40],s1[40];
printf("Enter the string 1");
scanf("%s",&s1);
printf("\nHow many characters you want to copy");
scanf("%d",&n);
printf("From which position you want it to be copied");
scanf("%d",&m);
for(i=m-1;i {

s2[i-m-1]=s1[i];
}

printf("\nThe string s2 is %d",puts(s2));
system("pause");
return 0;
}

C Program to perform operation on Strings (Array of Character)

Read a line of text into a character array and perform the following
operation:
a. Count the number of Upper case , Lower case, digits and
special characters.
b. Convert Upper case to Lower case and vice versa without
using string library functions.
c. Count the number of words, lines and characters in a given
text.


main()
{
char a[40];
int i=0,lc=0,uc=0,w=0,le=0,d=0,s=0;
printf("Enter string");
gets(a);

while(a[i]!='\0')
{
if(a[i]>='a' && a[i]<='z')
{
lc++;
a[i]=a[i]-32;
le++;
}
else if (a[i]>='A' && a[i]<='Z')
{
uc++;
a[i]=a[i]+32;
le++;
}
else if (a[i]==' ')
w++;
else if(a[i]=='.')
s++;
else if (a[i]>='0' && a[i]<='9' )
d++;

i++;
}
printf("%s\n",a);
printf("Upper case %d\n",uc);
printf("Lower case %d\n",lc);
printf("Digits %d\n",d);
printf("Sentences/lines %d\n",s);
printf("Number of words %d\n",++w);
getch();
}

C program to implement Binary Search

#include
#include
int main()
{
int a[50],item,n,beg,last,mid,i;
printf("\n\n\t\t enter desired array size ");
scanf(" %d",&n);
printf("\n\n\t\t enter array elements \n");
for(i=0;i< n ;i++)
{
scanf("%d",&a[i]);
}
printf("\n\n\t\t enter elements to be searched for");
scanf("%d",&item);
beg=0;
last=n-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(item==a[mid])
{
printf("\n\n\t\t element found at position %d",mid+1);
}
else if(item>a[mid])
beg=mid+1;
else
last=mid-1;
printf("\n\n\t\t element not found");
}
getch();
}

C program to implement Linear Search

//program for linear search
#include
#include
int main()
{
int a[10],item,n,i;
printf("\n\n\t how many elements do you want to enter in the array:");
scanf("%d",&n);
printf("\n\n\t enter the elements of the array:");
for(int i=0;iscanf("%d",&a[i]);
printf("\n\n\t enter the element to be searched:");
scanf("%d",&item);
for(i=0;i{
if(a[i]==item)
printf("\n\n\t element is found at position %d",i+1);
else
printf("\n\n\t the elememt is not present in the array:");
}
getch();
}

C program to remove duplicates from an array

/*Read elements into an array. Write a Program to find duplicate
elements in the array and display the resultant array by removing
the duplicates.*/


int main()
{
int a[100],i,j,k,b,t=0;


printf("\nEnter the size of 1 dimensional array");
scanf("%d",&b);
printf("\nEnter the array");
for(i=0;i scanf("%d",&a[i]);
for(i=0;i {
for(j=i+1;j {
if(a[i]==a[j])
{
t++;
for(k=j;k a[k]=a[k+1];

}
}
}
for(i=0;i<=b-t;i++)
printf("\n%d",a[i]);
system("pause");
return 0;
}

C program To reverse the contents of an Array

int main()
{
int a[100],i,j,k,b,t;


printf("\nEnter the size of 1 dimensional array");
scanf("%d",&b);
printf("\nEnter the array");
for(i=0;i scanf("%d",&a[i]);
for(j=0;j<=b/2;j++)
{
t=a[j];
a[j]=a[b-j-1];
a[b-j-1]=t;
}

for(k=0;kprintf("\n%d",a[k]);
system("pause");
return 0;
}

C program to understand Single dimension array

Read values into an one dimensional array and compute the sum of
their squares


int main()
{
int a[100],i,j,b,sum=0;


printf("\nEnter the size of 1 dimensional array");
scanf("%d",&b);
printf("\nEnter the array");
for(i=0;i scanf("%d",&a[i]);
printf("\nThe sum of there squares is");
for(j=0;j {
sum=sum+pow(a[j],2);
}
printf("%d",sum);
system("pause");
return 0;
}

Saturday, May 14, 2011

c program for file copying

#include
main()
{
FILE *f1,*f2;//FILE is a defined data type under stdio.h
char c;
f1=fopen("Input","r");
f2=fopen("Inputcopy","w"); /*Open the file Input, Creates or opens a file*/
while((c=getc(f1))!=EOF) /*get a character from key board until cntrl+z is pressed*/
{
putc(c,f2);
printf("Copying %c to file\n",c);/*write a character to input*/
}
fclose(f1);
fclose(f2); /*close the file input*/
system("pause");
}