Wednesday, 18 March 2015

C Programs For Engineering 1st year Students page 10



Page 10


91. Program to accept elements into single dimensional array and print the array in ascending order by using three different arrays.

#include<conio.h>
#include<stdio.h>
void read_array(int x[]);
void sort_array(int y[]);
void print_array(int z[]);
main()
{
int a[10];
clrscr( );
read_array(a);
sort_array(a);
print_array(a);
getch( );
}
void read_array(int x[])
{
int i;
for(i=0;i<10;i++)
{
printf(“enter value for a[%d]”,i);
scanf(“%d”,&x[i]);
}
}
void sort_array(int y[])
{
int i,j,k;
for(i=0;i<9;i++)
for(j=i+1;j<=9;j++)
if(y[i]>y[j])
{
k=y[i];
y[i]=y[j];
y[j]=k;
}
}
void print_array(int z[])
{
int i;
for(i=0;i<10;i++)
printf(“%d\n”,z[i]);
}

92.Program to accept data and store the given data into file print the data.

#include<conio.h>
#include<stdio.h>
main( )
{
FILE *fp;
char c;
fp=fopen(“data.dat”,”w”);
clrscr();
printf(“enter text”);
while(1)
{
c=getchar( );
if(c==eof( ))
break;
putc(c);
}
fclose(fp);
fp=fopen(“data.dat”,”r”);
while(1)
{
c=getc(fp);
if(c==eof( ))
break;
putchar(c);
}
getch( );
fclose(fp);
}

93. Program to accept data in lower case and store the given data into file into upper case and print the data.

#include<conio.h>
#include<stdio.h>
main( )
{
FILE *fp;
Char c;
fp=fopen(“data2.dat”,”w”);
clrscr( );
printf(“enter text”);
while((c=getchar( ))!=eof( ))
{
putc(toupper(c),fp)
}
fclose(fp);
fp=fopen(“data2.dat”,”r”);
while(1)
{
c=getc(fp);
if(c==eof( ))
break;
putchar(c);
}
getch( );
fclose(fp);
}

94.Program to copy  contents of one file into another.

#include<conio.h>
#include<stdio.h>
main( )
{
FILE * fp1,*fp2;
char ch;
fp1=fopen(“text1”,”w”);
printf(“enter the text”);
while((ch=getchar()!=EOF);
putc(ch,fp1);
fclose(fp1);
fp1=fopen(“text1”,”r”);
fp2=fopen(“text2”,”w”);
while((ch=getc(fp1))!=EOF)
putc(ch,fp2);
fcolse(fp1);
fcolse(fp2);
getch( );
}

95. Program to create a file of numbers and copy odd number into second file and even number into third file

#include<conio.h>
#include<stdio.h>
main( )
{
FILE  *fp1,*fp2,*fp3;
int i;
fp1=open(“data1”,w”);
printf(“enter the number”);
scanf(“%d”,&i);
while(i!=eof)
{
putw(i,fp1);
scanf(“%d”,&i);
}
fcolse(fp1);
fp1=fopen(“data1”,”r”);
fp2=fopen(“data2”,”w”);
fp3=fopen(“data3”,”w”);
while((i=getc(fp1))!=eof)
if(i%2==0)
putc(i,fp3);
else
putw(i,fp2);
fcolse(fp1);
fcolse(fp2);
fcolse(fp3);
getch( );
}

96.Program to accept a string in lower case and print first character of each word in upper case.

       #include<conio.h>
       #include<stdio.h>
        main( )
       {
       char str1[80];
        int  length,i;
        clrscr( );
        printf(“enter a string; “);
        length=getline(str1,80);
        for(i=0;i<length;i++)
           {
        str1[0]-=32;
        if(str1[i]= =’  ‘)
       str1[i+1]-=32;
         printf(“%c”.str1[i]);
          }
          getch();
           }
           int getline(char line [], int lim)
            {
           int i;
           for(i=0;i<lim && ((line[i]=getchar( ))!=’\n’);i++);
           if(line[i]= =’\n’)
           line[i]=’\0’;
            return i;
            }








97.Program to accept two numbers and interchange two values using functions.
     
#include<conio.h>
#include<stdio.h>
void swap (int a, int b);
main( )
{
int a,b;
clrscr( );
printf(“enter value for a;”);
scanf(“%d”,&a);
printf(“enter value for b;”);
scanf(“%d”,&b);
swap(a,b);
getch( );
}
void swap(int a,int b)
}
int c;
c=a;
a=b;
b=c;
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
}

98.Program for example of static variable.

#include<conio.h>
#include<stdio.h>
static int i=1;
main( )
{
int j;
clrscr( );
for (j=1;j<=5;j++);
fun( );
getch( );
}
fun( )
{
printf(“\n%d”,i);
i=i+1;
}

99.Program  to accept a string and print by trailing spaces.

#include<conio.h>
#include<stdio.h>
main( )
{
char n,n1;
clrscr ( );
printf(“enter a string;”);
while((n=getchar( )!=’\n’)
if(n>=’a’ && n<=’z’)
putchar(n);
else
if(n>=’a’ && n<=’z’)
putchar(n);
getch( );
}




100. Program to print anti diagonal.

#include<conio.h>
#include<stdio.h>
main( )
{
int a[4][4],i,j,c;
clrscr( );
printf(“enter which number you want;”);
scanf(“%d”,&c);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(i+j= =3)
a[i]]j]=c;
else
a[i][j]=0
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf(“%d”,a[i][j]);
printf(“\n”);
}
getch( );
}


Pages 123456789, 10

Load disqus comments

0 comments