![]() |
| The Textbook of COMPUTER SCIENCE For Class XII – Class 12 – Sindh Textbook Board |
BOTH SECTION B & SECTION C
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:
- Start the program.
- Read the value of the number for which the factorial is to be calculated.
- Initialize the variable factorial to 1.
- Use a for loop to iterate i from 1 to num.
- In each iteration, multiply factorial with I.
- After the loop ends, print the message "The factorial of num is factorial".
- 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;
}
![]() |
| 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.
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 |
- 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.
- 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.
1
22
333
4444
55555
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.
For (int a=1; a <5;a++)
{
For (int b=a;
b <5;b++)
Printf ("%d",b);
Printf("\n");
}

.bmp)

nice
ReplyDeletesuper
ReplyDelete