Wednesday, April 1, 2015

C program to accept m*n matrix from user and display the elements of a given matrix using function.

Write a ‘C’ program to accept m*n matrix from user and display the elements of a given matrix using function.

#include<stdio.h>
#include<conio.h>
void disp(int *,int,int);
void main()
{
int a[3][3],i,j;
clrscr();
printf("enter the matrix element");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
}
disp(a,3,3);
getch();
}
void disp(int *p,int x,int y)
{
int i,j;
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d\t",*p);
}
printf("\n");
}
}

C program using command line arguments to search for a word in a file and replace it with the specified word.

Write a ‘C’ program using command line arguments to search for a word in a file and replace it with the specified word.

#include<stdio.h>
#include<conio.h>
void main(int argc,char *argv[])
{
FILE *fp,*fq;
char ch;
clrscr();
if(argc!=4)
{
printf("improper no of argument");
exit(0);
}
fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("file does not exist");
exit(0);
}
fq=fopen("b.txt","w");
if(fq==NULL)
{
printf("file does not exist");
exit(0);
}
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
 break;
if(ch==*argv[2])
{
  fputc(*argv[3],fq);
}
else
{
 fputc(ch,fq);
}
}
 fclose(fp);
 fclose(fq);
 fp=fopen("b.txt","r");
 fq=fopen(argv[1],"w");
 while(1)
 {
   ch=fgetc(fp);
   if(ch==EOF)
      break;
   fputc(ch,fq);
 }
 getch();
       }

C program to check whether given string is palindrome or not.

Write a ‘C’ program to check whether given string is palindrome or not.


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20];
int l,f,i;
clrscr();
printf("enter the string");
gets(name);
l=strlen(name);
for(i=0;i<l;i++)
{
if(name[i]!=name[l-1])
{
f=0;
break;
}
else
{
f=1;
l--;
}
}
if(f==1)
{
printf("no is palindrome");
}
else
{
printf("no is not palindrome");
}
getch();
}




C program to accept n numbers store all perfect numbers in an array and display this array.

Write a ‘C’ program to accept ‘n’ numbers, store all perfect numbers in an array and display this array.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],i,j,k,n,m,sum,l=0;
clrscr();
printf("enter the range value");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter no");
scanf("%d",&m);
j=1;
sum=0;
while(j<m)
{
k=m%j;
if(k==0)
{
sum=sum+j;
}
j++;
}
if(sum==m)
{
a[l]=m;
l++;
}
}
for(i=0;i<l;i++)
{
printf("%d",a[i]);
}
getch();
}

C program to accept the line from user, delete all the occurrences of word ‘the' from the line and display the result.

Write a ‘C’ program to accept the line from user, delete all the occurrences of word ‘the’ from the line and display the result.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10],res[10];
int l,i,j=0;
clrscr();
printf("enter the string");
gets(name);
l=strlen(name);
for(i=0;i<l;i++)
{
if((name[i]=='t')&&(name[i+1]=='h')&&(name[i+2]=='e'))
{
name[i]=' ';
name[i+1]=' ';
name[i+2]=' ';
}
}
for(i=0;i<l;i++)
{
if(name[i]!=' ')
{
res[j]=name[i];
j++;
}
}
for(i=0;i<j;i++)
{
printf("%c",res[i]);
}
getch();
}

C program to calculate sum of elements of lower triangle of a n*n matrix.

Write a ‘C’ program to calculate sum of elements of lower triangle of a n*n matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j,sum=0;
clrscr();
printf("enter the matrix element");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(i>j)
{
sum=sum+a[i][j];
}
}
}
printf("%d\n",sum);
getch();
}

C program to calculate length of a given string without using standard function.

Write a ‘C’ program to calculate length of a given string without using standard function.


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20];
int i,l=0;
clrscr();
printf("enter the string");
gets(name);
for(i=0;name[i]!='\0';i++)
{
l++;
}
printf("%d",l);
getch();
}

C program to accept ‘n’ numbers from user and find out the maximum element out of them by using dynamic memory allocation.

Write a ‘C’ program to accept ‘n’ numbers from user and find out the maximum element out of them by using dynamic memory allocation.

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void main()
{
int n,i,max=0;
int *p;
clrscr();
printf("enter the size");
scanf("%d",&n);
p=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
if(p[i]>max)
max=p[i];
}
printf("%d",max);
getch();
}

C program to Create a text file input.txt and Print the contents of file in reverse order.

Write a ‘C’ program to do the following.
> Create a text file ‘input.txt’
> Print the contents of file in reverse order.

#include<stdio.h>
#include<conio.h>
void main()
{
 FILE *fp,*ft;
 char ch,name[80];
 int i=0;
 clrscr();
 fp=fopen("input.txt","r");
 if(fp==NULL)
 {
   printf("File Does Not Exist");
   exit(0);
 }
 ft=fopen("b.txt","w");
 if(ft==NULL)
 {
   printf("File Does Not Exist");
   exit(0);
 }
 while(1)
 {
  ch=fgetc(fp);
  if(ch!=' ')
  {
    name[i]=ch;
    i++;
  }
  else
  {
    strrev(name);
    fputs(name,ft);
    i=0;
   }
  if(ch==EOF)
  {
    break;
  }
 }
 fclose(fp);
 fclose(ft);
 getch();
}

C program to accept string from user and convert all lower case letters into upper case and vice-versa.

Write a ‘C’ program to accept string from user and convert all lower case letters into upper case and vice-versa.

#include<stdio.h>
#include<conio.h>
void main()
{
  char name[80];
  int l,i;
  clrscr();
  printf("Enter the String");
  gets(name);
  l=strlen(name);
  for(i=0;i<l;i++)
  {
    if(islower(name[i]))
    {
      printf("%c",toupper(name[i]));
    }
    else  if(isupper(name[i]))
    {
      printf("%c",tolower(name[i]));
    }
    else
    {
      printf("%c",name[i]);
    }
  }
  getch();
 }

Featured posts

Mongolia

 Mongolia! Mongolia is a vast and sparsely populated country in East Asia, known for its stunning natural beauty, rich history, and unique c...

Popular posts