0% found this document useful (0 votes)
28 views13 pages

Brijesh 1

The document contains details of 7 programming experiments including writing programs to find factorial using recursion, converting decimal to binary using recursion, evaluating a mathematical series using recursion, defining and using a time structure, creating and displaying a book structure, defining and using a person structure, and reading player details into a structure and displaying by team.
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)
28 views13 pages

Brijesh 1

The document contains details of 7 programming experiments including writing programs to find factorial using recursion, converting decimal to binary using recursion, evaluating a mathematical series using recursion, defining and using a time structure, creating and displaying a book structure, defining and using a person structure, and reading player details into a structure and displaying by team.
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
You are on page 1/ 13

INDEX

CONDUCTION INSTRUCTOR’s
S.NO. NAME OF THE EXPERIMENTS
DATE SIGNATURE
Write a program to find the factorial of a given
1
number using recursion.
WAP to convert decimal numbers into binary using
2
recursion.
WAP to use recursive calls to evaluate F(x) = x –
3
x3/3! + x5/5! – x7/7! + … + xn/n!
Define a structure data type called time_struct
containing three members' integer hours, minutes,
4 and seconds. Develop a program that assigns values
to individual members and displays the time in the
following format : HH:MM:SS.
WAP to create the book's structure with title,
5 author name, publication, and price. Read data of
n books and display them.
Define a structure person that would contain the
person's name, date of joining, and salary using
6
this structure to read this information of 5 people
and print the same on screen.
Using cricket, declare an array player with 50
elements and WAP to read the information about
7 all the 50 players and print a team-wise list
containing players' names with their batting
average.
Practical-01
Objective: Write a program to find the factorial of a given number using recursion.
Code:
#include <stdio.h>
factorial(int n) {
if (n == 0 || n == 1) {
return 1; // Base case: 0! and 1! are both 1
} else {
return n * factorial(n - 1); // Recursive case
}
}

