Showing posts with label Structure and Union. Show all posts
Showing posts with label Structure and Union. Show all posts

C program to store information using structures with dynamically memory allocation

PROGRAM:

#include <stdio.h>
#include<stdlib.h>
 
struct course
{
   int marks;
   char subject[30];
};
 
int main()
{
   struct course *ptr;
   int i, noOfRecords;
   printf("Enter number of records: ");
   scanf("%d", &noOfRecords);
 
 
   ptr = (struct course*) malloc (noOfRecords * sizeof(struct course));
 
   for(i = 0; i < noOfRecords; ++i)
   {
       printf("Enter name of the subject and marks respectively:\n");
       scanf("%s %d", &(ptr+i)->subject, &(ptr+i)->marks);
   }
 
       printf("Displaying Information:\n");
 
       for(i = 0; i < noOfRecords ; ++i)
       printf("%s\t%d\n", (ptr+i)->subject, (ptr+i)->marks);
 
   return 0;
}

OUTPUT:

Enter number of records: 2
Enter name of the subject and marks respectively:
Programming
22
Enter name of the subject and marks respectively:
Structure
33
 
Displaying Information:
Programming      22
Structure        33

C program to store information of students using structure

PROGRAM:
#include <stdio.h>
struct student
{
    char name[50];
    int roll;
    float marks;
} s[10];
 
int main()
{
    int i;
 
    printf("Enter information of students:\n");
 
    for(i=0; i<10; ++i)
    {
        s[i].roll = i+1;
 
        printf("\nFor roll number%d,\n",s[i].roll);
 
        printf("Enter name: ");
        scanf("%s",s[i].name);
 
        printf("Enter marks: ");
        scanf("%f",&s[i].marks);
 
        printf("\n");
    }
 
    printf("Displaying Information:\n\n");
    for(i=0; i<10; ++i)
    {
        printf("\nRoll number: %d\n",i+1);
        printf("Name: ");
        puts(s[i].name);
        printf("Marks: %.1f",s[i].marks);
        printf("\n");
    }
    return 0;
}
 

OUTPUT:

Enter information of students: 
 
For roll number1,
Enter name: Tom
Enter marks: 98
 
For roll number2,
Enter name: Jerry
Enter marks: 89
.
.
.
Displaying Information:
 
Roll number: 1
Name: Tom
Marks: 98
.
.
.

C program to calculate difference between two time periods

PROGRAM:

#include <stdio.h>
struct TIME
{
  int seconds;
  int minutes;
  int hours;
};
void differenceBetweenTimePeriod(struct TIME t1, struct TIME t2, struct TIME
     *diff);
 
int main()
{
    struct TIME startTime, stopTime, diff;
 
    printf("Enter start time: \n");
    printf("Enter hours, minutes and seconds respectively: ");
    scanf("%d %d %d", &startTime.hours, &startTime.minutes, 
    &startTime.seconds);
 
    printf("Enter stop time: \n");
    printf("Enter hours, minutes and seconds respectively: ");
    scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds);
 
   
    differenceBetweenTimePeriod(startTime, stopTime, &diff);
 
    printf("\nTIME DIFFERENCE: %d:%d:%d - ", startTime.hours, 
    startTime.minutes, startTime.seconds);
    printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds);
    printf("= %d:%d:%d\n", diff.hours, diff.minutes, diff.seconds);
 
    return 0;
}
 
void differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct 
TIME *diff)
    {
    if(stop.seconds > start.seconds){
    --start.minutes;
    start.seconds += 60;
    }
 
    diff->seconds = start.seconds - stop.seconds;
    if(stop.minutes > start.minutes){
    --start.hours;
    start.minutes += 60;
    }
 
    diff->minutes = start.minutes - stop.minutes;
    diff->hours = start.hours - stop.hours;
}

OUTPUT:

Enter start time:
Enter hours, minutes and seconds respectively: 12
34
55
Enter stop time:
Enter hours, minutes and seconds respectively:8
12
15
 
TIME DIFFERENCE: 12:34:55 - 8:12:15 = 4:22:40