0% found this document useful (0 votes)
24 views29 pages

Lab Maual FCS

Uploaded by

pranavbhagat946
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)
24 views29 pages

Lab Maual FCS

Uploaded by

pranavbhagat946
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/ 29

Experiment no 1

Introduction to C Programming

Aim:

The aim of this introduction is to understand the basics of C programming, including the general
structure of a C program, how to write and execute a simple "Hello, World!" program, and how
to run it using a text editor.

Theory

C programming is a powerful, general-purpose programming language that is widely used for


system programming, developing operating systems, embedded systems, and software
applications. It was developed by Dennis Ritchie in the early 1970s at Bell Labs. Known for its
efficiency and flexibility, C serves as the foundation for many modern programming languages,
including C++, Java, and Python.

Key Features of C Programming:

1. Structured Language: Supports modular programming and functions.


2. Low-Level Access: Provides direct access to memory, making it suitable for system-level
programming.
3. Portable: Code written in C can run on different platforms with minimal modifications.
4. Rich Library Support: Includes a wide range of built-in functions for tasks like I/O,
string handling, and math operations.

C programming is essential for building efficient and robust applications, offering a balance
between high-level programming capabilities and low-level hardware interaction.

General Structure of a C Program:

A basic C program consists of the following parts:

1. Preprocessor Directives: Lines beginning with # are preprocessor commands used to


include external libraries or define constants.
2. Main Function: The main() function is the starting point of every C program. It
contains the instructions that are executed when the program runs.
3. Variable Declarations: Declares variables used in the program.
4. Statements and Expressions: The actual instructions that carry out operations, such as
calculations or data processing.
5. Return Statement: The return statement in the main() function ends the program and
optionally returns a value to the operating system.
#include <stdio.h> // Preprocessor Directive

// Main function - entry point of the program


int main() {
// Variable declaration (if any)

// Statements and expressions


printf("Hello, World!\n"); // Output statement

return 0; // Exit the program


}
Execution of a C Program in a Text Editor:

To execute a C program, follow these steps:

Writing the Program:

Open a text editor (such as Notepad, Sublime Text, or any IDE like Code::Blocks or Visual
Studio).

Steps to Compile and Run a C Program in Code::Blocks:

1. Open Code::Blocks and create a new Console Application project.


2. Write your C code in main.c (e.g., printf("Hello, World!\n");).
3. Click Build (or press F9) to compile the program.
4. Click Run (or press Ctrl + F10) to execute the program.
5. View the output in the console.

Conclusion

C programming is foundational for system-level programming and applications. By following the basic
structure, you can write, compile, and execute C programs.

#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}
Experiment 02

Program to illustrate data types and operators

Aim:

The aim of this program is to demonstrate the usage of different types of operators in C,
including arithmetic operators, relational operators, logical operators, and assignment
operators.

Theory:

1. Operators in C:

Operators are symbols that perform operations on variables and values. In C, operators are used
to manipulate data and variables. These operators can be classified into several types, each
serving a specific function:

Types of Operators in C:

1. Arithmetic Operators:

These operators are used to perform mathematical calculations such as addition, subtraction,
multiplication, division, and modulus.

Examples:

+ (Addition): Adds two operands.

- (Subtraction): Subtracts the second operand from the first.

* (Multiplication): Multiplies two operands.

/ (Division): Divides the first operand by the second.

% (Modulus): Returns the remainder when the first operand is divided by the second.

Example:

result = a + b; // Adds 'a' and 'b'


result = a - b; // Subtracts 'b' from 'a'
result = a * b; // Multiplies 'a' and 'b'
result = a / b; // Divides 'a' by 'b'
result = a % b; // Returns the remainder of 'a' divided by 'b'
2. Relational Operators:

These operators compare two values and return a boolean value (1 for true, 0 for false). They are
used to evaluate conditions in decision-making structures.

Examples:

> (Greater than): Checks if the left operand is greater than the right operand.

< (Less than): Checks if the left operand is less than the right operand.

== (Equal to): Checks if both operands are equal.

!= (Not equal to): Checks if the operands are not equal.

Example

result = (a > b); // Checks if 'a' is greater than 'b'


