Computer Science BIEK XII Notes: C Programming Complete Notes XII BIEK , Computer Science Past Paper Question With Answer

https://idealcollegiate1.blogspot.com/2023/02/computer-science-notes-c-programming.html
The Textbook of COMPUTER SCIENCE For Class XII – Class 12 – Sindh Textbook Board 

BOTH SECTION B & SECTION C

Q1: Why is C language called 'case sensitive language?
Ans: C language is called a "case-sensitive language" because it differentiates between uppercase and lowercase letters in the syntax of the language. In C, variable names, functions, and keywords are case-sensitive, meaning that the names must be written with the correct capitalization in order for the code to be interpreted correctly. For example, if you write a variable name "count" in one place and "Count" in another place, C will treat these as two different variables. This feature allows for greater flexibility and nuance in writing code, but also requires that the programmer be mindful of the capitalization used throughout their code.

Q2: What will be the output of the following program segment?

For(i=1; i<5; i++)

if(i = = 3)

continue;

else

print("%d",i);

Ans: The program segment you provided is written in C and it should produce the following output:

1    2    4

The for loop initializes i to 1, continues as long as i is less than 5, and increments i by 1 after each iteration. The if-else statement inside the loop checks the value of i and executes the continue statement if i is equal to 3, causing the rest of the statements in the loop to be skipped for that iteration. If i is not equal to 3, the printf statement is executed, which prints the value of i to the console. As a result, the numbers 1, 2, and 4 are printed, but the number 3 is not printed because the continue statement causes that iteration of the loop to be skipped.

Q3: Write a program that generates  the following  output:(use any loop)

                2              4

                3              6

                4              8             

                5              10

Ans: We use a for loop to generate the desired output using C.

#include <stdio.h>

int main()

{

    int i;

    for (i = 2; i <= 5; i++)

    {

        printf("%d\t%d\n", i, i * 2);

    }

    return 0;

}

The program uses a for loop to iterate through the values 2 to 5. In each iteration of the loop, the values of i and i * 2 are printed to the console using the printf function. The \t characters in the format string cause the output to be tab-separated, and the \n character causes a new line to be printed after each iteration of the loop.

The output of this program will be:

2 4

3 6

4 8

5 10

Q4: Write a program that prints the factorial of a given number?

Ans: Algorithm:

  1. Start the program.
  2. Read the value of the number for which the factorial is to be calculated.
  3. Initialize the variable factorial to 1.
  4. Use a for loop to iterate i from 1 to num.
  5. In each iteration, multiply factorial with I.
  6. After the loop ends, print the message "The factorial of num is factorial".
  7. End the program.

C-Programmming Code

#include <stdio.h>

int main() {

    int num, i, factorial = 1;

    printf("Enter an integer: ");

    scanf("%d", &num);

    // Calculate the factorial of the given number

    for (i = 1; i <= num; i++) {

        factorial *= i;

    }

    printf("The factorial of %d is %d", num, factorial);

    return 0;

}

https://idealcollegiate1.blogspot.com/2023/02/computer-science-notes-c-programming.html
Flowchart for finding the factorial of a number by programming9

Q5: What is a function? What are the advantages of using a function?

Ans: FUNCTION

In programming, a function is a piece of code that performs a specific task. It allows you to group a set of instructions together and call them as many times as needed from different parts of your program, without having to rewrite the same code over and over again. 

Functions provide several advantages, such as improving code readability and organization, reducing redundancy, promoting code reusability, and enhancing maintainability and scalability.

FUNCTIONS USED IN C PROGRAMMING

In C programming, functions play a critical role and are used extensively. Some of the most common functions in C include:

printf() - used to print text on the screen

scanf() - used to read input from the user

strlen() - used to determine the length of a string

strcmp() - used to compare two strings

strcpy() - used to copy one string to another

strcat() - used to concatenate two strings

rand() - used to generate random numbers

sqrt() - used to calculate the square root of a number

sin() - used to calculate the sine of an angle

cos() - used to calculate the cosine of an angle

By using functions in C programming, you can break down your code into smaller, more manageable pieces that are easier to read, test, and maintain. Additionally, you can reuse the same functions in different parts of your program, saving time and reducing errors.

ADVANTAGES OF FUNCTIONS USED IN C PROGRAMMING

There are several advantages of using functions in C programming, some of which are:

1. Code Reusability: Functions allow us to reuse the same code again and again at different places in our program, which makes it easier to maintain and reduces the overall development time.

2. Modular Programming: By dividing the program into smaller and more manageable modules, we can make the code more organized and easier to understand. This also makes it easier to update and modify code without affecting other parts of the program.

3. Debugging: With functions, it is easier to isolate and fix errors as each function has a specific task and can be tested and debugged separately.

4. Abstraction: Functions provide a level of abstraction that allows us to focus on the task that needs to be performed rather than how it is done. This makes the code more readable and easier to understand.

5. Efficiency: Functions can make code more efficient by reducing the amount of redundant code and by allowing us to optimize specific parts of the program.

Overall, functions are an essential part of C programming and provide many benefits that help in creating efficient, scalable, and maintainable code.

Q6: Define scanf() with its syntax?

Ans: In C programming, scanf() is a standard input function that is used to read input data from the user or from a file. The syntax for scanf() function is:

                scanf("format specifier", &variable_name);

The format specifier specifies the type of data to be read, such as integer, float, character, or string, while the '&' symbol before the variable name is used to pass the address of the variable to the function. This allows the function to directly modify the value stored in the variable.

For example: To read an integer value from the user, the syntax would be:

                                    scanf("%d", &num);

where '%d' is the format specifier for integers, and 'num' is the variable where the input value will be stored.

NOTE:

It is important to note that scanf() can be susceptible to buffer overflow vulnerabilities if not used properly, so it is important to ensure that the input data is valid and the buffer size is appropriate to prevent such issues.

Q7: Differentiate between Actual Parameters and formal Parameters.

Ans: 

Parameter type Actual Parameters Formal Parameters
Definition The values or expressions that are passed to a function during its call The parameters that are defined in the function declaration
Usage Act as input values to the function Act as placeholders for input values
Value May be a constant or a variable Always a variable
Assignment Values are assigned to actual parameters during the function call Values are assigned to formal parameters during the function declaration
Data type Can be of any data type, including literals, variables, expressions, or function calls Must have a specific data type specified in the function declaration
Examples In add(2, 3), 2 and 3 are actual parameters In int add(int x, int y), x and y are formal parameters

Q8: What is reserved word or keywords in C language? Define any three?
Ans: Reserved words or keywords in C programming language are predefined words that have a special meaning and cannot be used as variable names or function names. These keywords are used to define the syntax of the language and cannot be redefined or re-purposed by the programmer.

C programming language has a total of 32 reserved words or keywords. Here are some examples of reserved words in the C programming language:

  • if: The if keyword is used to define a conditional statement in C programming language. The if statement is used to test whether a certain condition is true or false, and to execute different code blocks based on the result of the test.
  • for: The for a keyword is used to define a loop statement in C programming language. The for loop is used to execute a block of code multiple times, based on a certain condition. The for loop consists of an initialization statement, a condition statement, and an iteration statement.
  • while: The while keyword is used to define another type of loop statement in the C programming language. The while loop is used to execute a block of code repeatedly, based on a certain condition. The while loop consists of a condition statement, which is tested before each iteration of the loop. If the condition is true, the loop is executed, and the process continues until the condition is false.
  • int: The int keyword is used to define a variable of type integer in C programming language. The int data type is used to store whole numbers, and the size of an int variable is typically 4 bytes.
  • float: The float keyword is used to define a variable of type floating-point in C programming language. The float data type is used to store real numbers with single precision, and the size of a float variable is typically 4 bytes.
  • char: The char keyword is used to define a variable of type character in C programming language. The char data type is used to store a single character, such as a letter, digit, or special symbol, and the size of a char variable is typically 1 byte.

These reserved words in C programming language are essential for defining the syntax and structure of the language, and it is important for programmers to understand their meanings and usage in order to write effective and efficient code.
Q9: What are library functions? Name any four?
Ans: LIBRARY FUNCTION
Library functions are pre-written functions that are part of the C standard library, which provides a set of functions that can be used in C programs without the need to write them from scratch. These functions are written in C and are available in the precompiled form, making them easy to use in C programs.

