Wednesday, April 1, 2015

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();
 }

C program to calculate sum of two m*n matrices and store the result in 3rd matrix.

Write a ‘C’ program to calculate sum of two m*n matrices and store the result in 3rd matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[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]);
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<=2;i++)
{
printf("\n");
for(j=0;j<=2;j++)
{
printf("%d\t",c[i][j]);
}
}
getch();
}

C program for 2-D array

Write a ‘C’ program to accept ‘n’ names from user store these name into
2-D array. Accept the name from user and search whether the name is
present in an array or not.

#include<stdio.h>
#include<conio.h>
void main()
{
char name[5][10],res[10];
int i,j,n;
clrscr();
printf("enter the no");
scanf("%d",&n);
printf("enter the matrix element");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%s",&name[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("enter the name");
scanf("%s",&res);
if(strcmp(&name[i][j],&res)!=0)
{
printf("name is not found");
break;
}
else
{
printf("name is found");
}
}
}
getch();
}

C program create a student struct accept details of n students into structure rearrange data in alphabetical order of stud name and display result.

Write a ‘C’ program to create a student structure having field stud name and address. Accept the details of ‘n’ students into the structure rearrange the data in alphabetical order of stud name and display the result.

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char sname[20];
char add[20];
};
void main()
{
struct student s[10];
char ch[20];
int i,j,n;
clrscr();
printf("enter the student");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the sname&add");
for(i=0;i<n;i++)
{
scanf("%s%s",&s[i].sname,&s[i].add);
}
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(&s[i].sname,&s[j].sname)>0)
{
strcpy(ch,&s[i].sname);
strcpy(&s[i].sname,&s[i].sname);
strcpy(&s[j].sname,ch);
}
}
}
for(i=0;i<n;i++)
{
printf("%s",s[i].sname);
}
getch();
}

Petabit examples

 Petabit examples  A petabit is a unit of digital information or storage, equivalent to 1,000 terabits or 1 million gigabits. To put it into...