Saturday, July 26, 2008

C PROGRAMS

C PROGRAMS

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

c program for to check Armstrong number

void main()
{
int num,r,sum=0,temp;
clrscr();
printf(”\nEnter a number:-”);
scanf(”%d”,&num);
temp=num;
while(num!=0)
{
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf(”\nThe number %d is an armstrong number”,temp);
else
printf(”\nThe number %d is not an armstrong number”,temp);
getch();
}

c program to reverse any number

void main()
{
int num,out;
clrscr();
scanf(”%d”,&num);
out=rev(num);
printf(”%d”,out);
getch();
}
int rev(int num)
{
static sum,r;
if(num)
{
r=num%10;
sum=sum*10+r;
rev(num/10);
}
else return 0;
return sum;
}

c program for to check prime number

void main()
{
int num,i,count=0;
clrscr();
printf(”\nEnter a number:”);
scanf(”%d”,&num);
for(i=1;i<=num;i++)
{
if(num%i==0)
count++;
}
if(count==2)
printf(”%d is a prime number”,num);
else
printf(”%d is not a prime number”,num);
getch();
}


c program to find sum of the digit of number

void main()
{
int num,sum=0,r;
clrscr();
printf(”\nEnter a number:”);
scanf(”%d”,&num);
while(num)
{
r=num%10;
num=num/10;
sum=sum+r;
}
printf(”sum=%d”,sum);
getch();
}

Write a c program to check the given number is palindrome number or not

void main()
{
int num,r,sum=0,temp;
clrscr();
printf(”\nEnter a number:”);
scanf(”%d”,&num);
temp=num;
while(num)
{
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf(”\n%d is a palindrome”,temp);
else
printf(”\n%d is not a palindrome”,temp);
getch();
}

Write c program to find g.c.d of two number

void main()
{
int n1,n2;
clrscr();
printf(”\nEnter two numbers:”);
scanf(”%d %d”,&n1,&n2);
while(n1!=n2)
{
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
printf(”\nGCD=%d”,n1);
getch();
}


WRITE A C PROGRAM TO FIND GENERIC ROOT OF A NUMBER.

void main()
{
long int num,sum,r;
clrscr();
printf(”\nEnter a number:-”);
scanf(”%ld”,&num);
while(num>10)
{
sum=0;
while(num)
{
r=num%10;
num=num/10;
sum+=r;
}
if(sum>10)
num=sum;
else
break;
}
printf(”\nSum of the digits in single digit is: %ld”,sum);
getch();
}


WRITE A C PROGRAM FOR INSERT AN ELMENT IN AN ARRAY AT DESIRED POSITION

void main()
{
int a[50],size,num,i,pos,temp;
clrscr();
printf(”\nEnter size of the array: “);
scanf(”%d”,&size);
printf(”\nEnter %d elements in to the array: “,size);
for(i=0;iscanf(”%d”,&a[i]);
printf(”\nEnter position and number to insert: “);
scanf(”%d %d”,&pos,&num);
i=0;
while(i!=pos-1)
i++;
temp=size++;
while(i{
a[temp]=a[temp-1];
temp–;
}
a[i]=num;
for(i=0;iprintf(” %d”,a[i]);
getch();
}

WRITE A C PROGRAM FOR GREATEST AMONG 3 NUMBERS USING CONDITIONAL OPERATOR

void main()
{
int a,b,c,big;
clrscr();
printf(”\nEnter 3 numbers:”);
scanf(”%d %d %d”,&a,&b,&c);
big=(a>b&&a>c?a:b>c?b:c);
printf(”\nThe biggest number is:%d”,big);
getch();
}

c PROGRAM FOR DELETE ELEMENT FROM AN ARRAY AT DESIRED POSITION

void main()
{
int a[50],i,pos,size;
clrscr();
printf(”\nEnter size of the array: “);
scanf(”%d”,&size);
printf(”\nEnter %d elements in to the array: “,size);
for(i=0;i
scanf(”%d”,&a[i]);
printf(”\nEnter position where to delete: “);
scanf(”%d”,&pos);
i=0;
while(i!=pos-1)
i++;
while(i<10)
{
a[i]=a[i+1];
i++;
}
size–;
for(i=0;i
printf(” %d”,a[i]);
getch();
}

C PROGRAM FOR QUICK SORT

void main()
{
int x[20],size,i;
clrscr();
printf(”\nEnter size of the array :”);
scanf(”%d”,&size);
printf(”\nEnter %d elements :”,size);
for(i=0;ix[pivot])
j–;
if(i {
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}

C PROGRAM FOR CONVERSION FROM DECIMAL TO OCTAL

void main()
{
int i=0,j=0,rem=0,a[10],b[10];
long int num;
clrscr();
printf(”\nEnter a number :”);
scanf(”%ld”,&num);
while(num)
{
if(num< 8)
{
a[j++]=num;
break;
}
else
{
a[j++]=num%8;
num=num/8;
}
}
for(i=j-1;i>=0;i–)
b[rem++]=a[i];
printf(”\nOctal equivalent :”);
for(j=0;j
printf(”%d”,b[j]);
getch();
}

C PROGRAM FOR REVERSE A NUMBER USING RECURSION

void main()
{
int num,rev;
clrscr();
printf(”\nEnter a number :”);
scanf(”%d”,&num);
rev=reverse(num);
printf(”\nAfter reverse the no is :%d”,rev);
getch();
}
int sum=0,r;
reverse(int num)
{
if(num)
{
r=num%10;
sum=sum*10+r;
reverse(num/10);
}
else
return sum;
}

C PROGRAM FOR REMOVE DUPLICATE ELEMENTS IN AN ARRAY

void main()
{
int arr[50];
int *p;
int i,j,k,size,n;
clrscr();
printf(”\nEnter size of the array: “);
scanf(”%d”,&n);
printf(”\nEnter %d elements into the array: “,n);
for(i=0;i
scanf(”%d”,&arr[i]);
size=n;
p=arr;
for(i=0;i
{
for(j=0;j
{
if(i==j)
{
continue;
}
else if(*(p+i)==*(p+j))
{
k=j;
size–;
while(k < size)
{
*(p+k)=*(p+k+1);
k++;
}
j=0;
}
}
}
printf(”\nThe array after removing duplicates is: “);
for(i=0;i < size;i++)
{
printf(” %d”,arr[i]);
}
getch();
}

C PROGRAM FOR BINARY SEARCH THROUGH RECURSSION

void main()
{
int a[10],i,n,m,c,l,u;
clrscr();
printf(”Enter the size of an array->”);
scanf(”%d”,&n);
printf(”\nEnter the elements of the array->”);
for(i=0;i
{
scanf(”%d”,&a[i]);
}
printf(”\nThe elements of an array are->”);
for(i=0;i
{
printf(” %d”,a[i]);
}
printf(”\nEnter the number to be search->”);
scanf(”%d”,&m);
l=0,u=n-1;
c=binary(a,n,m,l,u);
if(c==0)
printf(”\nThe number is not in the list”);
else
printf(”\nThe number is found”);
getch();
}
int binary(int a[],int n,int m,int l,int u)
{
int mid,c=0;
if(l<=u)
{
mid=(l+u)/2;
if(m==a[mid])
{
c=1;
}
else
if(m
{
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
}
else
return c;
}

C PROGRAM FOR LINEAR SEARCH

void main()
{
int a[10],i,n,m,c=0;
clrscr();
printf(”Enter the size of an array->”);
scanf(”%d”,&n);
printf(”\nEnter the elements of the array->”);
for(i=0;i
{
scanf(”%d”,&a[i]);
}
printf(”\nThe elements of an array are->”);
for(i=0;i
{
printf(” %d”,a[i]);
}
printf(”\nEnter the number to be search->”);
scanf(”%d”,&m);
for(i=0;i
{
if(a[i]==m)
{
c=1;
break;
}
}
if(c==0)
printf(”\nThe number is not in the list”);
else
printf(”\nThe number is found”);
getch();
}

ACROMATIC STRING(PRINTING INITIALS)

void main()
{
int i=0,j=0;
char *str1,*str2,*str3;
clrscr();
puts(”Enter first string”);
gets(str1);
puts(”Enter second string”);
gets(str2);
printf(”Before concatenation the strings are\n”);
puts(str1);
puts(str2);
while(*str1)
{
str3[i++]=*str1++;
}
while(*str2)
{
str3[i++]=*str2++;
}
str3[i]=”;
printf(”After concatenation the strings are\n”);
puts(str3);
getch();
}

C PROGRAM FOR CONCATENATION OF TWO STRINGS USING POINTER

void main()
{
int i=0,j=0;
char *str1,*str2,*str3;
clrscr();
puts(”Enter first string”);
gets(str1);
puts(”Enter second string”);
gets(str2);
printf(”Before concatenation the strings are\n”);
puts(str1);
puts(str2);
while(*str1)
{
str3[i++]=*str1++;
}
while(*str2)
{
str3[i++]=*str2++;
}
str3[i]=”;
printf(”After concatenation the strings are\n”);
puts(str3);
getch();
}

C PROGRAM FOR SWAPPING OF STRINGS

void main()
{
int i=0,j=0,k=0;
char str1[20],str2[20],temp[20];
clrscr();
puts(”Enter first string”);
gets(str1);
puts(”Enter second string”);
gets(str2);
printf(”Before swaping the strings are\n”);
puts(str1);
puts(str2);
while(str1[i]!=”)
{
temp[j++]=str1[i++];
}
temp[j]=”;
i=0,j=0;
while(str2[i]!=”)
{
str1[j++]=str2[i++];
}
str1[j]=”;
i=0,j=0;
while(temp[i]!=”)
{
str2[j++]=temp[i++];
}
str2[j]=”;
printf(”After swaping the strings are\n”);
puts(str1);
puts(str2);
getch();
}

C PROGRAM FOR FIND POWER OF A NUMBER USING RECURSION

void main()
{
int pow,num;
long int res;
long int power(int,int);
clrscr();
printf(”\nEnter a number: “);
scanf(”%d”,&num);
printf(”\nEnter power: “);
scanf(”%d”,&pow);
res=power(num,pow);
printf(”\n%d to the power %d is: %ld”,num,pow,res);
getch();
}
int i=1;
long int sum=1;
long int power(int num,int pow)
{
if(i<=pow)
{
sum=sum*num;
power(num,pow-1);
}
else
return sum;
}

C PROGRAM FOR FIND POWER OF A NUMBER

void main()
{
int pow,num,i=1;
long int sum=1;
clrscr();
printf(”\nEnter a number: “);
scanf(”%d”,&num);
printf(”\nEnter power: “);
scanf(”%d”,&pow);
while(i<=pow)
{
sum=sum*num;
i++;
}
printf(”\n%d to the power %d is: %ld”,num,pow,sum);
getch();
}

C PROGRAM TO FIND SUM OF DIGITS OF A NUMBER USING RECURSION

void main()
{
int num,x;
clrscr();
printf(”\nEnter a number: “);
scanf(”%d”,&num);
x=findsum(num);
printf(”Sum of the digits of %d is: %d”,num,x);
getch();
}
int r,s;
int findsum(int n)
{
if(n)
{
r=n%10;
s=s+r;
findsum(n/10);
}
else
return s;
}

C PROGRAM TO FIND GCD OF A NUMBER USING RECURSION

void main()
{
int n1,n2,gcd;
clrscr();
printf(”\nEnter two numbers: “);
scanf(”%d %d”,&n1,&n2);
gcd=findgcd(n1,n2);
printf(”\nGCD of %d and %d is: %d”,n1,n2,gcd);
getch();
}
int findgcd(int x,int y)
{
while(x!=y)
{
if(x>y)
return findgcd(x-y,y);
else
return findgcd(x,y-x);
}
return x;
}

C PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION

void main()
{
int num,f;
clrscr();
printf(”\nEnter a number: “);
scanf(”%d”,&num);
f=fact(num);
printf(”\nFactorial of %d is: %d”,num,f);
getch();
}
int fact(int n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}

C PROGRAM FOR DISPLAY SOURCE CODE AS OUTPUT

#include”stdio.h”
void main()
{
FILE *p;
char ch;
clrscr();
p=fopen(”raja.c”,”r”);
while((ch=getc(p))!=-1)
putchar(ch);
fclose(p);
getch();
}

C PROGRAM FOR INSERTION SORT

void main()
{
int i,j,s,temp,a[20];
clrscr();
printf(”\nEnter size of the array: “);
scanf(”%d”,&s);
printf(”\nEnter %d elements in to the array:”,s);
for(i=0;i
scanf(”%d”,&a[i]);
for(i=1;i
{
temp=a[i];
j=i-1;
while((temp=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf(”\nAfter sorting the elements are: “);
for(i=0;i
printf(” %d”,a[i]);
getch();
}

C PROGRAM FOR INSERTION SORT

void main()
{
int s,i,j,temp,a[20];
clrscr();
printf(”\nEnter size of the array :”);
scanf(”%d”,&s);
printf(”\nEnter %d elements in to the array:”);
for(i=0;i
scanf(”%d”,&a[i]);
for(i=0;i
{
for(j=i+1;j
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf(”\nThe array after sorting is: “);
for(i=0;i
printf(” %d”,a[i]);
getch();
}

C PROGRAM FOR SELECTION SORT

void main()
{
int s,i,j,temp,a[20];
clrscr();
printf(”\nEnter size of the array :”);
scanf(”%d”,&s);
printf(”\nEnter %d elements in to the array:”);
for(i=0;i
scanf(”%d”,&a[i]);
for(i=0;i
{
for(j=i+1;j
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf(”\nThe array after sorting is: “);
for(i=0;i
printf(” %d”,a[i]);
getch();
}

C PROGRAM FOR BUBBLE SORT

void main()
{
int s,temp,i,j,a[20];
clrscr();
printf(”\nEnter size of the array: “);
scanf(”%d”,&s);
printf(”\nEnter %d elements in to the array:”,s);
for(i=0;i
scanf(”%d”,&a[i]);
for(i=0;i
{
for(j=0;j
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf(”\nThe array after sorting is: “);
for(i=0;i
printf(” %d”,a[i]);
getch();
}

C PROGRAM FOR SORTING OF STRING

void main()
{
int i,j,n;
char str[20][20],temp[20];
clrscr();
puts(”Enter the no. of string to be sorted”);
scanf(”%d”,&n);
for(i=0;i<=n;i++)
gets(str[i]);
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf(”The sorted string\n”);
for(i=0;i<=n;i++)
puts(str[i]);
getch();
}

C PROGRAM FOR COUNTING DIFFERENT CHARACTERS IN A STRING

main()
{
int a[26],A[26],i,c=0;
char str[100];
clrscr();
puts(”Enter a string->”);
gets(str);
for(i=0;i<26;i++)
{
a[i]=0;
A[i]=0;
}
for(i=0;str[i]!=”;i++)
{
c=str[i];
if(c<97)
{
c=c-65;
A[c]++;
}
else
{
c=c-97;
a[c]++;
}
}
for(i=0;i<26;i++)
{
if(a[i]!=0)
printf(”\n%c occurs %d times”,i+97,a[i]);
}
for(i=0;i<26;i++)
{
if(A[i]!=0)
printf(”\n%c occurs %d times”,i+97,A[i]);
}
getch();
}

C PROGRAM FOR CONVERSION OF FAREHNITE TO CENTIGRADE

void main()
{
float c,f;
clrscr();
printf(”Enter temp. in farehnite”);
scanf(”%f”,&f);
c=(5*(f-32))/9;//Formula for conversion
printf(”The temp. in centigrade is->%f”,c);
getch();
}

Write a c program to find the perfect number

void main(){

int n,i=1,sum=0;

clrscr();

printf(”\nEnter a number:-”);

scanf(”%d”,&n);

while(i

{

if(n%i==0)

sum=sum+i;

i++;

}

if(sum==n)

printf(”\nThe no %d is a perfect number”,i);

else

printf(”\nThe no %d is not a perfect number”,i);

getch();

}

WRITE A C PROGRAM TO FIND TRASPOSE OF A MATRIX

void main()
{
int a[10][10],b[10][10],i,j,k=0,m,n;
clrscr();
printf(”\nEnter the row and column of matrix”);
scanf(”%d %d”,&m,&n);
printf(”\nEnter the First matrix->”);
for(i=0;i
for(j=0;j
scanf(”%d”,&a[i][j]);
printf(”\nThe matrix is\n”);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,a[i][j]);
}
}
for(i=0;i
for(j=0;j
b[i][j]=0;
for(i=0;i
{
for(j=0;j
{
b[i][j]=a[j][i];
printf(”\n%d”,b[i][j]);
}
}
printf(”\n\nTraspose of a matrix is -> “);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,b[i][j]);
}
}
getch();
}

Write a c program to find SUM OF DIAGONAL ELEMENTS OF A MATRIX

void main()
{
int a[10][10],i,j,sum=0,m,n;
clrscr();
printf(”\nEnter the row and column of matrix”);
scanf(”%d %d”,&m,&n);
printf(”\nEnter the First matrix->”);
for(i=0;i < m;i++)
for(j=0;j < n;j++)
scanf(”%d”,&a[i][j]);
printf(”\nThe matrix is\n”);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,a[i][j]);
}
}
for(i=0;i
{
for(j=0;j
{
if(i==j)
sum=sum+a[i][j];
}
}
printf(”\n\nSum of the diagonal elements of a matrix is -> “);
printf(”%d”,sum);
getch();
}

WRITE A C PROGRAM FOR MULTIPLICATION OF TWO MATRICES

void main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
clrscr();
printf(”\nEnter the row and column of first matrix”);
scanf(”%d %d”,&m,&n);
printf(”\nEnter the row and column of second matrix”);
scanf(”%d %d”,&o,&p);
if(n!=o)
{
printf(”Matrix mutiplication is not possible”);
printf(”\nColumn of first matrix must be same as row of second matrix”);
}
else
{
printf(”\nEnter the First matrix->”);
for(i=0;i
for(j=0;j
scanf(”%d”,&a[i][j]);
printf(”\nEnter the Second matrix->”);
for(i=0;i
for(j=0;j
scanf(”%d”,&b[i][j]);
printf(”\nThe First matrix is\n”);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,a[i][j]);
}
}
printf(”\nThe Second matrix is\n”);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,b[i][j]);
} }
for(i=0;i
for(j=0;j
c[i][j]=0;
for(i=0;i
{
for(j=0;j
{
sum=0;
for(k=0;k
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf(”\nThe multiplication of two matrix is\n”);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,c[i][j]);
}
}
getch();
}

WRITE A C PROGRAM TO FIND SECOND LARGEST NUMBER IN AN UNSORTED ARRAY

main()
{
int un[10], i, big1, big2;
printf(”Enter array elements: “);
for ( i = 0; i < 10; ++i )
scanf(”%d”, &un[i]);
big1 = un[0];
for ( i = 1; i < 10; ++i )
{
if ( big1 < un[i] )
big1 = un[i];
if ( big1 != un[0] )
big2 = un[0];
else
big2 = un[1];
}
for ( i = 1; i < 10; ++i )
{
if ( big1 != un[i] && big2 < un[i] )
big2 = un[i];
}
printf(”Second largest: %d\n”, big2);
return 0;
}

WRITE C PROGRAM TO FIND SUM OF THE SERIES 1+2+3+———+n

void main()
{
int r;
clrscr();
printf(”\nEnter the number range: “);
scanf(”%d”,&r);
printf(”\nSum of the series is: %d”,(r*(r+1))/2);
getch();
}

WRITE A C PROGRAM TO FIND LARGEST NUMBER IN AN ARRAY

void main()
{
int a[50],size,i,big;
clrscr();
printf(”\nEnter the size of the array: “);
scanf(”%d”,&size);
printf(”\nEnter %d elements in to the array: ” ,size);
for(i=0;i
scanf(”%d”,&a[i]);
big=a[0];
for(i=1;i
{
if(big
big=a[i];
}
printf(”\nBiggest: %d”,big);
getch();
}

WRITE C PROGRAM TO FIND ADDITION & SUBTRACTION OF TWO COMPLEX NUMBERS

void main()
{
int a,b,c,d,x,y;
clrscr();
printf(”\nEnter the first complex number:”);
scanf(”%d%d”,&a,&b);
printf(”\nEnter the second complex number:”);
scanf(”%d%d”,&c,&d);
if(b<0)
printf(”%d-i\n”,a-b);
else
printf(”d+i\n”,a+b);
if(d<0)
printf(”d-i\n”,c-d);
else
printf(”%d+i\n”,c+d);
printf(”\nADDITION “);
x=a+c;
y=b+d;
if(y>0)
printf(”%d-i%d”,x,-y);
else
printf(”%d+i%d”,x,+y);
printf(”\n\nSUBTRACTION “);
x=a-c;
y=b-d;
if(y<0)
printf(”%d-i%d”,x,-y);
else
printf(”%d+i%d”,x,+y);
getch();
}

WRITE A C PROGRAM TO FIND COPY DATA FROM ONE FILE TO ANOTHER FILE

#include”stdio.h”
void main()
{
FILE *p,*q;
char file1[20],file2[20];
char ch;
clrscr();
printf(”\nEnter the source file name to be copied:”);
gets(file1);
p=fopen(file1,”r”);
if(p==NULL)
{
printf(”cannot open %s”,file1);
exit(0);
}
printf(”\nEnter the destination file name:”);
gets(file2);
q=fopen(file2,”w”);
if(q==NULL)
{
printf(”cannot open %s”,file2);
exit(0);
}
while((ch=getc(p))!=EOF)
putc(ch,q);
printf(”\nCOMPLETED”);
fclose(p);
fclose(q);
getch();
}

WRITE A C PROGRAM FOR STRING PALINDROME

#include”string.h”
void main()
{
char *str,*rev;
int i,j;
clrscr();
printf(”\nEnter a string:”);
scanf(”%s”,str);
for(i=strlen(str)-1,j=0;i>=0;i–,j++)
rev[j]=str[i];
rev[j]=”;
if(strcmp(rev,str))
printf(”\nThe string is not a palindrome”);
else
printf(”\nThe string is a palindrome”);
getch();
}

WRITE A C PROGRAM TO DELETE THE VOWELS FROM A STRING

void main()
{
char str[20],s[20];
int i,j=0;
clrscr();
printf(”Enter any string->”);
scanf(”%s”,str);
printf(”The string is->%s”,str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]==’a’str[i]==’e’str[i]==’i’str[i]==’o’str[i]==’u')
str[i]=’ ‘;
else
s[j++]=str[i];
}
s[j]=”;
printf(”\nThe string without vowel is->%s”,s);
getch();
}

WRITE A C PROGRAM TO CONVERSION FROM LOWER CASE TO UPPER CASE

void main()
{
char str[20];
int i;
clrscr();
printf(”Enter any string->”);
scanf(”%s”,str);
printf(”The string is->%s”,str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf(”\nThe string in lowercase is->%s”,str);
getch();
}

WRITE A C PROGRAM FOR ADDITION OF TWO MATRICES

void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf(”Enter the First matrix->”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(”%d”,&a[i][j]);
printf(”\nEnter the Second matrix->”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(”%d”,&b[i][j]);
printf(”\nThe First matrix is\n”);
for(i=0;i<3;i++)
{
printf(”\n”);
for(j=0;j<3;j++)
printf(”%d\t”,a[i][j]);
}
printf(”\nThe Second matrix is\n”);
for(i=0;i<3;i++)
{
printf(”\n”);
for(j=0;j<3;j++)
printf(”%d\t”,b[i][j]);
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf(”\nThe Addition of two matrix is\n”);
for(i=0;i<3;i++)
{
printf(”\n”);
for(j=0;j<3;j++)
printf(”%d\t”,c[i][j]);
}
getch();
}

WRITE A C PROGRAM TO CONVERSION FROM UPPERCASE TO LOWER CASE

void main()
{
char str[20];
int i;
clrscr();
printf(”Enter any string->”);
scanf(”%s”,str);
printf(”The string is->%s”,str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf(”\nThe string in uppercase is->%s”,str);
getch();
}

WRITE A C PROGRAM FOR SWAPING OF TWO ARRAYS

void main()
{
int a[10],b[10],c[10],i;
clrscr();
printf(”Enter First array->”);
for(i=0;i<10;i++)
scanf(”%d”,&a[i]);
printf(”\nEnter Second array->”);
for(i=0;i<10;i++)
scanf(”%d”,&b[i]);
printf(”Arrays before swapping”);
printf(”\nFirst array->”);
for(i=0;i<10;i++)
{
printf(”%d”,a[i]);
}
printf(”\nSecond array->”);
for(i=0;i<10;i++)
{
printf(”%d”,b[i]);
}
for(i=0;i<10;i++)
{
//write any swapping technique
c[i]=a[i];
a[i]=b[i];
b[i]=c[i];
}
printf(”\nArrays after swapping”);
printf(”\nFirst array->”);
for(i=0;i<10;i++)
{
printf(”%d”,a[i]);
}
printf(”\nSecond array->”);
for(i=0;i<10;i++)
{
printf(”%d”,b[i]);
}
getch();
}

WRITE A C PROGRAM FOR CONVERSION OF BINARY TO DECIMAL

void main()
{
long int no,n=0,j=1,rem,no1;
clrscr();
printf(”Enter any number any binary form->”);
scanf(”%ld”,&no);
no1=no;
while(no!=0)
{
rem=no%10;
n=n+rem*j;
j=j*2;
no=no/10;
}
printf(”\nThe value of binary no. %ld is ->%ld”,no1,n);
getch();
}

WRITE A C PROGRAM FOR CONVERSION OF DECIMAL TO BINARY

void main()
{
int n,m,no=0,a=1,rem;
clrscr();
printf(”Enter any decimal number->”);
scanf(”%d”,&n);
m=n;
while(n!=0)
{
rem=n%2;
no=no+rem*a;
n=n/2;
a=a*10;
}
printf(”The value %d in binary is->”,m);
printf(”%d”,no);
getch();
}

WRITE A C PROGRAM FOR CHECKING LEAP YEAR

void main()
{
int year;
clrscr();
printf(”Enter any year->”);
scanf(”%d”,&year);
if(((year%4==0)&&(year%100!=0))(year%400==0))
printf(”%d is a leap year”,year);
else
printf(”%d is not a leap year”,year);
getch();
}

WRITE A C PROGRAM FOR PRINTING ASCII VALUE

void main()
{
int i;
clrscr();
for(i=0;i<=255;i++)
{
printf(”%d -> %c “,i,i);
delay(10);
}
getch();
}

WRITE A C PROGRAM TO FIND FIBONACCI SERIES

void main()
{
int i=0,j=1,k=2,r,f;
clrscr();
printf(”Enter the number range:”);
scanf(”%d”,&r);
printf(”\nFIBONACCI SERIES: “);
printf(”%d %d”,i,j);
while(k{
f=i+j;
i=j;
j=f;
printf(” %d”,j);
k++;
}
getch();
}

WRITE A C PROGRAM TO FIND FACTORIAL OF A NUMBER

void main()
{
int i=1,f=1,num;
clrscr();
printf(”\nEnter a number:”);
scanf(”%d”,&num);
while(i<=num)
{
f=f*i;
i++;
}
printf(”\nFactorial of %d is:%d”,num,f);
getch();
}

WRITE A C PROGRAM FOR SWAP TWO VARIABLES WITHOUT USING THIRD VARIABLE

void main()
{
int a,b;
clrscr();
printf(”\nEnter two numbers:”);
scanf(”%d %d”,&a,&b);
printf(”\nBefore swapping a=%d b=%d”,a,b);
a=a^b;
b=b^a;
a=a^b;
printf(”\nAfter swapping a=%d b=%d”,a,b);
getch();
}

WRITE A C PROGRAM TO FIND PRIME FACTORS OF A NUMBER

void main()
{
int num,i=1,j,k;
clrscr();
printf(”\nEnter a number:”);
scanf(”%d”,&num);
while(i<=num)
{
k=0;
if(num%i==0)
{
j=1;
while(j<=i)
{
if(i%j==0)
k++;
j++;
}
if(k==2)
printf(”\n%d is a prime factor”,i);
}
i++;
}
getch();
}

WRITE A C PROGRAM FOR CONCATENATE MANY FILES AND STORE THEM IN A FILE NAMED FILES.

#include”stdio.h”
void concatenate(FILE *fp1,FILE *fp2,char *argv[],int argc);
void main(int argc,char *argv[])
{
FILE *fp1,*fp2;
concatenate(fp1,fp2,argv,argc);
getch();
}
void concatenate(FILE *fp1,FILE *fp2,char **argv,int argc)
{
int i,ch;
fp2=fopen(”files”,”a”);
for(i=1;i
{
fp1=fopen(argv[i],”r”);
while((ch=getc(fp1))!=EOF)
putc(ch,fp2);
}
}

WRITE A CPROGRAM TO FIND MULTIPLICATION TABLE

void main()
{
int r,i,j,k;
clrscr();
printf(”\nEnter the number range:-”);
scanf(”%d”,&r);
for(i=1;i<=r;i++)
{
for(j=1;j<=10;j++)
printf(” %d*%d=%d”,i,j,i*j);
printf(”\n”);
}
getch();
}

WRITE A C PROGRAM FOR WRITING OF ENTIRE ARRAY TO A FILE

#include”stdio.h”
void main()
{
FILE *p;
int i,a[10];
if((p=fopen(”myfile.dat”,”wb”))==NULL)
{
printf(”\nUnable to open file myfile.dat”);
exit(1);
}
printf(”\nEnter ten values, one value on each line\n”);
for(i=0;i<10;i++)
scanf(”%d”,&a[i]);
fwrite(a,sizeof(a),1,p);
fclose(p);
getch();
}

WRITE A C PROGRAM FOR READING OF STRINGS FROM A FILE

#include”stdio.h”
void main()
{
char str[70];
FILE *p;
clrscr();
if((p=fopen(”string.txt”,”r”))==NULL)
{
printf(”\nUnable t open file string.txt”);
exit(1);
}
while(fgets(str,70,p)!=NULL)
puts(str);
fclose(p);
}

WRITE A C PROGRAM TO CREATE A FILE AND STORE DATA IN IT

#include”stdio.h”
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen(”file.txt”,”w”);
printf(”\nEnter data to be stored in to the file:”);
while((ch=getchar())!=EOF)
putc(ch,fp);
fclose(fp);
getch();
}

WRITE A C PROGRAM TO PASSING ONE-DIMENSIONAL ARRAY TO A FUNCTION

#include “stdio.h”
#define N 5
void fstore1D(int a[], int a_size);
void fretrieve1D(int a[], int a_size);
void fedit1D(int a[], int a_size);
int main()
{
int a[N];
printf(”Input data into the matrix:\n”);
fstore1D(a, N);
fretrieve1D(a, N);
fedit1D(a, N);
fretrieve1D(a, N);
return 0;
}
void fstore1D(int a[], int n)
{
int i;
for ( i = 0; i < n; ++i )
scanf(”%d”, &a[i]);
}
void fretrieve1D(int a[], int n)
{
int i;
for ( i = 0; i < n; ++i )
printf(”%6d “, a[i]);
printf(”\n”);
}
void fedit1D(int a[], int n)
{
int i, q;
for ( i = 0; i < n; ++i )
{
printf(”Prev. data: %d\nEnter 1 to edit 0 to skip.”, a[i]);
scanf(”%d”, &q);
if ( q == 1 )
{
printf(”Enter new value: “);
scanf(”%d”, &a[i]);
}
}
}

WRITE A C PROGRAM FOR WRITING OF STRINGS TO A FILE

#include”stdio.h”
void main()
{
FILE *p;
char str[70];
if((p=fopen(”string.txt”,”w”))==NULL)
{
printf(”\nUnable to open file string.txt”);
exit(1);
}
else
printf(”\nEnter a set of strings,Press just enter key to finish\n: “);
while(strlen(gets(str))>0)
{
fputs(str,p);
fputs(”\n”,p);
}
fclose(p);
getch();
}

WRITE A C PROGRAM TO PASSING 2-DIMENSIONAL ARRAY TO A FUNCTION

#include “stdio.h”
#define M 3
#define N 5
void fstore2D(int a[][N]);
void fretrieve2D(int a[][N]);
int main()
{
int a[M][N];
printf(”Input data in matrix (%d X %d)\n”, M, N);
fstore2D(a);
fretrieve2D(a);
return 0;
}
void fstore2D(int a[][N])
{
int i, j;
for ( i = 0; i < M; ++i )
{
for ( j = 0; j < N; ++j )
scanf(”%d”, &a[i][j]);
}
}
void fretrieve2D(int a[][N])
{
int i, j;
for ( i = 0; i < M; ++i )
{
for ( j = 0; j < N; ++j )
printf(”%6d “, a[i][j]);
printf(”\n”);
}
}

No comments: