0% found this document useful (0 votes)
3 views8 pages

QBPIC

The document provides a series of programming exercises and explanations related to C language concepts. It includes algorithms for finding the largest of three numbers, explanations of loops, keywords, identifiers, variables, constants, and the use of header files. Additionally, it contains examples of C programs demonstrating various control structures and mathematical functions.
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)
3 views8 pages

QBPIC

The document provides a series of programming exercises and explanations related to C language concepts. It includes algorithms for finding the largest of three numbers, explanations of loops, keywords, identifiers, variables, constants, and the use of header files. Additionally, it contains examples of C programs demonstrating various control structures and mathematical functions.
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/ 8

4 marks question

1) Write an algorithm to find the largest of three numbers.

Step 1: Start

Step 2 : declare three variable as a, b, c

Step 3: take input of a, b and c

Step 4: check if a > b then goto step 5 else goto step 6

Step 5: check if a>c then goto step 7 else goto step 9

Step 6: check if b>c then goto step 8 else goto step 9

Step 7: display “a is greatest”

Step 8: display “b is greatest”

Step 9: display “c is greatest”

Step 10: End

2)Explain do while loop with example

​ A do-while loop is an exit-controlled loop, meaning it executes at least once before checking
the condition.

Syntax:

​ do {

​ ​ // Code to execute

} while(condition);

Example:

​ #include <stdio.h>

int main() {

int i = 1;

do {

printf("%d ", i);


i++;

} while (i <= 5);

return 0;

3)Describe the following terms : (i) Keyword (ii) Identifier (iii) Variable (iv) Constant

(i) Keyword: Reserved words in C that have special meanings (e.g., int, return, while).

(ii) Identifier: Name used to identify variables, functions, etc. (e.g., age, sum).

(iii) Variable: Named storage that holds values which can change during execution (e.g., int x
= 10;).

(iv) Constant: A fixed value that doesn’t change during program execution (e.g., #define PI
3.14 or const int x = 5;).

4) Write a ‘C’ program to find greatest number among three numbers.

​ #include <stdio.h>

int main() {

int a, b, c;

printf("Enter three numbers: ");

scanf("%d %d %d", &a, &b, &c);

if (a > b && a > c)

printf("Largest number is: %d", a);

else if (b > a && b > c)

printf("Largest number is: %d", b);

else

printf("Largest number is: %d", c);

return 0;

}
5) Explain Go-to statement with example

The goto statement transfers control to a labeled statement in a program.

Example:

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num < 0)

goto negative;

printf("You entered a positive number.");

return 0;

negative:

printf("You entered a negative number.");

return 0;

6) Explain any two math function with syntax and give example of each.

(i) sqrt(): Calculates square root.


Syntax: double sqrt(double x);

Example:

#include <stdio.h>

#include <math.h>

int main() {

double num = 16;

printf("Square root of 16: %.2f", sqrt(num));

return 0;

​ (ii) pow(): Computes power of a number.

Syntax: double pow(double base, double exp);

Example:

#include <stdio.h>

#include <math.h>

int main() {

printf("2^3 = %.2f", pow(2, 3));

return 0;

7) Describe use of header files in C language.

Header files in C are files that contain function declarations, macro definitions, and constants,
which can be used in multiple source files. They allow code reusability and modular
programming by separating function definitions from their declarations.

​ There are two types of header files in C:

●​ Standard Header Files: Provided by C, such as


○​ <stdio.h> - Input/output functions (printf(), scanf())
○​ <math.h> - Mathematical functions (sqrt(), pow())
○​ <string.h> - String functions (strlen(), strcpy())
●​ User-Defined Header Files: Created by programmers for custom functions.

Advantages of Header Files

●​ Reduces redundancy in code.


●​ Enhances maintainability and debugging.
●​ Encourages modular programming.

8) Explain nested if-else with example.

A nested if-else statement is an if condition inside another if or else block.

Example:

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num > 0) {

if (num % 2 == 0)

printf("Positive Even Number");

else

printf("Positive Odd Number");

} else {

printf("Number is not positive");

return 0;

9) Illustrate the use of break and continue statement with example.


continue: Skips the current iteration and moves to the next one.

#include <stdio.h>

int main() {

for (int i = 1; i <= 5; i++) {

if (i == 3)

continue; // Skips when i = 3

printf("%d ", i);

return 0;

break: Terminates the loop immediately.

#include <stdio.h>

int main() {

for (int i = 1; i <= 5; i++) {

if (i == 3)

break;

printf("%d ", i);

return 0;

10) Write a program to add, subtract, multiply and divide two numbers, accepted from user using
switch case.

#include <stdio.h>

int main() {
int num1, num2, choice;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

printf("Choose operation: 1. Add 2. Subtract 3. Multiply 4. Divide\n");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Sum = %d", num1 + num2);

break;

case 2:

printf("Difference = %d", num1 - num2);

break;

case 3:

printf("Product = %d", num1 * num2);

break;

case 4:

if (num2 != 0)

printf("Quotient = %.2f", (float)num1 / num2);

else

printf("Division by zero not allowed");

break;

default:

printf("Invalid choice");
}

return 0;

You might also like