0% found this document useful (0 votes)
26 views6 pages

Assignment - 5: #Include

Uploaded by

aryan86ya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

Assignment - 5: #Include

Uploaded by

aryan86ya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ASSIGNMENT -5

Q=Write a C program to read and print an Employee’s Details using Structure - You have to read
employee’s details like name, employee id and salary using a structure and then print the entered
values

#include <stdio.h>

int main() {

char id[10];

int hour;

double value, salary;

printf("Input the Employees ID(Max. 10 chars): ");

scanf("%s", &id);

printf("\nInput the working hrs: ");

scanf("%d", &hour);

printf("\nSalary amount/hr: ");

scanf("%lf", &value);

salary = value * hour;

printf("\nEmployees ID = %s\nSalary = U$ %.2lf\n", id,salary);

return 0;

Q=Write a C Program to Add Two Complex Numbers by Passing Structure to a Function

#include<stdio.h>
/* Declaring Structure */

struct complex

float real;

float imaginary;

};

int main()

/* Declaring structure variable using struct complex */

struct complex cnum1, cnum2, sum;

printf("Enter real and imaginary part of first complex number:\n");

scanf("%f%f", &cnum1.real, &cnum1.imaginary);

printf("Enter real and imaginary part of second complex number:\n");

scanf("%f%f", &cnum2.real, &cnum2.imaginary);

sum.real = cnum1.real + cnum2.real;

sum.imaginary = cnum1.imaginary + cnum2.imaginary;

printf("SUM = %0.2f + i %0.2f", sum.real, sum.imaginary);

return 0;

Q=Write a C Program to store Information of 5 Students Using Array of Structure


#include<stdio.h>
struct student
{
char name[20];
int id;
float marks;
};
void main()
{
struct student s1,s2,s3;
int dummy;
printf("Enter the name, id, and marks of student 1 ");
scanf("%s %d %f",s1.name,&s1.id,&s1.marks);
scanf("%c",&dummy);
printf("Enter the name, id, and marks of student 2 ");
scanf("%s %d %f",s2.name,&s2.id,&s2.marks);
scanf("%c",&dummy);
printf("Enter the name, id, and marks of student 3 ");
scanf("%s %d %f",s3.name,&s3.id,&s3.marks);
scanf("%c",&dummy);
printf("Printing the details....\n");
printf("%s %d %f\n",s1.name,s1.id,s1.marks);
printf("%s %d %f\n",s2.name,s2.id,s2.marks);
printf("%s %d %f\n",s3.name,s3.id,s3.marks);
}

Q=Write a program in C to create and store information in a text file

#include <stdio.h>
#include <stdlib.h>

int main()
{
char str[1000];
FILE *fptr;
char fname[20]="test.txt";

printf("\n\n Create a file (test.txt) and input text :\n");


printf("----------------------------------------------\n");
fptr=fopen(fname,"w");
if(fptr==NULL)
{
printf(" Error in opening file!");
exit(1);
}
printf(" Input a sentence for the file : ");
fgets(str, sizeof str, stdin);
fprintf(fptr,"%s",str);
fclose(fptr);
printf("\n The file %s created successfully...!!\n\n",fname);
return 0;
}

Q=Write a program in C to read an existing file

#include <stdio.h>

#include <stdlib.h>

void main()

FILE *fptr;

char fname[20];

char str;

printf("\n\n Read an existing file :\n");

printf("------------------------------\n");

printf(" Input the filename to be opened : ");

scanf("%s",fname);

fptr = fopen (fname, "r");

if (fptr == NULL)

printf(" File does not exist or cannot be opened.\n");


exit(0);

printf("\n The content of the file %s is :\n",fname);

str = fgetc(fptr);

while (str != EOF)

printf ("%c", str);

str = fgetc(fptr);

fclose(fptr);

printf("\n\n");

Q=Write a program in C to Find the Number of Lines in a Text File

#include <stdio.h>

#define FSIZE 100

int main()
{
FILE *fptr;
int ctr = 0;
char fname[FSIZE];
char c;
printf("\n\n Read the file and count the number of lines :\n");
printf("--------------------------------------------------\n");
printf(" Input the file name to be opened : ");
scanf("%s",fname);

fptr = fopen(fname, "r");


if (fptr == NULL)
{
printf("Could not open file %s", fname);
return 0;
}
// Extract characters from file and store in character c
for (c = getc(fptr); c != EOF; c = getc(fptr))
if (c == '\n') // Increment count if this character is newline
ctr = ctr + 1;
fclose(fptr);
printf(" The lines in the file %s are : %d \n \n", fname, ctr-1);
return 0;
}

You might also like