Program 9 to 12
Program 9 to 12
Program 9 to 12
void main( )
{
int n,digit;
char str1[10],str2[10];
do
{
printf("press 1-compare 2-concatenate 3-length of string");
printf("\n enter your choice=");
scanf("%d",&n);
switch(n)
{
case 1:printf("enter first string=");
scanf("%s",str1);
printf("enter second string=");
scanf("%s",str2);
compare(str1,str2);
break;
Program – 10
Implement structures to read, write and compute average- marks of the
students, list the students scoring above and below the average marks for a
class of N students.
#include<stdio.h>
struct student
{
char usn[10];
char name[10];
float m1,m2,m3;
float avg,total;
};
void main()
{
struct student s[20];
int n,i;
printf("Enter the number of student=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the details of Student-%d \n",i+1);
printf("\n Enter USN=");
scanf("%s",s[i].usn);
printf("\n Enter Name=");
scanf("%s",s[i].name);
printf("\n Enter the three subject score=");
scanf("%f%f%f",&s[i].m1,&s[i].m2,&s[i].m3);
s[i].total=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].total/3;
}
for(i=0;i<n;i++)
{
if(s[i].avg>=35)
printf("\n %s has scored above the average marks",s[i].name);
else
printf("\n %s has scored below the average marks",s[i].name);
}
}
Program – 11
Develop a program using pointers to compute the sum, mean and standard
deviation of all elements stored in an array of N real numbers.
#include<stdio.h>
#include<math.h>
int main()
{
float a[10],sum=0,mean,temp=0,std,variance,*p;
int i,n;
printf("how many elements:\n");
scanf("%d",&n);
printf("enter array elements:\n");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
p=a;
for(i=0;i<n;i++)
{
sum = sum+*p;
p++;
}
mean= sum/n;
p=a;
for(i=0;i<n;i++)
{
temp=temp+pow((*p-mean),2);
p++;
}
variance=temp/n;
std= sqrt(variance);
printf("sum = %f\n mean= %f\n standard deviation= %f\n",sum,mean,std);
}
Program – 12
Write a C program to copy a text file to another, read both the input file name
and target file name.
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fptr1,*fptr2;
char filename[100], c;
printf("Enter filename to open for reading:");
scanf("%s",filename);
fptr1=fopen(filename,"r");
if(fptr1==NULL)
{
printf("Cannot open file %s\n", filename);
exit(0);
}
printf("Enter filename to open for writing:");
scanf("%s",filename);
fptr2=fopen(filename,"w");
if(fptr2==NULL)
{
printf("Cannot open file %s\n", filename);
exit(0);
}
c=fgetc(fptr1);
while(c!=EOF)
{
fputc(c,fptr2);
c=fgetc(fptr1);
}
printf("\nContents copied to %s",filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}