int
mai
n() {
int
num
;

// Input the number


printf("Enter a non-negative integer: ");
scanf("%d", &num);

// Check if the number is non-


negative if (num < 0) {
printf("Please enter a non-negative
integer.\n"); return 1; // Exit with an error code
}
result = factorial(num);

printf("The factorial of %d is: %llu\n", num, result);

return 0;
}

Output:

Brijesh prasad Roll No: 230105093 B. TechIT (1st Semester, Section-K)


School of Computing
Practical-02
Objective: WAP to convert decimal numbers into binary using recursion.

Code:
#include <stdio.h>

// Function to convert decimal to binary using recursion void


decimalToBinary(int n) {
if (n > 0) {
decimalToBinary
(n / 2);
printf("%d", n % 2);
}
}

int main() {
int decimalNumber;

// Input the decimal number


printf("Enter a decimal number: ");
scanf("%d", &decimalNumber);

// Check if the number is non-


negative if (decimalNumber < 0) {
printf("Please enter a non-negative
integer.\n"); return 1; // Exit with an error code
}

// Display the binary equivalent


printf("Binary equivalent: ");
if (decimalNumber == 0) {
printf("0");
} else {
decimalToBinary(decimalNumber);
}

printf("\n");

return 0;
}

Brijesh prasad Roll No: 230105093 B. TechIT (1st Semester, Section-K)


School of Computing
Output:

Brijesh prasad Roll No: 230105093 B. TechIT (1st Semester, Section-K)


School of Computing
Practical-03
Objective: WAP to use recursive calls to evaluate F(x) = x – x3/3! + x5/5! – x7/7!+… +
xn/n!

Code:
#include <stdio.h>
recursion double factorial(int n) { if (n == 0
|| n == 1) {
return 1;
} else {
return n * factorial(n - 1); // Recursive case
}
}
series(double x, int n) {
if (n == 0) {
return x; // Base case: F(x) = x when n = 0
} else {
double term = (n % 2 == 0) ? -1 : 1; // Alternating signs
return term * (pow(x, 2 * n + 1) / factorial(2 * n + 1)) + series(x, n - 1);
}
}n printf("Enter the value of x: "); scanf("%lf", &x); printf("Enter the value of n: ");
scanf("%d", &n);
F(x) printf("F(x) = %.4f\n", series(x,
n));
return 0;
}

Output:

Brijesh prasad Roll No: 230105093 B. TechIT (1st Semester, Section-K)


School of Computing
Practical-04
Objective: Define a structure data type called time_struct containing three members'
integer hours, minutes, and seconds. Develop a program that assigns values to individual
members and displays the time in the following format : HH:MM: SS

Code:
#include <stdio.h>

// Define the structure


data type struct
time_struct {
int hours;
int minutes; int seconds;
};
displayTime(struct time_struct time) {
printf("%02d:%02d:%02d\n", time.hours, time.minutes, time.seconds);
}

int main() {
variable struct time_struct myTime;
scanf("%d", &myTime.hours);

printf("Enter minutes: ");


scanf("%d", &myTime.minutes);

printf("Enter seconds: ");


scanf("%d", &myTime.seconds);

function printf("Time: ");


displayTime(myTime);

return 0;
}

Output:

Brijesh prasad Roll No: 230105093 B. TechIT (1st Semester, Section-K)


School of Computing
Practical-05
Objective: WAP to create the book's structure with title, author name, publication, and
price. Read data of n books and display them.

Code:
#include <stdio.h>
data type struct book {
char
title[100]; char
author[100]; char
publication[100];
float price;
};
int main (){
int n
// Input the number of books
printf("Enter the number of books:
"); scanf("%d", &n);

structures struct book books[n];

book for (int i = 0; i < n;


i++) {
printf("Enter details for book #%d:\n", i + 1);

// Input title
printf("Title: ");
scanf(" %[^\n]s", books[i].title);

// Input author name


printf("Author: ");
scanf(" %[^\n]s", books[i].author);

// Input publication
printf("Publication: ");
scanf(" %[^\n]s", books[i].publication);
scanf("%f", &books[i].price);

printf("\n");
}
printf("Details of the books:\n");
for (int i = 0; i < n; i++)
Brijesh prasad Roll No: 230105093 B. TechIT (1st Semester, Section-K)
School of Computing
{
printf("Book#%d\n",i+1);
printf("Title:%s\n", books[i].title);
printf("Author: %s\n", books[i].author);
printf("Publication:%s\n",books[i].public);
printf("Price: $%.2f\n", books[i].price);
printf("\n");
}
return 0;
}

Output:

Brijesh prasad Roll No: 230105093 B. TechIT (1st Semester, Section-K)


School of Computing
Practical-06
Objective: Define a structure person that would contain the person's name, date of
joining, and salary using this structure to read this information of 5 people and print
the same on screen

Code:
#include <stdio.h>

// Define the structure


data type struct person {
char
name[100]; char
dateOfJoining[20];
float salary;
};

int main() {
// Declare an array of person
structures struct person people[5];

// Input data for each


person for (int i = 0; i <
5; i++) {
printf("Enter details for person #%d:\n", i + 1);

// Input name
printf("Name: ");
scanf(" %[^\n]s", people[i].name);

// Input date of
joining printf("Date of
Joining: ");
scanf(" %[^\n]s", people[i].dateOfJoining);

// Input salary
printf("Salary: $");
scanf("%f", &people[i].salary);

printf("\n");
}

// Display the details of each person

Brijesh prasad Roll No: 230105093 B. TechIT (1st Semester, Section-K)


School of Computing
printf("Details of the people:\n"); for (int i = 0; i < 5; i++) { printf("Person
#%d\n", i + 1); printf("Name:
%s\n", people[i].name);
printf("Date of Joining: %s\n",
people[i].dateOfJoining); printf("Salary:
$%.2f\n", people[i].salary); printf("\n");
}
return 0;
}

Output:

Brijesh prasad Roll No: 230105093 B. TechIT (1st Semester, Section-K)


School of Computing
Brijesh prasad Roll No: 230105093 B. TechIT (1st Semester, Section-K)
School of Computing
Practical-07
Objective: Using cricket, declare an array player with 50 elements and WAP to
read the information about all the 50 players and print a team-wise list
containing players' names with their
batting average.

Code:
#include <stdio.h>
type struct
cricket_player { char
name[100]; float battingAverage; char team[50];
};

int main() {
// Declare an array of cricket_player
structures struct cricket_player players[50];

// Input data for


each player for (int i =
0; i < 50; i++) {
printf("Enter details for player #%d:\n", i + 1);

// Input name
printf("Name: ");
scanf(" %[^\n]s", players[i].name);

// Input batting
average
printf("Batting
Average: ");
scanf("%f", &players[i].battingAverage);

// Input team
printf("Team: ");
scanf(" %[^\n]s", players[i].team);

printf("\n");
Brijesh prasad Roll No: 230105093 B. TechIT (1st Semester, Section-K)
School of Computing
}

// Display the team-wise list with batting


averages printf("Team-wise list with batting
averages:\n");
for (int i = 0; i < 50; i++) {
printf("Player: %s, Batting Average: %.2f, Team: %s\n",
players[i].name, players[i].battingAverage, players[i].team);
}
return 0;
}
Output:

Brijesh prasad Roll No: 230105093 B. TechIT (1st Semester, Section-K)


School of Computing

You might also like