C Programming: Data Structures Examples
C Programming: Data Structures Examples
Problem 1
#include <stdio.h>
#include <string.h>
struct Census
{
char cityName[100];
int population;
float literacyLevel;
};
int main()
{
struct Census cities[10];
struct Census temp;
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 2
#include <stdio.h>
#include <string.h>
struct cricket
{
char playerName[100];
char teamName[100];
float battingAverage;
};
int main()
{
struct cricket player[20];
// If the team has not been printed, print the team header
if (!teamPrinted)
{
printf("\nTeam: %s\n", player[i].teamName);
// Now print the players for that team
for (int j = 0; j < 20; j++)
{
if (strcmp(player[i].teamName, player[j]×teamName) == 0)
{
printf("Player: %s | Batting Average: %.2f\n", player[j].playerName,
player[j].battingAverage);
}
}
}
}
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 3
#include <stdio.h>
#include <string.h>
struct hotel
{
char name[100];
char address[200];
int grade;
float avgRoomCharge;
int numRooms;
};
int main()
{
struct hotel hotels[10];
int n;
printf("Name: ");
scanf("%s", hotels[i].name); // Reads a single word, no spaces allowed
printf("Address: ");
scanf("%s", hotels[i].address); // Reads a single word, no spaces allowed
printf("Grade: ");
scanf("%d", &hotels[i].grade);
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 4
#include <stdio.h>
#include <string.h>
#define MAX 5
while (1)
{
printf("\nLibrary Menu:\n");
printf("1. Add book information\n");
printf("2. Display book information\n");
printf("3. List all books of given author\n");
printf("4. List the title of specified book\n");
printf("5. List total no. of books in the library\n");
printf("6. List the books in the order of accession number\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
if (count < MAX)
{
printf("Enter accession number: ");
scanf("%d", &books[count].accessionNumber);
printf("Enter title: ");
scanf("%s", books[count].title);
printf("Enter author: ");
scanf("%s", books[count].author);
printf("Enter price: ");
scanf("%f", &books[count].price);
printf("Is the book issued? (1 for Yes, 0 for No): ");
scanf("%d", &books[count].isIssued);
count++;
}
else
{
printf("Library is full!\n");
}
break;
case 2:
for (i = 0; i < count; i++)
{
printf("\nAccession Number: %d", books[i].accessionNumber);
printf("\nTitle: %s", books[i].title);
printf("\nAuthor: %s", books[i].author);
printf("\nPrice: %.2f", books[i].price);
printf("\nIssued: %s\n", books[i].isIssued ? "Yes" : "No");
}
break;
case 3:
printf("Enter author name: ");
scanf("%s", author);
for (i = 0; i < count; i++)
{
if (strcmp(books[i].author, author) == 0)
{
printf("\nTitle: %s", books[i].title);
printf("\nAccession Number: %d\n", books[i].accessionNumber);
}
}
break;
case 4:
printf("Enter accession number: ");
scanf("%d", &accessionNumber);
for (i = 0; i < count; i++)
{
if (books[i]×accessionNumber == accessionNumber)
{
printf("\nTitle: %s\n", books[i].title);
break;
}
}
if (i == count)
{
printf("Book not found.\n");
}
break;
case 5:
printf("Total number of books in the library: %d\n", count);
break;
case 6:
for (i = 0; i < count - 1; i++)
{
for (j = i + 1; j < count; j++)
{
if (books[i].accessionNumber > books[j].accessionNumber)
{
struct library temp = books[i];
books[i] = books[j];
books[j] = temp;
}
}
}
for (i = 0; i < count; i++)
{
printf("\nAccession Number: %d", books[i].accessionNumber);
printf("\nTitle: %s", books[i].title);
printf("\nAuthor: %s", books[i].author);
printf("\nPrice: %.2f", books[i].price);
printf("\nIssued: %s\n", books[i].isIssued ? "Yes" : "No");
}
break;
case 7:
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 5
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (leap(year) == 0)
{
printf("%d is a leap year.\n", year);
}
else
{
printf("%d is not a leap year.\n", year);
}
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 6
#include <stdio.h>
int sumOfDigits(int n) {
int sum = 0;
if (n < 0) {
n = 0 - n;
}
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
int main() {
int number;
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 7
#include <stdio.h>
int isPrime(int n)
{
if (n <= 1)
{
return 0;
}
else if (n == 2)
{
return 1;
}
else
{
if (n % 2 == 0)
return 0;
for (int i = 3; i * i <= n; i += 2)
{
if (n % i == 0)
{
return 0;
}
}
}
return 1;
}
int main()
{
int number;
if (isPrime(number))
{
printf("%d is a prime number.\n", number);
}
else
{
printf("%d is not a prime number.\n", number);
}
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 8
#include <stdio.h>
tempNumber = number;
while (tempNumber != 0)
{
int digit = tempNumber % 10;
sumOfPowers = sumOfPowers + calculatePower(digit, digitCount);
tempNumber = tempNumber / 10;
}
if (sumOfPowers == originalNumber)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int number;
if (isArmstrongNumber(number))
{
printf("%d is an Armstrong number.\n", number);
}
else
{
printf("%d is not an Armstrong number.\n", number);
}
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 9
#include <stdio.h>
int factorial(int n)
{
int fact = 1;
if (n == 0 || n == 1)
{
return 1;
}
return fact;
}
int main()
{
int number;
if (number < 0)
{
printf("Factorial is not defined for negative numbers.\n");
}
else
{
printf("Factorial of %d is %d\n", number, factorial(number));
}
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 10
#include <stdio.h>
int main() {
int array[] = {64, 25, 12, 22, 11};
int size = sizeof(array) / sizeof(array[0]);
sortAscending(array, size);
printf("Array in ascending order: ");
printArray(array, size);
sortDescending(array, size);
printf("Array in descending order: ");
printArray(array, size);
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 11
#include <stdio.h>
k = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arr[i][j] = temp[k++];
}
}
}
k = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arr[i][j] = temp[k++];
}
}
}
int main() {
int rows, cols;
int arr[rows][cols];
printf("\nOriginal Array:\n");
print2DArray(rows, cols, arr);
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 12
#include <stdio.h>
int main() {
int size;
int arr[size];
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 13
#include <stdio.h>
void findLargestSmallest(int rows, int cols, int arr[rows][cols], int *largest, int *smallest) {
*largest = arr[0][0];
*smallest = arr[0][0];
int main() {
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int arr[rows][cols];
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 14
#include <stdio.h>
typedef struct {
int day;
int month;
int year;
} Date;
void calculateAge(Date birth, Date current, int *years, int *months, int *days) {
*years = [Link] - [Link];
*months = [Link] - [Link];
*days = [Link] - [Link];
if (*days < 0) {
*months -= 1;
*days += 30;
}
if (*months < 0) {
*years -= 1;
*months += 12;
}
}
int main() {
Date birth, current;
int years, months, days;
printf("Your age is: %d years, %d months, and %d days\n", years, months, days);
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 15
#include <stdio.h>
return 0;
}
int main() {
int day, month, year;
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 16
#include <stdio.h>
struct Date
{
int day;
int month;
int year;
};
int main()
{
struct Date date1, date2;
if (compareDates(date1, date2) == 0)
{
printf("Dates are equal.\n");
}
else
{
printf("Dates are different.\n");
}
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 17
#include <stdio.h>
struct Time
{
int hour;
int minute;
int second;
};
int main()
{
struct Time t;
Input(&t);
if (Validate(t) == 0)
{
Display(t);
}
else
{
printf("Invalid time entered.\n");
}
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 18
#include <stdio.h>
int main()
{
int rows, cols;
int arr[rows][cols];
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 19
#include <stdio.h>
void fibonacci(int n)
{
int a = 0, b = 1, next;
printf("\n");
}
int main()
{
int n;
if (n <= 0)
{
printf("Please enter a positive number.\n");
}
else
{
fibonacci(n);
}
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 20
#include <stdio.h>
int sumOfDigits(int n) {
int sum = 0;
if (n < 0) {
n = 0 - n;
}
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
int main() {
int number;
int main()
{
int rows, cols;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
int arr[rows][cols];
printf("Enter elements of the array:\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("Element [%d][%d]: ", i, j);
scanf("%d", &arr[i][j]);
}
}
int isPrime(int n)
{
if (n <= 1)
{
return 0;
}
else if (n == 2)
{
return 1;
}
else
{
if (n % 2 == 0)
return 0;
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (isPrime(number))
{
printf("%d is a prime number.\n", number);
}
else
{
printf("%d is not a prime number.\n", number);
}
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 23
#include <stdio.h>
tempNumber = number;
while (tempNumber != 0)
{
int digit = tempNumber % 10;
sumOfPowers = sumOfPowers + calculatePower(digit, digitCount);
tempNumber = tempNumber / 10;
}
if (sumOfPowers == originalNumber)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int number;
if (isArmstrongNumber(number))
{
printf("%d is an Armstrong number.\n", number);
}
else
{
printf("%d is not an Armstrong number.\n", number);
}
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 24
#include <stdio.h>
void printPattern(int n)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - i; j++)
{
printf(" ");
}
for (int k = 1; k <= 2 * i - 1; k++)
{
printf("*");
}
printf("\n");
}
}
int main()
{
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
printPattern(n);
return 0;
}
-----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
problem 25
#include <stdio.h>
void printPattern(int n)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - i; j++)
{
printf(" ");
}
for (int k = 1; k <= 2 * i - 1; k++)
{
printf("%d", k);
}
printf("\n");
}
}
int main()
{
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
printPattern(n);
return 0;
}
-----------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
problem 26
#include<stdio.h>
int fact(int);
int fact(int n)
{
printf("\nFactorial of %d is called",n);
if(n==0)
{
return 0;
}
else if(n==1)
{
return 1;
}
else
{
return n*fact(n-1);
}
}
int main()
{
int n,result;
printf("Enter n : ");
scanf("%d",&n);
result=fact(n);
printf("\n\nFactorial of %d = %d",n,result);
return 0;
}
-----------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
problem 27
#include<stdio.h>
int fibo(int n);
int fibo(int n)
{
if(n==0)
{
return 0;
}
else if(n==1)
{
return 1;
}
else
{
return fibo(n-1)+fibo(n-2);
}
}
int main()
{
int n;
printf("Enter n : ");
scanf("%d",&n);
-----------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
problem 28
#include<stdio.h>
int multiply(int,int);
int main()
{
int a,b,product;
printf("Enter Any two Integers : ");
scanf("%d %d",&a,&b);
product=multiply(a,b);
-----------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
problem 29
#include<stdio.h>
int add(int);
int add(int n)
{
if(n<=0)
{
return 0;
}
else
{
return n+add(n-1);
}
}
int main()
{
int n,res;
printf("Enter n : ");
scanf("%d",&n);
res=add(n);
-----------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
problem 30
#include<stdio.h>
int power(int,int);
printf("\n%d^%d = %d",n,p,power(n,p));
return 0;
}