Module 5 Programs
Module 5 Programs
#include<stdio.h>
struct student
{
char name[20];
int roll;
};
void main()
{
int i;
struct student s1;
printf("enter student details\n");
printf("Enter name :");
scanf("%s",s1.name);
printf("Enter roll number :");
scanf("%d",&s1.roll);
printf("Student details are\n");
printf("student name is %s and the roll no. is %d\n",s1.name,s1.roll);
}
#include<stdio.h>
struct comp
{
float real;
float img;
};
void result(struct comp a,struct comp b);
void main()
{
struct comp a1,a2;
printf("enter 1st complex number real and imaginary parts\n");
scanf("%f%f",&a1.real,&a1.img);
printf("enter 2nd complex number real and imaginary parts\n");
scanf("%f%f",&a2.real,&a2.img);
result(a1,a2);
}
void result(struct comp x,struct comp y)
{
struct comp res;
res.real=x.real+y.real;
res.img=x.img+y.img;
printf("result complex number is =%.1f+%.1fi",res.real,res.img);
}
#include<stdio.h>
struct student
{
char name[20];
int marks;
float perc;
}s[10];
void main()
{
printf("enter 10 student details");
for(int i=0;i<10;i++)
{ printf("Enter name, marks and percentage of the student");
scanf("%s%d%f",s[i].name,&s[i].marks,&s[i].perc);
}
printf("The 10 student details are");
for(int i=0;i<10;i++)
{
printf("Name=%s\t marks=%d\t percentage=%f\n",s[i].name,s[i].marks,s[i].perc);
}
}
#include<stdlib.h>
void main()
{
FILE *fp;
char ch;
fp=fopen("E:/ramani/college-work/subjects/CP/textfile.txt","r");
if(fp==NULL)
{
printf("error");
exit(0);
}
while((ch=fgetc(fp))!=EOF)
{
putchar(ch);
}
fclose(fp);
}
Read numbers from a file and write even and odd numbers to separate file.
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp,*fp1,*fp2;
int a;
fp=fopen("E:/ramani/college-work/subjects/CP/numfile.txt","r");
if(fp==NULL)
{
printf("error");
exit(0);
}
fp1=fopen("E:/ramani/college-work/subjects/CP/even.txt","w");
if(fp1==NULL)
{
printf("error");
exit(0);
}
fp2=fopen("E:/ramani/college-work/subjects/CP/odd.txt","w");
if(fp2==NULL)
{
printf("error");
exit(0);
}
while(fscanf(fp,"%d",&a)!=EOF)
{
if (a%2==0)
fprintf(fp1,"%d ",a);
else
fprintf(fp2,"%d ",a);
}
fclose(fp);
fclose(fp1);
fclose(fp2);
}
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
char ch;
int characters=0,lines=0,words=0;
fp=fopen("E:/ramani/college-work/subjects/CP/textfile.txt","r");
if(fp==NULL)
{
printf("error");
exit(0);
}
while((ch=fgetc(fp))!=EOF)
{
characters++;
/* Check new lines */
if (ch == '\n' )//|| ch == '\0')
{
lines++;
//characters++;
}
/* Check words */
if (ch == ' ' || ch == '\t' || ch == '\n' )// || ch == '\0')
{
words++;
//characters++;
}
}
/* Increment words and lines for last word */
if (characters > 0)
{
words++;
lines++;
}
fclose(fp);
}