result = (a < b); // Checks if 'a' is less than 'b'
result = (a == b); // Checks if 'a' is equal to 'b'
result = (a != b); // Checks if 'a' is not equal to 'b'

3. Logical Operators:

Logical operators are used to perform logical operations between two or more conditions. They
are commonly used in decision-making and control flow statements.

Examples:

&& (Logical AND): Returns true if both conditions are true.

|| (Logical OR): Returns true if at least one condition is true.

! (Logical NOT): Reverses the boolean value of the condition (i.e., converts true to false and vice
versa).

Example:

result = (x && y); // True if both 'x' and 'y' are true
result = (x || y); // True if either 'x' or 'y' is true
result = !x; // Reverses the value of 'x'

Assignment Operators:

Assignment operators are used to assign values to variables. The most basic assignment operator
is the = sign.
Examples:

= (Assignment): Assigns the value of the right operand to the left operand.

+= (Addition Assignment): Adds the right operand to the left operand and assigns the result to
the left operand.

-= (Subtraction Assignment): Subtracts the right operand from the left operand and assigns the
result to the left operand.

*= (Multiplication Assignment): Multiplies the left operand by the right operand and assigns the
result to the left operand.

/= (Division Assignment): Divides the left operand by the right operand and assigns the result to
the left operand.

Example:

c += 3; // Adds 3 to 'c' and assigns the result to 'c'


c -= 2; // Subtracts 2 from 'c' and assigns the result to 'c'
c *= 2; // Multiplies 'c' by 2 and assigns the result to 'c'
c /= 2; // Divides 'c' by 2 and assigns the result to 'c'

3. Conclusion:

This program helps in understanding the fundamental types of operators in C. Operators are
essential for performing calculations, making comparisons, controlling program flow, and
assigning values to variables.

#include <stdio.h>
int main() {
// Declare variables
int a = 10, b = 5;
int result;
// Arithmetic Operators
result = a + b;
printf("Addition: %d + %d = %d\n", a, b, result);
result = a - b;
printf("Subtraction: %d - %d = %d\n", a, b, result);
result = a * b;
printf("Multiplication: %d * %d = %d\n", a, b, result);
result = a / b;
printf("Division: %d / %d = %d\n", a, b, result);
result = a % b;
printf("Modulus: %d %% %d = %d\n", a, b, result);
// Relational Operators
printf("\nRelational Operators:\n");
printf("%d > %d = %d\n", a, b, a > b);
printf("%d < %d = %d\n", a, b, a < b);
printf("%d == %d = %d\n", a, b, a == b);
printf("%d != %d = %d\n", a, b, a != b);
// Logical Operators
int x = 1, y = 0;
printf("\nLogical Operators:\n");
printf("x && y = %d\n", x && y);
printf("x || y = %d\n", x || y);
printf("!x = %d\n", !x);
// Assignment Operators
printf("\nAssignment Operators:\n");
int c = 5;
c += 3; // c = c + 3
printf("c += 3: %d\n", c);
c -= 2; // c = c - 2
printf("c -= 2: %d\n", c);
return 0;
}

Output:
Addition: 10 + 5 = 15
Subtraction: 10 - 5 = 5
Multiplication: 10 * 5 = 50
Division: 10 / 5 = 2
Modulus: 10 % 5 = 0
Relational Operators:
10 > 5 = 1
10 < 5 = 0
10 == 5 = 0
10 != 5 = 1
Logical Operators:
x && y = 0
x || y = 1
!x = 0
Assignment Operators:
c += 3: 8
c -= 2: 6
Experiment 03
Program to illustrate switch case statement

AIM:
To demonstrate the use of the switch statement in C for efficient multi-way branching based on a
variable's value, simplifying decision-making in programs.

Theory:
The switch statement in C is a control structure used to handle multiple possible execution paths
based on the value of an expression or variable. It simplifies the process of decision-making
when there are many potential conditions to evaluate.
 The switch statement allows you to compare a single variable or expression against a
list of predefined constants.
 It is a more readable and efficient alternative to writing multiple if-else statements
when handling numerous conditions.

Syntax:
switch (expression) {
case constant1:
// Code to execute if expression equals constant1
break;

case constant2:
// Code to execute if expression equals constant2
break;

// Add more cases as needed

default:
// Code to execute if no cases match (optional)
break;
}