Some of the most commonly used library functions in C programming are:
  • printf() - This function is used to display output on the console. It takes a string as input and prints it to the console with optional arguments that can be used to format the output.
  • scanf() - This function is used to read input from the console. It takes one or more format specifiers as input and reads the input values accordingly.
  • malloc() - This function is used to allocate memory dynamically at runtime. It takes size as input and returns a pointer to the allocated memory block.
  • strlen() - This function is used to calculate the length of a string. It takes a string as input and returns the number of characters in the string.
  • strcmp() - This function is used to compare two strings. It takes two strings as input and returns an integer value indicating whether the strings are equal or not.
  • fopen() - This function is used to open a file. It takes the filename and the file mode as input and returns a file pointer.
  • fclose() - This function is used to close a file. It takes a file pointer as input and closes the corresponding file.
  • rand() - This function is used to generate a random number. It returns a random integer value.
  • atof() - This function is used to convert a string to a double value. It takes a string as input and returns a double value.
  • atoi() - This function is used to convert a string to an integer value. It takes a string as input and returns an integer value.
Q10:By using nested loop write the program to print the following output .
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Ans: Here is the C program to print the desired output using nested loops:
Program
#include <stdio.h>
int main() {
    int i, j;
    for (i = 2; i <= 5; i++) {
        for (j = 1; j <= 5; j++) {
            printf("%d\t", i * j);
        }
        printf("\n");
    }
    return 0;
}

Explanation:
The outer loop is used to iterate over the rows, which is controlled by the variable i.
The inner loop is used to iterate over the columns, which is controlled by the variable j.
Inside the inner loop, we print the product of i and j, followed by a tab character.
After each row, we print a newline character to move to the next line.
The loop continues until all rows and columns are printed.

Q11:Write a program in C language that prints the following :

1

22

333

4444

55555

Ans: Here is the C program to print the desired output using nested loops:
Program

#include <stdio.h>

int main() {
    int i, j;
    for (i = 1; i <= 5; i++) {
        for (j = 1; j <= i; j++) {
            printf("%d", i);
        }
        printf("\n");
    }
    return 0;
}
Explanation:
We start by including the stdio.h header file, which provides the printf function for printing output to the console.
In the main function, we declare two integer variables i and j for use in the loops.
The outer loop for (i = 1; i <= 5; i++) runs from 1 to 5, for each row of the pattern.
The inner loop for (j = 1; j <= i; j++) runs from 1 to the current row number i, and prints the number i repeatedly.
After printing the inner loop, we use printf("\n") to move to the next line and start the next row.
Finally, we return 0 to indicate successful program completion.
When you run the program, it will print the following output:
1
22
333
4444
55555
Note that the number of rows and the pattern can be changed by modifying the loop limits and the printf statements accordingly.

Q12:If x=1, what is the output of following program segments?

Printf ("x=%d\n",x);

Printf("x=%d\n",++x);

Printf("x= %d\n",x++);

Ans: If x=1, the output of the following program segments would be:

  • The first printf statement printf("x=%d\n",x); simply prints the value of x, which is 1.
  • The second printf statement printf("x=%d\n",++x); increments the value of x by 1 using the prefix increment operator ++ before the variable x. So, x becomes 2, and then it is printed by this printf statement.
  • The third printf statement printf("x= %d\n",x++); prints the value of x, which is still 1 because the post-increment operator ++ is used after the variable x in this statement. This means that x is first printed and then incremented by 1. So, even though x is incremented to 2 in the previous statement, it is not reflected in this statement. Therefore, the output is 1.

Q13:What is the output of the following program statements :

For (int a=1; a <5;a++)

{

For (int b=a;  b <5;b++)

Printf ("%d",b);

Printf("\n");

}

Ans: The output of the program statements would be:


1
2
3
4
2
3
4
3
4
4

Explanation:

The program is using nested for loops. The outer loop starts with the variable "a" equal to 1 and continues as long as "a" is less than 5. The inner loop starts with the variable "b" equal to "a" and also continues as long as "b" is less than 5. Inside the inner loop, the program prints the value of "b" using the printf function. After the inner loop finishes, the program prints a new line character ("\n") to start a new line. This pattern continues until the outer loop finishes.

