Skip to main content

Posts

Showing posts with the label Structure and Union

C Program to Store Data in Structures Dynamically

  Demonstrate the Dynamic Memory Allocation for Structure # include <stdio.h> # include <stdlib.h> struct course { int marks; char subject[ 30 ]; }; int main () { struct course * ptr ; int i, noOfRecords; printf ( "Enter the number of records: " ); scanf ( "%d" , &noOfRecords); // Memory allocation for noOfRecords structures ptr = (struct course *) malloc (noOfRecords * sizeof (struct course)); for (i = 0 ; i < noOfRecords; ++i) { printf ( "Enter the 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 the number of records: 2 Enter the name of the subject and marks respectivel...

C Program to Calculate Difference Between Two Time Periods

  Calculate Difference Between Two Time Periods # 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 the start time. \n" ); printf ( "Enter hours, minutes and seconds: " ); scanf ( "%d %d %d" , &startTime.hours, &startTime.minutes, &startTime.seconds); printf ( "Enter the stop time. \n" ); printf ( "Enter hours, minutes and seconds: " ); scanf ( "%d %d %d" , &stopTime.hours, &stopTime.minutes, &stopTime.seconds); // Difference between start and stop time differenceBetweenTimePeriod(startTime, stopTime, &diff); printf ( "\nTime Difference: %d:%d:%d - ...

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

  Add Two Complex Numbers # include <stdio.h> typedef struct complex { float real; float imag; } complex ; complex add ( complex n1, complex n2) ; int main () { complex n1, n2, result; printf ( "For 1st complex number \n" ); printf ( "Enter the real and imaginary parts: " ); scanf ( "%f %f" , &n1.real, &n1.imag); printf ( "\nFor 2nd complex number \n" ); printf ( "Enter the real and imaginary parts: " ); scanf ( "%f %f" , &n2.real, &n2.imag); result = add(n1, n2); printf ( "Sum = %.1f + %.1fi" , result.real, result.imag); return 0 ; } complex add ( complex n1, complex n2) { complex temp; temp.real = n1.real + n2.real; temp.imag = n1.imag + n2.imag; return (temp); } Output For 1st complex number Enter the real and imaginary parts: 2.1 -2.3 For 2nd complex number Enter the real and imaginary parts: 5.6 23.2...

C Program to Add Two Distances (in inch-feet system) using Structures

  Program to add two distances in the inch-feet system # include <stdio.h> struct Distance { int feet; float inch; } d1, d2, result; int main () { // take first distance input printf ( "Enter 1st distance\n" ); printf ( "Enter feet: " ); scanf ( "%d" , &d1.feet); printf ( "Enter inch: " ); scanf ( "%f" , &d1.inch); // take second distance input printf ( "\nEnter 2nd distance\n" ); printf ( "Enter feet: " ); scanf ( "%d" , &d2.feet); printf ( "Enter inch: " ); scanf ( "%f" , &d2.inch); // adding distances result.feet = d1.feet + d2.feet; result.inch = d1.inch + d2.inch; // convert inches to feet if greater than 12 while (result.inch >= 12.0 ) { result.inch = result.inch - 12.0 ; ++result.feet; } printf ( "\nSum of distances = %d\'-%.1f\"" , result.f...