Expression:

 The expression in a switch statement is evaluated once and compared against the values
specified in the case labels.
 It must result in an integer or a value that can be converted to an integer (e.g., char).

Case Labels:

 Each case defines a potential match for the expression.


 The case label must end with a colon (:) and must be followed by a block of code.

Break Statement:
 The break statement prevents the code from "falling through" to subsequent cases.
 If a break is omitted, execution will continue into the next case (fall-through behavior).

Default Case:

 The default label is optional and provides a catch-all for cases where no other case
matches the expression.
 If omitted and no cases match, the switch block will be skipped.

Working if switch case

 The switch statement evaluates the expression.


 It compares the value of the expression with each case label.
 If a match is found:
 The code block corresponding to that case is executed.
 Execution continues until a break statement is encountered, or the switch block ends.
 If no match is found and a default case is defined, the code inside the default block is
executed.

Conclusion:

The switch statement is a powerful tool for managing multi-way branching in a program. It is
particularly useful when there are multiple conditions based on a single variable or expression.
By ensuring proper use of break and default cases, programmers can create efficient and easy-to-
read decision-making structures

#include <stdio.h> // Include the standard input-output library

int main() {
char operator;
int num1, num2, result;

// Input
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &operator); // Space before %c to consume any leftover newline
character
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

// Perform operation and handle division by zero and invalid operators


if (operator == '/' && num2 == 0) {
printf("Cannot divide by zero!\n");
return 1; // Exit early on division by zero
}
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
printf("Invalid operator!\n");
return 1; // Exit early on invalid operator
}

// Output result
printf("Result: %d %c %d = %d\n", num1, operator, num2, result);

return 0;
}
Experiment No-4

Programs to illustrate conditional statements

Aim:

To write and execute programs that illustrates the use of conditional statements for decision-
making in C programming.

Theory:

Conditional statements are used in programming to make decisions based on certain conditions.
These conditions evaluate to either true or false, and the program executes a specific block of code
depending on the result. Conditional statements add logical flow and allow programs to handle
different situations effectively.

Types of Conditional Statements in C:

1. if Statement:

Executes a block of code only if a specified condition evaluates to true.

Syntax:

if (condition)

// Code to execute if condition is true

if-else Statement:

Provides two possible paths for execution: one if the condition is true and the other if it is false.

Syntax

if (condition) {

// Code to execute if condition is true

} else {

// Code to execute if condition is false


}

Nested if-else Statement:

A nested if-else statement is an if or else statement that is contained inside another if or else
statement. This allows for more complex decision-making by checking additional conditions
within each branch of the logic.

Syntax

if (condition1) {

// Code block to be executed if condition1 is true

if (condition2) {

// Code block to be executed if condition2 is true

} else {

// Code block to be executed if condition2 is false

} else {

// Code block to be executed if condition1 is false

Conclusion

Conditional statements like if, if-else, and nested if-else allow a program to make decisions based on
conditions. They enable the execution of different code blocks depending on whether a condition is true
or false. Nested if-else provides more complex decision-making by allowing multiple layers of conditions
to be checked.

a) // Program to check the given number is positive or negative


#include <stdio.h>

int main() {
int num;

// Input a number
printf("Enter a number: ");
scanf("%d", &num);

// Check if the number is positive or negative


if (num >= 0) {
printf("The number is positive.\n");
} else {
printf("The number is negative.\n");
}

return 0;
}

b) Finding the largest among three number using nested if- else

#include <stdio.h>

int main() {
int a, b, c, largest;

// Input three numbers


printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);

// Nested if-else to find the largest number


if (a >= b) {
if (a >= c) {
largest = a; // a is larger than or equal to both b and c
} else {
largest = c; // c is larger than a and b
}
} else {
if (b >= c) {
largest = b; // b is larger than or equal to both a and c
} else {
largest = c; // c is larger than a and b
}
}
// Output the largest number
printf("The largest number is: %d\n", largest);

return 0;
}
Experiment No 5

Programs to illustrate looping statement

Aim: The aim is to understand and implement entry-controlled and exit-controlled loops,

Theory:

Looping is a fundamental concept in programming that allows repetitive execution of a block of code. It

