0% found this document useful (0 votes)
3 views6 pages

Structures

The document provides various examples of recursive functions in C, including calculating factorial, Fibonacci numbers, sum of first N numbers, reversing a number, and finding the GCD. It also covers structures in C, detailing how to define and use structures for student and employee details, as well as nested structures. Additionally, it includes examples of arrays of structures for managing multiple records, such as students and employees, along with operations like searching and calculating total salaries.

Uploaded by

yashadahajare32
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Structures

The document provides various examples of recursive functions in C, including calculating factorial, Fibonacci numbers, sum of first N numbers, reversing a number, and finding the GCD. It also covers structures in C, detailing how to define and use structures for student and employee details, as well as nested structures. Additionally, it includes examples of arrays of structures for managing multiple records, such as students and employees, along with operations like searching and calculating total salaries.

Uploaded by

yashadahajare32
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Recursion

1. Factorial using Recursion

#include <stdio.h>

int fact(int n) {
if(n == 0) return 1;
return n * fact(n - 1);
}

int main() {
int n;
scanf("%d", &n);
printf("Factorial = %d", fact(n));
return 0;
}

2. Fibonacci using Recursion

#include <stdio.h>

int fib(int n) {
if(n <= 1) return n;
return fib(n-1) + fib(n-2);
}

int main() {
int n;
scanf("%d", &n);
printf("%d", fib(n));
return 0;
}

3. Sum of first N numbers (Recursion)

#include <stdio.h>

int sum(int n) {
if(n == 0) return 0;
return n + sum(n - 1);
}

int main() {
int n;
scanf("%d", &n);
printf("Sum = %d", sum(n));
return 0;
}
4. Reverse a number using Recursion

#include <stdio.h>

int reverse(int n, int rev) {


if(n == 0) return rev;
return reverse(n/10, rev*10 + n%10);
}

int main() {
int n;
scanf("%d", &n);
printf("Reverse = %d", reverse(n, 0));
return 0;
}

5. GCD using Recursion

#include <stdio.h>

int gcd(int a, int b) {


if(b == 0) return a;
return gcd(b, a % b);
}

int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("GCD = %d", gcd(a, b));
return 0;
}

Structures in C

1. Structure for Student Details

#include <stdio.h>

struct Student {
int roll;
char name[20];
float marks;
};

int main() {
struct Student s;
scanf("%d %s %f", &[Link], [Link], &[Link]);
printf("%d %s %.2f", [Link], [Link], [Link]);
return 0;
}
2. Structure for Employee

#include <stdio.h>

struct Employee {
int id;
float salary;
};

int main() {
struct Employee e;
scanf("%d %f", &[Link], &[Link]);
printf("%d %.2f", [Link], [Link]);
return 0;
}

3. Passing Structure to Function

#include <stdio.h>

struct Point {
int x, y;
};

void print(struct Point p) {


printf("(%d, %d)", p.x, p.y);
}

int main() {
struct Point p = {5, 10};
print(p);
return 0;
}

4. Structure with User Input & Output

#include <stdio.h>

struct Book {
char title[20];
int pages;
};

int main() {
struct Book b;
scanf("%s %d", [Link], &[Link]);
printf("%s %d", [Link], [Link]);
return 0;
}
5. Structure with Nested Structure

#include <stdio.h>

struct Date {
int d,m,y;
};

struct Person {
char name[20];
struct Date dob;
};

int main() {
struct Person p = {"Rahul", {1, 1, 2000}};
printf("%s %d-%d-%d", [Link], [Link].d, [Link].m, [Link].y);
return 0;
}

Array of Structures

1. Array of Students

#include <stdio.h>

struct Student {
int roll;
char name[20];
};

int main() {
struct Student s[3];
for(int i=0;i<3;i++)
scanf("%d %s", &s[i].roll, s[i].name);

for(int i=0;i<3;i++)
printf("%d %s\n", s[i].roll, s[i].name);

return 0;
}
2. Array of Employees

#include <stdio.h>

struct Employee {
int id;
float salary;
};

int main() {
struct Employee e[3];
for(int i=0;i<3;i++)
scanf("%d %f", &e[i].id, &e[i].salary);

for(int i=0;i<3;i++)
printf("%d %.2f\n", e[i].id, e[i].salary);

return 0;
}

3. Highest Marks Among Students

#include <stdio.h>

struct Student {
char name[20];
int marks;
};

int main() {
struct Student s[3];
int max = 0;

for(int i=0;i<3;i++) {
scanf("%s %d", s[i].name, &s[i].marks);
if(s[i].marks > s[max].marks)
max = i;
}

printf("Topper: %s %d", s[max].name, s[max].marks);


return 0;
}
4. Search in Array of Structures

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

struct Item {
int id;
char name[20];
};

int main() {
struct Item it[3];
char key[20];

for(int i=0;i<3;i++)
scanf("%d %s", &it[i].id, it[i].name);

scanf("%s", key);

for(int i=0;i<3;i++)
if(strcmp(it[i].name, key)==0)
printf("Found: %d %s", it[i].id, it[i].name);

return 0;
}

5. Total Salary from Array of Structures

#include <stdio.h>

struct Employee {
char name[20];
float salary;
};

int main() {
struct Employee e[3];
float total = 0;

for(int i=0;i<3;i++) {
scanf("%s %f", e[i].name, &e[i].salary);
total += e[i].salary;
}

printf("Total Salary = %.2f", total);


return 0;
}

You might also like