In the first iteration of the outer loop, the inner loop starts with "b" equal to 1 and prints the values of 1, 2, 3, and 4, each on a new line. In the second iteration of the outer loop, the inner loop starts with "b" equal to 2 and prints the values of 2, 3, and 4. This pattern continues until the outer loop finishes with "a" equal to 4, at which point the last iteration of the inner loop prints the value of 4.
Q14:Define each format specifier with an example :
(a)%c   (b)%d   (c)%f  (d)%s  (e)%if (f) %Id 
Ans: 
(a) %c  
This format specifier is used to input/output a single character
Example:
char ch;
scanf("%c", &ch); //input a character
printf("The character is: %c", ch); //output the character
(b) %d 
This format specifier is used to input/output an integer value. 
Example:
int num;
scanf("%d", &num); //input an integer
printf("The integer is: %d", num); //output the integer
(c) %f 
This format specifier is used to input/output a floating-point value. 
Example:
float fnum;
scanf("%f", &fnum); //input a floating-point value
printf("The floating-point value is: %f", fnum); //output the floating-point value
(d) %s 
This format specifier is used to input/output a string of characters. 
Example:
char str[50];
scanf("%s", str); //input a string
printf("The string is: %s", str); //output the string
(e) %i 
This format specifier is used to input/output an integer value in either decimal, octal, or hexadecimal format based on the prefix. 
Example:
int num;
scanf("%i", &num); //input an integer in decimal, octal or hexadecimal format
printf("The integer is: %i", num); //output the integer
(f) %Id 
This format specifier is used to input/output a signed integer of a specified width. Example:
int num;
scanf("%2d", &num); //input a 2-digit signed integer
printf("The signed integer is: %3d", num); //output the signed integer with a width of 3
Q15: Write the output of the following statements of the C program 
For (int I =2  i <10 ;  i++)
{
Printf"%d", i);
IF(i%2==0)
Continues'
Printf ("\n");
}
Ans: The given C program will output the following:
2
4
6
8
Explanation:

The program initializes the variable i to 2 and loops until i is less than 10. In each iteration of the loop, the program prints the value of i using the printf function.
Then, the program checks if i is divisible by 2 (i.e., even) using the modulus operator %. If i is even, the program uses the continue statement to skip the remaining statements in the current iteration of the loop, and jumps to the next iteration.
After skipping the remaining statements in the current iteration, the program prints a newline character using the printf function. This is done to print each even number on a new line.
Therefore, the program will output only the even numbers between 2 and 10 (inclusive) on separate lines.
Q16: Write a program to generate prime numbers upto 11 (like 2 3 5 7 11)
Ans: Here's a C program to generate prime numbers up to 11:

#include <stdio.h>
int main() {
    int num, i, flag;
    printf("Prime numbers up to 11: ");

    // Check numbers from 2 to 11
    for (num = 2; num <= 11; num++) {
        // Assume the current number is prime
        flag = 1;

        // Check if the current number is divisible by any number between 2 and the number itself
        for (i = 2; i < num; i++) {
            if (num % i == 0) {
                flag = 0;
                break;
            }
        }

        // If the number is prime, print it
        if (flag == 1) {
            printf("%d ", num);
        }
    }

    return 0;
}
Explanation:

This program uses a nested loop to generate prime numbers up to 11. The outer loop iterates over the numbers 2 to 11. For each number, the inner loop checks whether it is prime or not.

To check whether a number is prime or not, the program assumes that the number is prime initially (by setting flag = 1). It then checks if the number is divisible by any number between 2 and the number itself. If it is, then the number is not prime, and the program sets flag = 0. If the number is not divisible by any number between 2 and itself, then it must be prime, and the program prints it.

At the end of the program, it prints all the prime numbers found between 2 and 11, separated by spaces. The output of this program will be:

Prime numbers up to 11: 2 3 5 7 11
which are the prime numbers between 2 and 11 (inclusive).
                                OR
#include <stdio.h>

int main() {
    int i, j, flag;

    // Check prime numbers from 2 to 11
    for(i=2; i<=11; i++) {
        flag = 0;
        // Check if i is a prime number
        for(j=2; j<=i/2; j++) {
            if(i%j == 0) {
                flag = 1;
                break;
            }
        }
        // Print i if it is a prime number
        if(flag == 0) {
            printf("%d ", i);
        }
    }

    return 0;
}
The program starts by initializing a loop to check prime numbers from 2 to 11. For each number in the loop, it then checks if the number is a prime number by dividing it by every number from 2 to half of the number itself. If the number is divisible by any of these numbers, it is not a prime number and the program sets a flag to indicate this. If the number is not divisible by any of these numbers, the program prints the number as a prime number.

When you run this program, it will output the following sequence of prime numbers:

2 3 5 7 11



Post a Comment

2 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

ADMISSIONS OPEN IN MDCAT, ECAT, SESSION 2025-26
ADMISSIONS OPEN IN MDCAT, ECAT, SESSION 2025-26