helps automate repetitive tasks and can handle situations where a set of instructions needs to be executed

multiple times. In C, there are different types of loops: entry-controlled loops and exit-controlled loops,

which determine when the loop condition is checked in relation to the execution of the loop body.

The for loop is used when the number of iterations is known in advance. It is ideal for tasks that require

repetitive actions a fixed number of times. The loop is controlled by an initialization statement, a

condition, and an increment or decrement that updates the loop control variable.

Syntax:

for (initialization; condition; increment/decrement) {

// Code to be executed in each iteration

 Initialization: Sets the initial value of the loop control variable.


 Condition: The loop executes as long as this condition is true.
 Increment/Decrement: Modifies the loop control variable after each iteration.

Nested for Loop


A nested for loop is a for loop inside another for loop. This structure is useful when working with

multi-dimensional data or performing complex tasks that require repetition at multiple levels. The outer

loop controls the main iteration, while the inner loop handles secondary repetitions for each outer

iteration.

Syntax:

for (initialization; condition; increment/decrement) {

for (initialization; condition; increment/decrement) {

// Code to be executed in each iteration of the inner loop

while Loop (Entry-Controlled)

The while loop is another entry-controlled loop where the condition is evaluated before each
iteration.

while (condition) {

// Code to be executed

 The condition is checked before executing the loop body.

 If the condition is false initially, the loop body will not be executed.

do-while Loop
The do-while loop differs from the while loop in that it guarantees at least one execution of the loop

body, regardless of the condition. The condition is checked after the loop body executes, ensuring that

the code inside the loop is always executed at least once. This is useful for tasks like menu-driven

programs or input validation where the task should run at least once.

Syntax

do {

// Code to be executed

} while (condition);

Condition: The loop continues as long as this condition is true. The condition is evaluated after each

execution of the loop body.

Conclusion:

Entry-controlled loops check the condition before executing the loop body, while exit-controlled
loops ensure the body is executed at least once. Both loops are essential for efficient repetitive
tasks in programming.

a) C Program to Print a Pyramid Pattern Using Nested For Loops

#include <stdio.h>

int main() {
int rows;

// Ask the user for the number of rows


printf("Enter the number of rows for the pyramid: ");
scanf("%d", &rows);

// Nested for loop to print the pyramid


for (int i = 1; i <= rows; i++) {
// Print spaces
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
// Print stars
for (int k = 1; k <= (2 * i - 1); k++) {
printf("*");
}
// Move to the next line
printf("\n");
}

return 0;
}

b) C Program to Print Numbers from 1 to 5 Using a Do Loop


#include <stdio.h>

int main() {
int i = 1;

// Using a do-while loop to print numbers from 1 to 5


do {
printf("%d\n", i); // Print the value of i
i++; // Increment i
} while (i <= 5); // Continue looping as long as i is less than or equal to 5

return 0;
}

c) C program that calculates the sum of positive numbers using a do-while loop:

#include <stdio.h>

int main() {
int number, sum = 0;

// Using a do-while loop


do {
printf("Enter a positive number (or 0 to stop): ");
scanf("%d", &number);

// Add the positive number to the sum


if (number > 0) {
sum += number;
}
} while (number != 0); // Continue looping until 0 is entered

// Output the total sum


printf("The total sum is: %d\n", sum);

return 0;
}
Experiment No 6

Programs to illustrate functions and parameter passing

A function is a block of reusable code designed to perform a specific task in a program.


Functions help divide a program into smaller, manageable modules, improving readability,
maintainability, and reusability. Instead of writing the same code repeatedly, functions allow
programmers to define a set of instructions once and use them whenever needed by simply
calling the function.

In C programming, every program starts with a special function called main(), and additional
functions can be user-defined or predefined in libraries like printf() or scanf().

Key Features of Functions:

1. Modularity: Divides a program into smaller logical units.


2. Reusability: Functions can be called multiple times without rewriting code.
3. Ease of Debugging: Errors can be isolated and fixed within specific functions.
4. Parameter Passing: Functions can accept inputs (arguments) to process dynamic data.
5. Return Values: Functions can return results to the calling code.

Types of Functions:

1. Library Functions: Predefined functions like printf(), scanf(), sqrt().


2. User-Defined Functions: Functions created by the programmer to solve specific
problems.

a) Function with No Arguments and No Return Value

