Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

WEEK 15 LAB PRACTICAL

Question:

write a program in C to count the number of customers with educational qualification as phD who has ordered
food online .Display the count

to store the details of the customers (age, gender ,martial status,monthly income) who are self employeed into a
self file named customer_SelfEmployeed.csv

display the details of the customers(Age,Gender,Occupation) where monthly income is "No income"

Part1

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
FILE *fp = fopen("onlinefood.csv", "r");
if (fp==NULL)
printf("No file");
char a[500];
int count = 0;
while (fgets(a, 500, fp) != NULL) {
char *age = strtok(a, ",");
char *gender = strtok(NULL, ",");
char *marital = strtok(NULL, ",");
char *occup = strtok(NULL, ",");
char *month_income = strtok(NULL, ",");
char *edq = strtok(NULL, ",");
if (strcmp(edq, "Ph.D") == 0)
count++;
}
printf("The number of people that have Ph.D is %d\n", count);
fclose(fp);
return 0;
}
Part2

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

int main(void) {
FILE *fp = fopen("onlinefood.csv", "r");
FILE *fw = fopen("customer_selfemployed.csv", "w");
if (fp == NULL || fw == NULL) {
printf("Error opening file.\n");
return 1;
}

fprintf(fw, "Age,Gender,Marital status,Monthly Income\n");

char line[500];

// Skip the header line


fgets(line, sizeof(line), fp);

while (fgets(line, sizeof(line), fp) != NULL) {


char *age = strtok(line, ",");
char *gender = strtok(NULL, ",");
char *marital = strtok(NULL, ",");
char *occup = strtok(NULL, ",");
char *month_income = strtok(NULL, ",");

// Check if occupation is "Self Employed"


if (occup != NULL && strcmp(occup, "Self Employed") == 0) {
// Write the data to the output file
fprintf(fw, "%s,%s,%s,%s\n", age, gender, marital, month_income);
}
}

fclose(fp);
fclose(fw);

printf("Process completed successfully.\n");

return 0;
}

Age Gender Marital status Monthly Income


23 Female Single 10001 to 25000
30 Male Married More than 50000
27 Female Married 10001 to 25000
28 Male Married 10001 to 25000
26 Male Single 10001 to 25000
Prefer not to
24 Male say More than 50000
27 Female Married More than 50000
26 Male Single 25001 to 50000
26 Female Single 25001 to 50000
24 Male Married More than 50000
27 Female Married 25001 to 50000
25 Male Single 25001 to 50000
24 Female Married 10001 to 25000
25 Male Married More than 50000
26 Male Married More than 50000
25 Female Married 25001 to 50000
31 Male Married More than 50000
24 Female Married More than 50000
25 Male Single More than 50000
27 Male Married 25001 to 50000
25 Male Single 10001 to 25000
32 Male Married 10001 to 25000
27 Female Married 25001 to 50000
28 Male Single 10001 to 25000
26 Female Married 10001 to 25000
29 Male Single More than 50000
24 Female Single 25001 to 50000
Prefer not to
26 Male say More than 50000
30 Male Married More than 50000
25 Male Single More than 50000
27 Male Married More than 50000
30 Male Married 25001 to 50000
30 Male Married More than 50000
25 Male Married More than 50000
25 Male Married 25001 to 50000
27 Female Married More than 50000
28 Male Single 10001 to 25000
26 Female Married 10001 to 25000
29 Male Single More than 50000
24 Female Single 25001 to 50000
Prefer not to
26 Male say More than 50000
25 Male Single More than 50000
27 Male Married 25001 to 50000
25 Male Single 10001 to 25000
22 Male Married 10001 to 25000
25 Male Single More than 50000
27 Male Married 25001 to 50000
25 Male Single 10001 to 25000
26 Female Married 10001 to 25000
29 Male Single More than 50000
24 Female Single 25001 to 50000
Prefer not to
26 Male say More than 50000
30 Male Married More than 50000
25 Male Single More than 50000
Part3

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

int main(void){
FILE *fp = fopen("onlinefood.csv", "r");
if (fp==NULL)
printf("No file");
char a[500];
printf("Age\tGender\tOccupation\n");
while (fgets(a, 500, fp) != NULL) {
char *age = strtok(a, ",");
char *gender = strtok(NULL, ",");
char *marital = strtok(NULL, ",");
char *occup = strtok(NULL, ",");
char *month_income = strtok(NULL, ",");
if (strcmp(month_income, "No Income") == 0)
printf("%s\t%s\t%s\n", age, gender, occup);
}
fclose(fp);
return 0;
}

You might also like