C Program to print student examination result
Code:
#include<stdio.h> struct info { char name[20]; int roll; float marks; int rank; }; int main() { struct info student1; printf("Enter the values: "); scanf("%s",student1.name); scanf("%d",&student1.roll); scanf("%f",&student1.marks); scanf("%d",&student1.rank); printf("Name: %s\n",student1.name); printf("Roll: %d\n",student1.roll); printf("Marks: %f\n",student1.marks); printf("Rank: %d\n",student1.rank); return 0; }
Output:
Enter the values: Shahbaz 17 90 4 Name: Shahbaz Roll: 17 Marks: 90.000000 Rank: 4
C Program to Add Two Distances (in inch-feet system) using Structures
Code:
#include <stdio.h> struct Distance { int x; float y; } distance1, distance2, total; int main() { printf("Enter first distance\n"); printf("Enter in feet: "); scanf("%d", &distance1.x); printf("Enter in inches: "); scanf("%f", &distance1.y); printf("\nEnter second distance\n"); printf("Enter in feet: "); scanf("%d", &distance2.x); printf("Enter in inches: "); scanf("%f", &distance2.y); total.x = distance1.x + distance2.x; total.y = distance1.y + distance2.y; while (total.y >= 12.0) { total.y = total.y - 12.0; ++total.x; } printf("Sum of distance is %d\'-%.1f\n", total.x, total.y); return 0; }
Output:
Enter first distance Enter in feet: 5 Enter in inches: 8.8 Enter second distance Enter in feet: 6 Enter in inches: 4.2 Sum of distance is 12'-1.0
C Program to Calculate Difference Between Two Time Periods
Code:
#include <stdio.h> struct interval { int sec; int min; int hrs; }; void difference(struct interval t1, struct interval t2,struct interval *diff); void difference(struct interval start,struct interval stop,struct interval *diff) { while (stop.sec > start.sec) { --start.min; start.sec += 60; } diff->sec = start.sec - stop.sec; while (stop.min > start.min) { --start.hrs; start.min += 60; } diff->min = start.min - stop.min; diff->hrs = start.hrs - stop.hrs; } int main() { struct interval startinterval, stopinterval, diff; printf("Enter the start interval. \n"); printf("Enter hrs, min and sec: "); scanf("%d %d %d", &startinterval.hrs, &startinterval.min, &startinterval.sec); printf("Enter the stop interval. \n"); printf("Enter hrs, min and sec: "); scanf("%d %d %d", &stopinterval.hrs, &stopinterval.min, &stopinterval.sec); difference(startinterval, stopinterval, &diff); printf("Interval Difference: %d:%d:%d - ", startinterval.hrs, startinterval.min, startinterval.sec); printf("%d:%d:%d ", stopinterval.hrs, stopinterval.min, stopinterval.sec); printf("= %d:%d:%d\n", diff.hrs, diff.min, diff.sec); return 0; }
Output:
Enter the start interval. Enter hrs, min and sec: 5 32 15 Enter the stop interval. Enter hrs, min and sec: 9 45 52 interval Difference: 5:32:15 - 9:45:52 = -5:46:23
C Program to store and print details of stocks
Code:
#include<stdio.h> struct invent { char *name[15]; int number; float price; }; int main() { struct invent product[3],*ptr; ptr = &product[3]; printf("Enter the values: \n"); scanf("%s", ptr->name); scanf("%d", &ptr->number); scanf("%f", &ptr->price); printf("Your entered value:\n"); printf("Item name: %s\n",ptr->name); printf("Quantity: %d\n",ptr->number); printf("Price per item: %f\n",ptr->price); return 0; }
Output:
Enter the values: Shirts 70 1200 Your entered value: Item name: Shirts Quantity: 70 Price per item: 1200.000000
C Program to Store Data in Structures Dynamically
Code:
#include <stdio.h> #include <stdlib.h> struct class { int score; char subject[20]; }; int main() { struct class *ptr; int i, j; printf("Enter the number of subjects: "); scanf("%d", &j); ptr = (struct class *)malloc(j * sizeof(struct class)); for (i = 0; i < j; ++i) { printf("Enter the name of the subject:\n"); scanf("%s", (ptr + i)->subject); printf("Enter the score:\n"); scanf("%d", &(ptr + i)->score); } printf("Student Record:\n"); for (i = 0; i < j; ++i) printf("%s:\t%d\n", (ptr + i)->subject, (ptr + i)->score); return 0; }
Output:
Enter the number of subjects: 4 Enter the name of the subject: English Enter the score: 80 Enter the name of the subject: Hindi Enter the score: 40 Enter the name of the subject: Maths Enter the score: 90 Enter the name of the subject: Computer Enter the score: 85 Student Record: English: 80 Hindi: 40 Maths: 90 Computer: 85