Description: The function does not take any input (no arguments) and does not return any value. It
performs a specific task independently.
Syntax:

void functionName() {

// Code to perform the task

b) Function with Arguments and No Return Value

Description: The function takes input arguments but does not return a value. It uses the
arguments to perform a task.
Syntax:
void functionName(dataType arg1, dataType arg2, ...) {

// Code to perform the task using arguments

c) Function with Arguments and a Return Value

 Description: The function takes input arguments and returns a value to the calling
function.
Syntax:

returnType functionName(dataType arg1, dataType arg2, ...) {

// Code to perform the task

return value; // Return a value of the specified return Type

d) Function with No Arguments but a Return Value

returnType functionName() {

// Code to perform the task

return value; // Return a value of the specified returnType

Conclusion:

Understanding different types of functions helps in writing modular, reusable, and efficient code.
Functions with arguments provide flexibility for dynamic operations, while return values
facilitate result-driven processing.

Function with No Arguments and No Return Value


#include <stdio.h>
// Function with no arguments and no return value
void add() {
int a = 5, b = 10;
printf("The sum is: %d\n", a + b);
}
int main() {
add(); // Calling the function
return 0;
}
b) Function with Arguments and No Return Value
#include <stdio.h>
// Function with arguments and no return value
void add(int a, int b) {
printf("The sum is: %d\n", a + b);
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
add(num1, num2); // Calling the function
return 0;
}
c) Function with Arguments and Return Value
#include <stdio.h>
// Function with arguments and return value
#include <stdio.h>
// Function with arguments and return value
int add(int a, int b) {
return a + b;
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
int sum = add(num1, num2); // Calling the function
printf("The sum is: %d\n", sum);
return 0;
}
d) Function with No Arguments but Return Value
#include <stdio.h>
// Function with no arguments but return value
int add() {
int a = 5, b = 10;
return a + b;
}
int main() {
int sum = add(); // Calling the function
printf("The sum is: %d\n", sum);
return 0;
}
Experiment No 7

Programs to illustrate recursion

Aim:

To understand and implement recursive functions in C programming by solving problems where


a function calls itself to achieve repetitive computation, such as calculating factorials

Theory:

Recursion is a programming technique where a function calls itself to solve a smaller instance of
the same problem. A recursive function must have two parts:

1. Base Case: The condition at which the recursion stops. It prevents the function from
making infinite recursive calls.
2. Recursive Case: The part of the function where it calls itself to solve a smaller problem

Syntax:

