Q.1 Define Algorithm and list the advantage of algorithm.

English:

An algorithm is like a recipe or a set of instructions that tells a computer exactly what steps to take to solve a particular problem. It's like a cooking recipe that guides you step by step to make a delicious dish.

Advantages of Algorithms:

Clear Instructions (સ્પષ્ટ માર્ગદર્શન): Algorithms provide clear and precise instructions, making it easy for a computer to follow the steps.

Efficiency (કુશળતા): They help in finding the most efficient way to solve a problem, like finding the shortest route on a map.

Consistency (સતતતા): Algorithms give the same result for the same input every time, ensuring consistency.

Reusability (પુનરપ્રયોગ): Once you have a good algorithm, you can use it again for similar problems without starting from scratch.

Problem Solving (સમસ્યાઓનો સમાધાન): They help break down complex problems into smaller, more manageable steps, making it easier to solve them.

Gujarati (ગુજરાતી):

એલ્ગોરિદમ એવું છે જેમ કે વાનગીનો રેસિપી કે આદેશનો સેટ, જે કમ્પ્યુટરને કેટલાક પ્રશ્નનો સમાધાન કરવાની સુચનાઓ આપે છે. આ તેમ છે જેમ કે વાનગીની રેસિપી, જે તમને એક અવસર ડિશ બનાવવાની સ્થાને એક એકમાં માર્ગદર્શન આપે છે.

એલ્ગોરિદમના લાભ:

સ્પષ્ટ આદેશ (Clear Instructions - સ્પષ્ટ માર્ગદર્શન): એલ્ગોરિદમ્સ સ્પષ્ટ અને મર્ગદર્શક આદેશો આપે છે, જે કે કમ્પ્યુટરને તરીકે આવશે.

કુશળતા (Efficiency - કુશળતા): તે સહાય કરે છે કે કેવી રીતે સમસ્યાનો સમાધાન કરવો, જેવું કે નક્કી રાસ્તો શોધવો.

સતતતા (Consistency - સતતતા): સમાન ઇનપુટ માટે એલ્ગોરિદમને દર વખત એક સમાન પરિણામ આપવામાં મદદ કરે છે, જે સતતતા નક્કી રાખે છે.

પુનરપ્રયોગ (Reusability - પુનરપ્રયોગ): એક સારો એલ્ગોરિદમ હોવાથી, તમે તેનો પુનરપ્રયોગ કરી શકો છો, સમાન પ્રશ્નો માટે નવાં શોધવાનો તમને શરૂઆત કરવાના અંત થવાથી.

સમસ્યાઓનો સમાધાન (Problem Solving - સમસ્યાઓનો સમાધાન): તે મોટા સમસ્યાઓને છોટા, સરળ પગલામાં વાંચવામાં મદદ કરે છે.


Q.2 What is an algorithm? List out types of Algorithms.

English:

An algorithm is a step-by-step procedure or set of instructions designed to perform a specific task or solve a particular problem. It's like a recipe that tells you exactly what ingredients to use and what steps to follow to cook a dish.

Types of Algorithms:

Sorting Algorithms: Arrange a list of items in a specific order, like alphabetical or numerical.

Searching Algorithms: Find the location or presence of a particular item in a collection of data.

Recursive Algorithms: Solve a problem by breaking it down into smaller instances of the same problem.

Greedy Algorithms: Make locally optimal choices at each stage to find a global optimum.

Dynamic Programming Algorithms: Solve complex problems by breaking them into simpler overlapping subproblems.

Divide and Conquer Algorithms: Break a problem into smaller sub-problems, solve them, and then combine their solutions.

Brute Force Algorithms: Consider all possible solutions and pick the best one.

Randomized Algorithms: Introduce randomness to solve problems, useful in situations where a deterministic solution is difficult.

Backtracking Algorithms: Systematically explore all possible solutions and backtrack when reaching an incorrect solution.

Graph Algorithms: Solve problems related to graphs, which represent connections between different elements.

Gujarati (ગુજરાતી):

એક એલ્ગોરિદમ એવું છે જેમ કે વિશિષ્ટ કાર્ય કરવા માટે અથવા ખાસ સમસ્યાને સોલ્યૂશન કરવા માટે ડિઝાઇન કરેલું કદ. એવું છે જેમ કે એક રેસિપી, જે તમને બિલકુલ તમારી વાનગીને બનાવવાનું માટે કયા સામગ્રીઓ વાપરવાનું અને કેટલાક પગલામાં કેટલાક માર્ગદર્શનો છે.

એલ્ગોરિદમના પ્રકાર:

સોર્ટિંગ એલ્ગોરિદમ્સ: વસ્તુઓને વિશિષ્ટ ક્રમમાં વ્યવસ્થિત કરવા, જેવાં કે અક્ષરમાં અથવા આંકડાત્મક.

સર્ચિંગ એલ્ગોરિદમ્સ: ડેટાના સંગ્રહમાં ખાસ વસ્તુના સ્થાન અથવા હાજરી કરવા.

રિકર્સિવ એલ્ગોરિદમ્સ: એક સમસ્યાને તેના છોટા છોટા વસ્તુઓમાં ઘટકાંતર કરીને સોલ્યૂશન કરવા.

ગ્રીડી એલ્ગોરિદમ્સ: પ્રતિ ચરણે સ્થાનિક ઓપ્ટિમમ માટે યોજના બનાવવાનો પ્રયાસ કરે છે.

ડાયનામિક પ્રોગ્રામિંગ એલ્ગોરિદમ્સ: તેવી જટિલ સમસ્યાઓને સરળ અવરગર સબ-સમસ્યાઓમાં વાટ કરીને સોલ્યૂશન કરે છે.

ડિવાઇડ એન્ડ કન્કર એલ્ગોરિદમ્સ: સમસ્યાને છોટા સબ-સમસ્યાઓમાં વાટ કરીને તેમને હળવાવામાં મદદ કરે છે.

બ્ર્યુટ ફોર્સ એલ્ગોરિદમ્સ: તમામ સંભાવના સોલ્યૂશન્સ ગણાવવાનો પ્રયાસ કરે છે અને શ્રેષ્ઠ પસંદગી કરે છે.

રેન્ડમાઇઝ્ડ એલ્ગોરિદમ્સ: સમસ્યાઓ સોલ્યૂશન માટે વિકલ્પતા પ્રવૃત્તિઓ પ્રવેશ કરવામાં મદદ કરવામાં આવતી છે.

બેકટ્રેકિંગ એલ્ગોરિદમ્સ: તમામ સંભાવના સોલ્યૂશન્સ સિસ્ટમેટિકલી અને અમુક ખોટી સોલ્યૂશન પર પહોંચવામાં મદદ કરે છે.

ગ્રાફ એલ્ગોરિદમ્સ: ગ્રાફ સંબંધિત સમસ્યાઓના સમાધાન માટે પ્રયાસરૂપે, જે વિવિધ તત્વો વચ્ચેના કનેક્શન્સ દર્શાવે છે.


Q.3 Write an algorithm to find out the factorial of a given number.

Algorithm to Find Factorial:

Step 1. Input: Read a number 'n' for which factorial needs to be calculated.

Step 2. Initialize: Set factorial to 1.

Step 3. Loop:

   a. For i = 1 to n:

      - factorial = factorial * i

Step 4. Output: Display the value of factorial as the result.


Q.4 Write an algorithm to find out whether the given number is odd or even

Algorithm to Check Odd or Even:

Step 1. Input: Read a number 'num' for which odd or even needs to be determined.

Step 2. Check:
   a. If (num % 2) equals 0:
      - Output: Display "Even."
   b. Else:
      - Output: Display "Odd."

Step 3. End: 
   - End the algorithm.

Q.5 Explain Basic structure of C program

The basic structure of a C program consists of a series of components that organize the code and define its functionality. Here's an overview of the basic structure:

Documentation Section (Optional):
Comments and documentation to describe the purpose of the program, author information, and other relevant details.

Header Files:
Includes preprocessor directives that import necessary header files providing essential functions and definitions for the program.

Main Function:
Every C program must have a main() function. It serves as the entry point for the execution of the program.

Declaration Section:
Declarations for variables and functions are placed here. It specifies the data types and names of variables and functions used in the program.

Executable Statements:
Actual instructions or statements that perform the desired operations. These statements are executed in a sequence defined by the program logic.

Return Statement:
The return statement is used to terminate the main() function. It returns a value to the operating system indicating the exit status of the program.

Example:
// Documentation Section
// Author: Your Name
// Purpose: Brief description of the program

// Header Files
#include <stdio.h>  // Standard Input/Output functions

// Main Function
int main() {
    // Declaration Section
    int number;

    // Executable Statements
    printf("Enter a number: ");
    scanf("%d", &number);
    
    if (number % 2 == 0) {
        printf("The number is even.\n");
    } else {
        printf("The number is odd.\n");
    }

    // Return Statement
    return 0;
}


Q.6 Give the advantages of ‘C’ language
Easy to Learn: C is like the friendly language next door. It's not too hard to understand, making it a good starting point for beginners.

Foundation for Other Languages: Learning C is like learning the alphabet. Once you know it well, picking up other languages becomes easier, like solving puzzles with familiar pieces.

Widely Used: Many cool things like computer games, apps, and even the software running your microwave are made using C. It's everywhere!

Efficient and Fast: C is like a superhero for your computer. It helps make programs run really fast and efficiently, which means less waiting time for you.

Close to Hardware: It's like speaking the computer's language. C allows you to get close and personal with how computers work, making you feel like a tech wizard.

Community Support: Imagine having lots of friends who know C and can help you out when you're stuck. The C community is like a friendly gang always ready to assist.

Versatile: C is like a multi-tool. You can use it for many different things, from making games to controlling robots. It's a language with many talents!

Portable: C programs can be like nomads, moving from one computer to another without much hassle. It's like having a suitcase that fits everywhere.

Structured Programming: C encourages you to be organized and write neat code. It's like having a tidy room where you can find things easily.

Career Boost: Knowing C is like having a superpower on your resume. Many tech companies love people who know C, and it opens doors to exciting jobs in the future.

Q.7 Define Token with an example.
Smallest individual parts of program is call tokens.
Tokens can include following things.

Keywords: These are reserved words that have special meanings in C. Examples include int, float, if, for, and while.

Identifiers: These are names given to various program elements such as variables, functions, and arrays. An identifier must begin with a letter or an underscore, followed by letters, digits, or underscores. For example, sum, total_amount, and _counter are identifiers.

Constants: These are fixed values that do not change during the program execution. Constants can be of various types, such as integer constants (123), floating-point constants (3.14), and character constants ('A').

String Literals: These are sequences of characters enclosed in double quotes. For instance, "Hello, World!" is a string literal.

Operators: These are symbols that represent computations or operations. Examples include +, -, *, /, and %.

Punctuation Symbols: These are special symbols used to structure the code. Common examples are {, }, ;, ,, and ().

Comments: These are not part of the executable code but provide information to the programmer. In C, comments can be single-line (// This is a comment) or multi-line (/* This is a multi-line comment */).

Example:
#include <stdio.h>

int main() {
    // This is a simple C program
    int num1 = 10;
    int num2 = 5;
    int sum;

    sum = num1 + num2;

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

    return 0;
}

In this example:

Keywords: #include, int, return.

Identifiers: main, num1, num2, sum.

Constants: 10, 5, 0 (implicitly used in return 0).

String Literals: "The sum is: %d\n".

Operators: =, +, ;, (), {}, ,.

Punctuation Symbols: #, <, >, \n.

Comments: // This is a simple C program.

Q.8 Define Identifier with example.

An identifier in C is like a name you give to something in your program, like a variable or a function. It follows some rules:

Start with a letter or an underscore (_).
Then, use letters, numbers, or underscores.
No spaces or special symbols.

Example
int myNumber;   // 'myNumber' is an identifier for a variable
void printMessage() {  // 'printMessage' is an identifier for a function
    // Code goes here
}

So, an identifier is like a nickname for parts of your C program, helping you and the computer understand what each part does.

Q.9 What is a variable? Write rules for naming a variable.

A variable in C is like a container or a storage box that holds some information or data. It has a name (called an identifier), a type (like integer or character), and a value (the actual data it stores).
In other words we can say that a variable is a storage location which we can give a name.

Rules for Naming a Variable in C:
Start with a letter or an underscore (_): The name of a variable should begin with a letter (uppercase or lowercase) or an underscore.
int age;    // 'age' is a valid variable name
float _price;  // '_price' is a valid variable name

After the initial letter or underscore, use letters, digits, or underscores: You can continue the name using letters (uppercase or lowercase), digits (0-9), or underscores.
int studentCount;   // 'studentCount' is a valid variable name
double temperature_1;  // 'temperature_1' is a valid variable name

No spaces or special symbols are allowed: Variable names cannot have spaces or symbols (except underscores). Stick to simple characters and numbers.
// Invalid variable names:
// int my age;   // contains space
// float total$;  // contains a special symbol

Avoid using C keywords: Don't use words that are reserved for C language operations (like 'int', 'float', 'if', 'while') as variable names.
// Invalid variable name:
// int int;   // using a reserved keyword

Be meaningful and descriptive: Choose names that make sense and describe the purpose of the variable. This helps in understanding the code.
float temperature;   // 'temperature' is a meaningful variable name


Q.10 List out data types with their range of values used in ‘C’.

In C language, a data type is like a label that tells the computer what kind of information we want to store in a variable. 

data types help us define the kind of data a variable can hold. Each data type has a specific range of values it can represent. Here are some commonly used data types in C along with their range of values:

int (Integer):
Range: -2,147,483,648 to 2,147,483,647
Example: int myNumber = 42;

float (Floating-point):
Range: Approximately ±3.4E38 (7 decimal digits precision)
Example: float myFloat = 3.14;

double (Double Precision Floating-point):
Range: Approximately ±1.7E308 (15 decimal digits precision)
Example: double myDouble = 3.14159265358979;

char (Character):
Range: -128 to 127 or 0 to 255 (if unsigned)
Example: char myChar = 'A';

short (Short Integer):
Range: -32,768 to 32,767
Example: short myShort = 1000;

long (Long Integer):
Range: -2,147,483,648 to 2,147,483,647
Example: long myLong = 1234567890;

unsigned int (Unsigned Integer):
Range: 0 to 4,294,967,295
Example: unsigned int myUnsigned = 100;

unsigned char (Unsigned Character):
Range: 0 to 255
Example: unsigned char myUnsignedChar = 'B';

Q.11 What is a volatile variable and Enumerated data types? Explain with example

Volatile Variable:
In C, the volatile keyword is used to declare a variable as "volatile." This tells the compiler that the variable's value can be changed at any time, even if it doesn't seem obvious in the code. It's typically used when the value of a variable can be changed by something external, like hardware.

#include <stdio.h>
int main() {
    volatile int sensorValue;  // Declare a volatile variable

    while (1) {
        // Code to read sensor value (external change)
        sensorValue = /* some function to read sensor */;
        
        // Use the sensor value in the program
        printf("Sensor Value: %d\n", sensorValue);
    }

    return 0;
}

Enumerated Data Types:
Enumerated data types (enum) in C allow you to create a set of named integer constants. It's like making a list of values that represent something meaningful in your program. Enums provide a way to make your code more readable and maintainable.

#include <stdio.h>
// Define an enumeration named 'Weekdays'
enum Weekdays {
    MONDAY,    // 0
    TUESDAY,   // 1
    WEDNESDAY, // 2
    THURSDAY,  // 3
    FRIDAY,    // 4
    SATURDAY,  // 5
    SUNDAY     // 6
};

int main() {
    enum Weekdays today = TUESDAY;  // Declare a variable of enum type

    printf("Today is day number: %d\n", today);

    return 0;
}

Q.12 List out categories of operators in C language. Explain each in detail and with example.

In C language, operators are symbols that perform operations on variables and values. There are several categories of operators:

Arithmetic Operators:
Description: Perform basic mathematical operations.
Example:
int a = 5, b = 2;
int sum = a + b;   // Addition
int diff = a - b;  // Subtraction
int product = a * b;  // Multiplication
int quotient = a / b;  // Division
int remainder = a % b;  // Modulo (remainder after division)

Relational Operators:
Description: Compare values and return a boolean result (true or false).
Examples:
int x = 5, y = 10;
printf("%d\n", x == y);  // Equal to
printf("%d\n", x != y);  // Not equal to
printf("%d\n", x < y);   // Less than
printf("%d\n", x > y);   // Greater than
printf("%d\n", x <= y);  // Less than or equal to
printf("%d\n", x >= y);  // Greater than or equal to

Logical Operators:
Description: Combine multiple conditions and return a boolean result.
Examples:
int p = 1, q = 0;
printf("%d\n", p && q);  // Logical AND
printf("%d\n", p || q);  // Logical OR
printf("%d\n", !p);      // Logical NOT

Assignment Operators:
Description: Assign values to variables and perform operations at the same time.
Examples:
int num = 10;
num += 5;  // Equivalent to num = num + 5
num -= 3;  // Equivalent to num = num - 3
num *= 2;  // Equivalent to num = num * 2

Increment and Decrement Operators:
Description: Increase or decrease the value of a variable by 1.
Examples:
int counter = 5;
counter++;   // Increment by 1 (counter = counter + 1)
counter--;   // Decrement by 1 (counter = counter - 1)

Bitwise Operators:
Description: Perform operations on individual bits of binary numbers.
Examples:
int a = 5, b = 3;
int result = a & b;  // Bitwise AND
result = a | b;      // Bitwise OR
result = a ^ b;      // Bitwise XOR

Conditional (Ternary) Operator:
Description: A shorthand way of writing an if-else statement.
Example:
int x = 10, y = 20;
int max = (x > y) ? x : y;  // If x > y, max = x; otherwise, max = y;


Q.13 Explain explicit type conversion and implicit type conversion with example

Explicit Type Conversion:

Explicit type conversion is when you, the programmer, clearly tell the computer to change a variable from one type to another. It's like saying, "Hey computer, treat this like that!"
Example:
int num1 = 10;
float num2 = 3.14;
float sum = (float)num1 + num2;  // Convert 'num1' to float before adding

In this example, we explicitly convert num1 from an integer to a float by writing (float)num1. This helps the computer add it to num2 without getting confused.

Implicit Type Conversion:

Implicit type conversion happens automatically by the computer. It's like the computer being smart enough to figure out that it can mix different types without you telling it every time.
Example:
int num1 = 10;
float num2 = 3.14;
float sum = num1 + num2;  // Automatic conversion of 'num1' to float before adding

Here, the computer sees that you are adding an integer (num1) and a float (num2). It does the conversion on its own, making num1 a float and then adding them together.


Q.14 Explain Precedence of various operators in C

Hierarchy or a ranking system for operators, telling the computer which ones to evaluate first.

Parentheses (): The boss! Whatever is inside parentheses gets done first.
int result = (3 + 5) * 2;  // First, add 3 and 5, then multiply by 2

Multiplication (*), Division (/), and Modulo (%): These operators come next. They're like the team leaders.
int result = 6 + 4 * 3;  // First, multiply 4 and 3, then add 6

Addition (+) and Subtraction (-): These operators are like the last ones to get the job done.
int result = 5 - 2 + 1;  // First, subtract 2 from 5, then add 1

Assignment (=): Finally, the equal sign assigns values. It's like giving the result to a variable.
int result = 10;  // Assign the value 10 to the variable 'result'


Q.15 Write Syntax of printf and scanf with example.

printf Syntax:
  printf("format_string", variable1, variable2, ...);
  
printf is used to print formatted output to the console.
"format_string" is a string that specifies the format of the output.
variable1, variable2, ... are the values to be inserted into the format string. 

Example
#include <stdio.h>
void main() {
    int num = 42;
    float pi = 3.14;

    printf("The number is %d and the value of pi is %.2f\n", num, pi);

}

scanf Syntax:
 scanf("format_specifier", &variable1, &variable2, ...);
 
scanf is used to read input from the user.
"format_specifier" is a string that specifies the expected format of the input.
&variable1, &variable2, ... are the addresses of variables where the input values will be stored.

Example:
#include <stdio.h>

void main() {
    int age;
    float height;

    printf("Enter your age: ");
    scanf("%d", &age);

    printf("Enter your height in meters: ");
    scanf("%f", &height);

    printf("You are %d years old and %.2f meters tall\n", age, height);

}
Output:
Input: 25 (for age) and 1.75 (for height)
Output: You are 25 years old and 1.75 meters tall


Q.16 What is the use of putchar() and getchar()?

In C language, putchar() and getchar() are functions used for handling single characters in input and output. Let's break down their uses:

putchar()
Purpose: It is used to write a single character to the standard output (usually the console).

Syntex:
putchar(character);

Example:
#include <stdio.h>
int main() {
    putchar('H');
    putchar('i');
    putchar('\n');  // To print a newline character
    return 0;
}

getchar()
Purpose: It is used to read a single character from the standard input (usually typed by the user).

Syntex:
character = getchar();

Example:
#include <stdio.h>

int main() {
    int inputChar;
    printf("Type a character: ");
    inputChar = getchar();
    printf("You typed: ");
    putchar(inputChar);
    return 0;
}


Q.17 Give the list of conditional branching statements in ‘C’ language.

In C language, conditional branching statements are used to make decisions in the code, determining which set of instructions to execute based on a condition. Here is a list of conditional branching statements in C:

if statement:
if (condition) {
    // Code to be executed if the condition is true
}

if-else statement:
if (condition) {
    // Code to be executed if the condition is true
} else {
    // Code to be executed if the condition is false
}

if-else if-else statement:
if (condition1) {
    // Code to be executed if condition1 is true
} else if (condition2) {
    // Code to be executed if condition2 is true
} else {
    // Code to be executed if none of the conditions is true
}

switch statement:
switch (expression) {
    case value1:
        // Code to be executed if expression equals value1
        break;
    case value2:
        // Code to be executed if expression equals value2
        break;
    // More cases as needed
    default:
        // Code to be executed if expression doesn't match any case
}

These conditional statements help control the flow of the program based on the evaluation of certain conditions, allowing for more dynamic and responsive code execution.

Q.18 Explain goto statements with example

The goto statement in C allows you to transfer control to a labeled statement within the same function. While it can be a powerful tool, it's generally considered bad practice because it can make the code less readable and harder to maintain. In most cases, other control flow structures like if, else, while, and for are preferred.

Exaple:
goto label;
// Some code
label:
// Code to be executed after the goto statement

Here is a simple Example
#include <stdio.h>
int main() {
    int num = 1;

    // Using goto to jump to a labeled statement
    goto check;

    // This code won't be executed because of the goto statement
    printf("This won't be printed\n");

check:
    if (num == 1) {
        printf("The number is 1\n");
    } else {
        printf("The number is not 1\n");
    }

    return 0;
}

In this example, the goto check; statement jumps directly to the check: labeled statement, bypassing the code in between. This can make the code flow less intuitive and more difficult to understand.


Q.19 Write a program to print only prime number between 1 to N numbers
#include <stdio.h>

int main() {
    int N;

    // Get the value of N from the user
    printf("Enter the value of N: ");
    scanf("%d", &N);

    printf("Prime numbers between 1 and %d are:\n", N);

    for (int i = 2; i <= N; i++)
    {
        int isPrime = 1; // Assume i is prime initially

        // Check if i is divisible by any number from 2 to i-1
        for (int j = 2; j * j <= i; j++) 
        {
            if (i % j == 0)
            {
                isPrime = 0; // i is not prime
                break;
            }
        }

        // If isPrime is still 1, then i is a prime number
        if (isPrime) 
        {
            printf("%d\n", i);
        }
    }

    return 0;
}


Q.20 List and explain conditional branching statements and explain each with example

Conditional branching statements in C allow you to control the flow of your program based on certain conditions. Here are the primary conditional branching statements in C, along with explanations and examples for each:

if statement:
Description: Executes a block of code if a specified condition is true.

Syntax:
if (condition) {
    // Code to be executed if the condition is true
}

Example:
#include <stdio.h>
int main() {
    int num = 10;

    if (num > 5) {
        printf("The number is greater than 5\n");
    }

    return 0;
}
Output:
The number is greater than 5

if-else statement:
Description: Executes one block of code if a condition is true and another block if it's false.

Syntax:
if (condition) {
    // Code to be executed if the condition is true
} else {
    // Code to be executed if the condition is false
}

Example:
#include <stdio.h>
int main() {
    int num = 3;

    if (num % 2 == 0) {
        printf("The number is even\n");
    } else {
        printf("The number is odd\n");
    }

    return 0;
}
Output:
The number is odd

if-else if-else statement(else if ladder):
Description: Checks multiple conditions in sequence and executes the block associated with the first true condition.

Syntax:
if (condition1) {
    // Code to be executed if condition1 is true
} else if (condition2) {
    // Code to be executed if condition2 is true
} else {
    // Code to be executed if none of the conditions is true
}

Example:
#include <stdio.h>
int main() {
    int score = 75;

    if (score >= 90) {
        printf("Grade: A\n");
    } else if (score >= 80) {
        printf("Grade: B\n");
    } else if (score >= 70) {
        printf("Grade: C\n");
    } else {
        printf("Grade: F\n");
    }

    return 0;
}
Output: 
Grade: C

switch statement:
Description: Checks the value of an expression against a list of cases and executes the block associated with the first matching case.

Syntax:
switch (expression) {
    case value1:
        // Code to be executed if expression equals value1
        break;
    case value2:
        // Code to be executed if expression equals value2
        break;
    // More cases as needed
    default:
        // Code to be executed if expression doesn't match any case
}

Example:
#include <stdio.h>

int main() {
    int day = 3;

    switch (day) {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        default:
            printf("Unknown day\n");
    }

    return 0;
}

Output:
Wednesday

Q.21 Give the general structure of switch statement and example

he general structure of a switch statement in C is as follows:
switch (expression) {
    case value1:
        // Code to be executed if expression equals value1
        break;
    case value2:
        // Code to be executed if expression equals value2
        break;
    // More cases as needed
    default:
        // Code to be executed if expression doesn't match any case
}

Here's an example to illustrate the usage of a switch statement:
#include <stdio.h>

int main() {
    int day = 3;

    switch (day) {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        case 4:
            printf("Thursday\n");
            break;
        case 5:
            printf("Friday\n");
            break;
        case 6:
            printf("Saturday\n");
            break;
        case 7:
            printf("Sunday\n");
            break;
        default:
            printf("Invalid day\n");
    }

    return 0;
}

In this example, the switch statement is used to check the value of the variable day. Depending on the value of day, it prints the corresponding day of the week. If the value of day doesn't match any of the specified cases, the default block is executed, which prints "Invalid day." The break statements are used to exit the switch statement once a matching case is found.

Q.22 Explain break and continue statements with an example.

Certainly! In C language, break and continue are control flow statements used within loops to alter the normal flow of execution.

break Statement:
The break statement is used to exit a loop prematurely. When encountered, it causes the program to immediately exit the loop, regardless of whether the loop condition is true or false.

Example: Using break in a for loop to find the first even number in an array:
#include <stdio.h>
int main() {
    int numbers[] = {1, 3, 5, 7, 8, 9, 11};

    for (int i = 0; i < 7; i++) {
        if (numbers[i] % 2 == 0) {
            printf("First even number found: %d\n", numbers[i]);
            break; // Exit the loop once the first even number is found
        }
    }

    return 0;
}

continue Statement:
The continue statement is used to skip the rest of the loop body and move on to the next iteration of the loop.

Example: Using continue in a while loop to print odd numbers between 1 and 10:
#include <stdio.h>
int main() {
    int i = 1;

    while (i <= 10) {
        if (i % 2 == 0) {
            // Skip even numbers and move to the next iteration
            i++;
            continue;
        }

        printf("%d\n", i);
        i++;
    }

    return 0;
}

Both break and continue statements should be used judiciously, as their misuse can lead to code that is difficult to understand and maintain.


Q.23 Explain all looping statement with example in C

In C, there are three primary looping statements: for, while, and do-while. Each serves a similar purpose but has different syntax and use cases. Here's an explanation of each with examples:

1. for Loop:
The for loop is used when you know the exact number of iterations.

Syntax:
for (initialization; condition; update) {
    // Code to be executed in each iteration
}

Example: Printing numbers from 1 to 5 using a for loop:
#include <stdio.h>
int main() {
    for (int i = 1; i <= 5; i++) {
        printf("%d ", i);
    }

    return 0;
}
Output: 1 2 3 4 5

2. while Loop:
The while loop is used when you don't know the exact number of iterations beforehand, and the loop continues as long as a specified condition is true.

Syntax:
while (condition) {
    // Code to be executed in each iteration
}

Example: Printing numbers from 1 to 5 using a while loop:
#include <stdio.h>
int main() {
    int i = 1;
    while (i <= 5) {
        printf("%d ", i);
        i++;
    }

    return 0;
}
Output: 1 2 3 4 5

3. do-while Loop:
The do-while loop is similar to the while loop, but it guarantees that the loop body is executed at least once.

Syntax
do {
    // Code to be executed in each iteration
} while (condition);

Example: Printing numbers from 1 to 5 using a do-while loop:
#include <stdio.h>
int main() {
    int i = 1;
    do {
        printf("%d ", i);
        i++;
    } while (i <= 5);

    return 0;
}
Output: 1 2 3 4 5



Q.24 Compare while loop and do..while loop

While Loop
- It checks the condition before executing the loop.
- If the condition is false initially, the loop may not execute at all.
- Syntax: while (condition) { // code to be executed }
- It's a pre-test loop because it tests the condition before entering the loop.
- If the condition is false initially, the loop doesn't execute.
- The loop may not run if the condition is false from the beginning.
- It's suitable when you want to execute the loop body based on a condition.

Do..While Loop
- It executes the loop at least once, and then checks the condition.
- Even if the condition is false initially, the loop body runs once.
- Syntax: do { // code to be executed } while (condition);
- It's a post-test loop because it runs the loop body at least once before testing the condition.
- The loop body is executed first, and then the condition is checked.
- The loop always runs at least once, regardless of the initial condition.
- It's useful when you want to ensure the loop body is executed at least once, regardless of the condition.


Q.25 Explain while loop and do..while statement in c with example.

while Loop:
The while loop is used to repeatedly execute a block of statements as long as the given condition is true. 
Here's an example:
#include <stdio.h>
int main() {
    int count = 1;

    // Example of while loop
    while (count <= 5) {
        printf("Count: %d\n", count);
        count++;
    }

    return 0;
}
In this example, the while loop checks if count is less than or equal to 5. If true, it executes the block of statements inside the loop, prints the current value of count, and increments it. The loop continues until the condition becomes false.

do..while Loop:
The do..while loop is similar to the while loop, but it guarantees that the loop body is executed at least once because the condition is checked after the first iteration. 

Here's an example:
#include <stdio.h>
int main() {
    int count = 1;

    // Example of do..while loop
    do {
        printf("Count: %d\n", count);
        count++;
    } while (count <= 5);

    return 0;
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

In this example, the do..while loop executes the block of statements first and then checks if count is less than or equal to 5. If true, it continues with subsequent iterations; otherwise, it exits the loop.

In short
The primary difference is that the do..while loop guarantees at least one execution of the loop body, while the while loop may not execute the loop body at all if the initial condition is false.

Q.25 Define array. Explain array initialization, declaration of array. Write an example to print first 100 natural numbers with the help of an array

An array in C is a collection of elements of the same data type stored at contiguous memory locations. It allows you to store multiple values of the same type under a single variable name. Each element in an array can be accessed using an index.

Declaration of Array:
To declare an array, you specify the data type of the elements it will hold, followed by the array name and the size of the array in square brackets. 

The syntax is as follows:
data_type array_name[array_size];

Initialization of Array:
Arrays can be initialized at the time of declaration or later in the program using a loop or by individually assigning values to each element.

Here's an example of declaring and initializing an array:
#include <stdio.h>
int main() {
    // Declaration and initialization of an array
    int numbers[5] = {1, 2, 3, 4, 5};

    // Accessing array elements and printing them
    for (int i = 0; i < 5; i++) {
        printf("Element %d: %d\n", i + 1, numbers[i]);
    }

    return 0;
}

Example: Printing First 100 Natural Numbers using an Array:
#include <stdio.h>
int main() {
    // Declaration and initialization of an array for the first 100 natural numbers
    int naturalNumbers[100];

    // Initializing the array with the first 100 natural numbers
    for (int i = 0; i < 100; i++) {
        naturalNumbers[i] = i + 1;
    }

    // Printing the first 100 natural numbers
    for (int i = 0; i < 100; i++) {
        printf("%d ", naturalNumbers[i]);
    }

    return 0;
}
In this example, the array naturalNumbers is declared to store 100 integers, and then it is initialized in a loop with the first 100 natural numbers. Another loop is used to print these numbers.

Q.26 Write a program to reverse a string without using the built-in function.

#include <stdio.h>
int main() {
    // Maximum size of the string
    #define MAX_SIZE 100
    
    // Declare an array to store the string
    char str[MAX_SIZE];
    
    // Input: Ask the user to enter a string
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Find the length of the string
    int length = 0;
    while (str[length] != '\0' && str[length] != '\n') {
        length++;
    }

    // Reverse the string
    for (int i = 0; i < length / 2; i++) {
        // Swap characters from the beginning and end of the string
        char temp = str[i];
        str[i] = str[length - i - 1];
        str[length - i - 1] = temp;
    }

    // Output: Display the reversed string
    printf("Reversed string: %s", str);

    return 0;
}

Explanation:

-The program declares a character array str to store the input string.
-It prompts the user to enter a string using printf and reads the input using fgets.
-The program calculates the length of the string by iterating through it until it finds the null character \0.
-The for loop iterates over the first half of the string, swapping characters from the beginning and end to reverse the string.
-Finally, the reversed string is displayed using printf.

This program uses basic concepts like loops, arrays, and swapping to reverse a string, making it suitable for understanding at a 10th-grade level.


Q.27 What is pointer to pointer? Explain it with an appropriate program.

Definition:
A pointer in C is a variable that stores the memory address of another variable. It "points to" the location in memory where the actual data is stored.

Declaration:
To declare a pointer, you use the * (asterisk) symbol followed by the data type. For example, if you want to declare a pointer to an integer, you would write int*.

Program Example:
Let's write a simple program that takes a value from the user, stores it in a variable, and then stores the address of that variable in a pointer.

#include <stdio.h>
int main() {
    // Declare a variable to store the user input
    int userInput;

    // Prompt the user to enter a value
    printf("Enter a number: ");
    scanf("%d", &userInput);

    // Declare a pointer to an integer and store the address of the variable
    int *pointerToUserInput = &userInput;

    // Display the values
    printf("Entered number: %d\n", userInput);
    printf("Address of userInput variable: %p\n", (void*)&userInput);
    printf("Value stored in pointerToUserInput: %p\n", (void*)pointerToUserInput);

    return 0;
}

Explanation:
- We declare a variable userInput to store the value entered by the user.
- We use scanf to take a number as input from the user and store it in the userInput variable.
- We declare a pointer to an integer (int *pointerToUserInput) and store the address of the userInput variable in it using the address-of operator &.
- We use printf to display the entered number, the address of the userInput variable, and the value stored in the pointerToUserInput.



Q.28 Write a program to find the fibonacci series of n numbers.

#include <stdio.h>
int main() {
    int n;

    // Input: Ask the user for the number of terms in the Fibonacci series
    printf("Enter the number of terms in the Fibonacci series: ");
    scanf("%d", &n);

    // Initialize the first two terms of the series
    int first = 0, second = 1;

    // Display the Fibonacci series
    printf("Fibonacci Series for %d terms: ", n);

    for (int i = 0; i < n; i++) {
        // Print the current term
        printf("%d ", first);

        // Calculate the next term in the series
        int next = first + second;

        // Update first and second for the next iteration
        first = second;
        second = next;
    }

    return 0;
}

Explanation:
-We ask the user to input the number of terms (n) they want in the Fibonacci series.
-We initialize the first two terms of the series as 0 and 1.
-We use a for loop to iterate n times to generate and print the Fibonacci series.
-Inside the loop, we print the current term (first), and then calculate the next term by adding the current two terms (first and second).
-We update the values of first and second for the next iteration.
-The loop continues until n terms of the Fibonacci series are printed.



Q.29 What is the difference between call by value and call by reference?

Call by Value:

Definition:
In call by value, the values of the actual parameters (arguments) are passed to the function. The function receives a copy of the values, and any modifications made to the parameters inside the function do not affect the original values outside the function.

Example:
#include <stdio.h>
void square(int num) {
    num = num * num;
}

int main() {
    int x = 5;

    printf("Before function call, x = %d\n", x);

    // Call by value
    square(x);

    printf("After function call, x = %d\n", x);

    return 0;
}
Output:
Before function call, x = 5
After function call, x = 5

Call by Reference:

Definition:
In call by reference, the memory address (reference) of the actual parameters is passed to the function. This means that the function can directly access and modify the values stored at those addresses, affecting the original values outside the function.

Example:
#include <stdio.h>

void squareByReference(int *num) {
    *num = (*num) * (*num);
}

int main() {
    int x = 5;

    printf("Before function call, x = %d\n", x);

    // Call by reference
    squareByReference(&x);

    printf("After function call, x = %d\n", x);

    return 0;
}
Output:
Before function call, x = 5
After function call, x = 25


Q.30 Explain recursive function with example.

A recursive function is a function that calls itself, either directly or indirectly, to solve a smaller instance of the same problem. Recursive functions are often used to break down complex problems into simpler subproblems. Here's a simple example of a recursive function to calculate the factorial of a number in C:

#include <stdio.h>

// Recursive function to calculate the factorial of a number
int factorial(int n) {
    // Base case: factorial of 0 is 1
    if (n == 0 || n == 1) {
        return 1;
    } else {
        // Recursive case: n! = n * (n-1)!
        return n * factorial(n - 1);
    }
}

int main() {
    // Input: Ask the user for a number
    int number;
    printf("Enter a number: ");
    scanf("%d", &number);

    // Calculate and display the factorial using the recursive function
    printf("Factorial of %d is: %d\n", number, factorial(number));

    return 0;
}

Explanation:

Base Case:
The base case is the condition under which the recursive function stops calling itself. In the example, the base case is when n is 0 or 1. In this case, the function returns 1 because the factorial of 0 or 1 is 1.

Recursive Case:
The recursive case defines how the function calls itself with a smaller instance of the problem. In this example, the recursive case is n * factorial(n - 1), representing that the factorial of n is equal to n multiplied by the factorial of n - 1.

Main Function:
In the main function, we ask the user for a number, and then we calculate and display its factorial using the factorial recursive function.

When you run this program, it will prompt you to enter a number, and it will then calculate and display the factorial of that number using recursion. This example illustrates the basic structure of a recursive function and how it can be used to solve a mathematical problem.



Q.31 List out characteristics of function.

Modularity:
Functions help in dividing a program into smaller, manageable pieces called modules. Each function performs a specific task, making the code more organized and easier to understand.

Reusability:
Once a function is defined, it can be reused multiple times in the program. This reduces redundancy in the code and makes it more efficient.

Readability:
Functions enhance the readability of the code by breaking it into smaller units. Each function is responsible for a specific operation, making the code easier to follow.

Return Type:
Functions can have a return type that specifies the type of value the function will provide as output. For example, int for integers, float for floating-point numbers, etc.

Parameters:
Functions can take parameters (inputs) to perform operations. Parameters are variables that are passed to the function when it is called.

Function Name:
Every function has a unique name that is used to call the function. Function names should be meaningful and related to the task the function performs.

Local Variables:
Functions can have their own local variables, which are only accessible within the function. These variables are independent of variables with the same name in other functions.

Global Variables:
Global variables are variables declared outside any function and can be accessed by any function in the program. However, it is good practice to minimize the use of global variables.

Void Function:
A function with a return type of void does not return any value. It is used when the function is designed to perform a task without producing a result.

Function Declaration and Definition:
Functions are declared with a signature that includes the return type, function name, and parameter list. The actual implementation of the function is called its definition.



Q.32 Write a program to swap two numbers by using the concept of call by reference and call by value

#include <stdio.h>

// Function to swap two numbers using call by value
void swapByValue(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
    printf("Inside swapByValue function: a = %d, b = %d\n", a, b);
}

// Function to swap two numbers using call by reference
void swapByReference(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
    printf("Inside swapByReference function: a = %d, b = %d\n", *a, *b);
}

int main() {
    int num1, num2;

    // Input: Ask the user to enter two numbers
    printf("Enter first number: ");
    scanf("%d", &num1);
    printf("Enter second number: ");
    scanf("%d", &num2);

    // Display the original values
    printf("Original values: num1 = %d, num2 = %d\n", num1, num2);

    // Call by Value
    swapByValue(num1, num2);
    // Values in the main function remain unchanged
    printf("After swapByValue function: num1 = %d, num2 = %d\n", num1, num2);

    // Call by Reference
    swapByReference(&num1, &num2);
    // Values in the main function are swapped
    printf("After swapByReference function: num1 = %d, num2 = %d\n", num1, num2);

    return 0;
}

Explanation:

The program defines two functions: swapByValue (call by value) and swapByReference (call by reference).

The swapByValue function takes two parameters (a and b) and swaps their values. However, changes made inside the function do not affect the original values in the main function.

The swapByReference function takes two pointers (*a and *b) as parameters. It swaps the values pointed to by these pointers. Changes made inside the function affect the original values in the main function.

In the main function, the user inputs two numbers, and the original values are displayed.

The swapByValue function is called, and its effects on the original values are displayed.

The swapByReference function is called, and its effects on the original values are displayed.


Q.33 Write a program to find the factorial of a number using the concept of call by value.

#include <stdio.h>

// Function to swap two numbers using call by value
void swapByValue(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
    printf("Inside swapByValue function: a = %d, b = %d\n", a, b);
}

// Function to swap two numbers using call by reference
void swapByReference(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
    printf("Inside swapByReference function: a = %d, b = %d\n", *a, *b);
}

int main() {
    int num1, num2;

    // Input: Ask the user to enter two numbers
    printf("Enter first number: ");
    scanf("%d", &num1);
    printf("Enter second number: ");
    scanf("%d", &num2);

    // Display the original values
    printf("Original values: num1 = %d, num2 = %d\n", num1, num2);

    // Call by Value
    swapByValue(num1, num2);
    // Values in the main function remain unchanged
    printf("After swapByValue function: num1 = %d, num2 = %d\n", num1, num2);

    // Call by Reference
    swapByReference(&num1, &num2);
    // Values in the main function are swapped
    printf("After swapByReference function: num1 = %d, num2 = %d\n", num1, num2);

    return 0;
}


Explanation:

The program defines two functions: swapByValue (call by value) and swapByReference (call by reference).

The swapByValue function takes two parameters (a and b) and swaps their values. However, changes made inside the function do not affect the original values in the main function.

The swapByReference function takes two pointers (*a and *b) as parameters. It swaps the values pointed to by these pointers. Changes made inside the function affect the original values in the main function.

In the main function, the user inputs two numbers, and the original values are displayed.

The swapByValue function is called, and its effects on the original values are displayed.

The swapByReference function is called, and its effects on the original values are displayed.



Q.34 What is structure? Explain structure initialization.

A structure in C is like a box that can hold different types of information together. Imagine you have a box to keep details about a person, like their name, age, and height. In C, a structure helps you organize and store such information neatly.

Structure Initialization:
Initializing a structure is like putting values into the box. Let's say we have a structure for a person:

#include <stdio.h>

// Define a structure for a person
struct Person {
    char name[50];
    int age;
    float height;
};

int main() {
    // Declare a variable of type 'struct Person'
    struct Person person1;

    // Initialize the structure by putting values into it
    // Imagine we have a person named "John," who is 25 years old and 5.9 feet tall
    strcpy(person1.name, "John");
    person1.age = 25;
    person1.height = 5.9;

    // Display the information from the structure
    printf("Person's name: %s\n", person1.name);
    printf("Person's age: %d\n", person1.age);
    printf("Person's height: %.2f feet\n", person1.height);

    return 0;
}

In this example:

We create a structure called Person that has a name (a string of characters), an age (an integer), and a height (a floating-point number).
Inside the main function, we declare a variable person1 of type struct Person.
We use the strcpy function to copy the name "John" into the name field of person1.
We assign the age and height values to the age and height fields of person1.
Finally, we print out the information we stored in the structure.
So, initializing a structure is like putting specific details into the box, making it ready to hold and organize different pieces of information.



Q.35 What is union? Explain union initialization.

What is a Union?
A union in C is like a special box that can hold different types of things, but at any given time, it can only hold one thing. Imagine you have a magical box that can turn into a different shape whenever you need it to store something new.

Union Initialization:
Initializing a union is like giving the magical box its first shape. Let's say we have a union for storing different types of numbers:

#include <stdio.h>

// Define a union for storing different types of numbers
union MagicBox {
    int wholeNumber;
    float decimalNumber;
};

int main() {
    // Declare a variable of type 'union MagicBox'
    union MagicBox myBox;

    // Initialize the union by giving it its first shape
    // Imagine we want to store a whole number, let's say 10
    myBox.wholeNumber = 10;

    // Display the information from the union
    printf("The number in the box: %d\n", myBox.wholeNumber);

    // Now, let's give the box a new shape for a decimal number, like 5.5
    myBox.decimalNumber = 5.5;

    // Display the updated information from the union
    printf("The number in the box: %.2f\n", myBox.decimalNumber);

    return 0;
}

In this example:

- We create a union called MagicBox that can hold either a whole number (int) or a decimal number (float).
- Inside the main function, we declare a variable myBox of type union MagicBox.
- We initialize the union by giving it its first shape, storing the whole number 10 in the wholeNumber field.
- We print out the information from the union.
Then, we give the box a new shape for a decimal number, storing 5.5 in the decimalNumber field.
- We print out the updated information from the union.
- So, initializing a union is like giving the magical box its first shape, allowing it to store one type of thing at a time, and then you can change its shape to store something different!



Q.36  Explain user defined function. A.enum     B.typedef. Explain with an example.

User-Defined Function:
A user-defined function in C is like a small set of instructions that you create to perform a specific task. Imagine you have a little helper that knows how to do something, and whenever you need that task done, you ask your helper to do it.

Example of a User-Defined Function:
#include <stdio.h>

// Here, we are defining a function called 'addNumbers'
// It takes two parameters (inputs), adds them, and returns the result
int addNumbers(int a, int b) {
    int sum = a + b;
    return sum;
}

int main() {
    // Imagine we want to add two numbers, 5 and 7
    // We use our 'addNumbers' function to do that
    int result = addNumbers(5, 7);

    // Display the result
    printf("The sum is: %d\n", result);

    return 0;
}

Enum:
An enum in C is like a way to create a set of named integer values. It's like having predefined options or choices that you can use in your program.

Example:
#include <stdio.h>

// Here, we define an enum called 'Weekdays'
// It has named values for each day of the week
enum Weekdays {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
};

int main() {
    // Imagine we want to represent today as Tuesday
    // We use our 'Weekdays' enum to do that
    enum Weekdays today = Tuesday;

    // Display the value representing today
    printf("Today is day number: %d\n", today);

    return 0;
}


Typedef:
Typedef in C is like giving a new name to an existing data type. It helps in making code more readable and creating aliases for data types.

Example:
#include <stdio.h>

// Here, we use typedef to create a new name 'Distance' for the data type 'float'
typedef float Distance;

int main() {
    // Imagine we want to represent a distance of 3.5 units
    // We use our new name 'Distance' to do that
    Distance distanceValue = 3.5;

    // Display the distance value
    printf("The distance is: %.2f units\n", distanceValue);

    return 0;
}


Q.37 Explain various file functions with example.

File Functions in C:
File functions in C help you work with files on your computer. Think of a file as a piece of paper where you can write or read information. The file functions in C are like tools that help your program interact with these files.

Here are some basic file functions:

1. fopen() - Opening a File:
fopen function helps you open a file. You can open a file for reading, writing, or both.
Example:
FILE *filePointer;
filePointer = fopen("example.txt", "r");  // Opens the file named "example.txt" for reading

2. fclose() - Closing a File:
fclose function helps you close a file when you are done with it. It's like closing a book after reading it.
Example:
fclose(filePointer);  // Closes the file that was opened using the filePointer

3. fread() - Reading from a File:
fread function helps you read data from a file. It's like reading information from a book.
Example:
int data;
fread(&data, sizeof(int), 1, filePointer);  // Reads an integer from the file into the 'data' variable

4. fwrite() - Writing to a File:
fwrite function helps you write data to a file. It's like writing information on a piece of paper.
Example:
int data = 42;
fwrite(&data, sizeof(int), 1, filePointer);  // Writes the integer value '42' to the file

5. fprintf() - Formatted Writing to a File:
fprintf function helps you write formatted data to a file. It's like writing a nicely formatted letter.
Example:
fprintf(filePointer, "Hello, %s!", "World");  // Writes "Hello, World!" to the file

6. fscanf() - Formatted Reading from a File:
fscanf function helps you read formatted data from a file. It's like reading specific information from a letter.
Example:
char greeting[20];
fscanf(filePointer, "%s", greeting);  // Reads a string from the file into the 'greeting' variable


Q.38 Write a program that implements a structure Student, which stores the roll_number, Name, Contact details and result of 50 students.

#include <stdio.h>

// Define a structure for a Student
struct Student {
    int roll_number;
    char name[50];
    char contact_details[20];
    float result;
};

int main() {
    // Declare an array of type 'struct Student' to store information for 50 students
    struct Student students[50];

    // Input: Get information for each student using a loop
    for (int i = 0; i < 50; i++) {
        printf("Enter details for Student %d:\n", i + 1);

        // Get roll number
        printf("Roll Number: ");
        scanf("%d", &students[i].roll_number);

        // Get name
        printf("Name: ");
        scanf("%s", students[i].name);

        // Get contact details
        printf("Contact Details: ");
        scanf("%s", students[i].contact_details);

        // Get result
        printf("Result: ");
        scanf("%f", &students[i].result);

        // Print a blank line for better readability
        printf("\n");
    }

    // Output: Display the information for each student
    printf("Student Information:\n");

    for (int i = 0; i < 50; i++) {
        printf("Student %d:\n", i + 1);
        printf("Roll Number: %d\n", students[i].roll_number);
        printf("Name: %s\n", students[i].name);
        printf("Contact Details: %s\n", students[i].contact_details);
        printf("Result: %.2f\n", students[i].result);
        printf("\n");
    }

    return 0;
}

Explanation:

We define a structure called Student with fields for roll number, name, contact details, and result.

We declare an array students of type struct Student to store information for 50 students.

We use a loop to input information for each student, asking for their roll number, name, contact details, and result.

Another loop is used to display the information for each student.

This program allows you to input and display information for 50 students using a simple structure. It uses loops to make the process efficient and easy to understand.

If you have any question then comment below - We will answer your questions. And do not forget to follow us.