returnType functionName(parameters) {

if (baseCondition) {

// Base case: Stop recursion

return value;

} else {

// Recursive case: Function calls itself

return functionName(smallerParameters);

Conclusion:

Recursion is a powerful tool that simplifies problems by breaking them into smaller
subproblems. It is most useful for tasks with repetitive or hierarchical structures, such as
factorials, Fibonacci sequences, and tree traversals.
Recursive function to calculate factorial of a number
#include <stdio.h>
// Recursive function to calculate factorial
int factorial(int n) {
if (n == 0 || n == 1) { // Base case
return 1;
}
return n * factorial(n - 1); // Recursive call
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Factorial of %d is %d\n", num, factorial(num));
}
return 0;

}
Experiment No 8

Programs illustrate arrays

Aim;To understand and implement arrays in C programming, focusing on one-dimensional arrays, their
declaration, initialization, and operations.

Theory

A one-dimensional array is a linear collection of elements accessed using a single index. It is


used to store and process data such as marks of students, sales data, or daily temperatures.

 Declaration: Allocates memory for the array.

The general syntax for declaring an array is:

dataType arrayName[size];

 Initialization: Assigns values to the elements of the array.

Initialization During Declaration:

dataType arrayName[size] = {value1, value2, ..., valueN};

Array elements can be accessed or modified using their index:

arrayName[index];

Conclusion:

Arrays are essential for storing and manipulating large collections of similar data efficiently.
One-dimensional arrays are simple yet powerful tools for linear data organization. By
understanding their declaration, initialization, and access methods, programmers can handle
repetitive data storage and processing tasks effectively.

Finding the largest number in a array


#include <stdio.h>
int main() {
int n;
// Get the number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
// Get the elements of the array
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Find the largest number
int largest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
// Output the largest number
printf("The largest number in the array is: %d\n", largest);
return 0;

}
Experiment No 9

Programs to illustrate pointers

Aim:

To understand and implement pointers in C programming, focusing on their declaration,


initialization, and usage for various operations such as swapping of two numbers.

Theory:

A pointer is a variable that stores the memory address of another variable. Pointers are powerful
tools in C programming, allowing direct access and manipulation of memory, efficient array
handling, and dynamic memory allocation.

Key Concepts:

1. Pointer Declaration:
Declares a pointer that can store the address of a variable.
Syntax:

dataType *pointerName;
Example:
int *ptr; // Pointer to an integer

Pointer Initialization:
Assigns the address of a variable to a pointer.
Example:

int x = 10;

int *ptr = &x; // ptr stores the address of x

Dereferencing Pointers:
Access the value at the address stored in a pointer using the dereference operator *:

printf("%d", *p); // Prints the value of x (10)

Conclusion:

Pointers are essential for efficient memory management, allowing programs to directly access
and manipulate memory. They enhance functionality by enabling operations like dynamic
memory allocation, array traversal, and passing values by reference.
Swapping Two Integers Using Pointers
#include <stdio.h>
// Function to swap two integers using pointers
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;
// Taking input
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Printing values before swapping
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
// Swapping the values
swap(&num1, &num2);
// Printing values after swapping
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;

}
Experiment No 10
Program to illustrate structures

Aim:

To understand and demonstrate the concept of structures in C programming, including their


declaration, initialization, and usage for storing and accessing data of different types under one
entity.

Theory:

A structure in C is a user-defined data type that groups variables of different types under a
single name. It is particularly useful for handling complex data, such as records in a database or
attributes of an entity like a student or employee.

Key Concepts of Structures:

1. Declaration of Structures:
Structures are defined using the struct keyword:

struct structureName {
dataType member1;
dataType member2;
...
};

2. Creating Structure Variables:


Once defined, structure variables can be created

struct structureName variableName;

3. Accessing Structure Members:


Use the dot operator (.) to access or modify members:

variableName.memberName;

4. Initialization of Structures:
Structure members can be initialized during declaration:

struct structureName var = {value1, value2, ...};

Conclusion:

Structures in C enable the organization of complex data by grouping related variables of different
types into a single unit. They are essential for managing real-world data, making programs
modular and easier to maintain.
"C Program to Store and Display a Single Student's Information Using Structures"
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int rollNo;
float marks;
};
int main() {
struct Student s1; // Declare structure variable as s1
printf("Enter student's name: ");
scanf("%s", s1.name); // Directly read the name into the structure
printf("Enter student's roll number: ");
scanf("%d", &s1.rollNo);
printf("Enter student's marks: ");
scanf("%f", &s1.marks);
printf("\n Student Information:\n");
printf("Name: %s\n", s1.name);
printf("Roll Number: %d\n", s1.rollNo);
printf("Marks: %.2f\n", s1.marks);
return 0;
}

C Program to Store and Display Multiple Students' Information Using Structures and Arrays"
#include <stdio.h>
#include <string.h>
// Define a structure for student information
struct Student {
char name[50];
int rollNo;
float marks;
};
int main() {
int n;
// Input the number of students
printf("Enter the number of students: ");
scanf("%d", &n);
struct Student s[n]; // Array of structures
// Input details for each student
for (int i = 0; i < n; i++) {
printf("\n Enter details for student %d:\n", i + 1);
printf("Name: ");
scanf("%s", s[i].name); // Directly store the name
printf("Roll Number: ");
scanf("%d", &s[i].rollNo);
printf("Marks: ");
scanf("%f", &s[i].marks);
}
// Display details of all students
printf("\nStudent Information:\n");
for (int i = 0; i < n; i++) {
printf("\n Student %d:\n", i + 1);
printf("Name: %s\n", s[i].name);
printf("Roll Number: %d\n", s[i].rollNo);
printf("Marks: %.2f\n", s[i].marks);
}
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy