0% found this document useful (0 votes)
18 views7 pages

W3 Questions

bye'

Uploaded by

bejjiprasad2006
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)
18 views7 pages

W3 Questions

bye'

Uploaded by

bejjiprasad2006
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/ 7

1. Write a C program to determine if a number is even or odd.

(if - else)
Input: 8
Output: 8 is an even number.
Input:13
Output: 13 is an odd number.

Template.c
#include <stdio.h>
/* Include any headers here */

int main() {

/* Put your declarations here */

int number;

/* --- Start your solution here --- */

/* --- End of the solution --- */


return 0;
}

Test Cases:
Input Output
32 32 is an even number.
223 223 is an odd number.
9999 9999 is an odd number.
25874 25874 is an even number.
500000001 500000001 is an odd number.

2. Write a C program to find the largest of three numbers using nested if


statements.
Input: 8 9 7
Output: The largest number is 9
Input:13 15 19
Output: The largest number is 19
Template.c
#include <stdio.h>
/* Include any headers here */

int main() {

/* Put your declarations here */

int a,b,c;

/* --- Start your solution here --- */

/* --- End of the solution --- */


return 0;
}

Test Cases:
Input Output
56 The largest number is 97
97
36
223 The largest number is 458
458
369
9999 The largest number is 9999
-9999
999
25874 The largest number is 79656
79656
-99878

3. Write a C program to check if a number is divisible by both 5 and 11 using


if-else.

Template.c
#include <stdio.h>
/* Include any headers here */

int main() {
/* Put your declarations here */

int num;

/* --- Start your solution here --- */

/* --- End of the solution --- */


return 0;
}

Test Cases:
Input Output
32 The number 32 is not divisible by both 5 and 11.
55 The number 55 is divisible by both 5 and 11.
9999 The number 9999 is not divisible by both 5 and 11.
275 The number 275 is divisible by both 5 and 11.
33000 The number 33000 is divisible by both 5 and 11.

4. Write a C program to find the roots of a quadratic equation ax^2 + bx + c


= 0 using an if-else structure. Consider the three cases:
a. Roots are real and different.
b. Roots are real and equal.
c. Roots are complex.
Note: This program calculates the roots of a quadratic equation.
The discriminant determines whether the roots are real or complex, and the
conditions are handled using if-else.

Template.c
#include <stdio.h>
/* Include any headers here */

int main() {

/* Put your declarations here */

double a, b, c, discriminant, realPart, imaginaryPart, root1, root2;


/* --- Start your solution here --- */

/* --- End of the solution --- */


return 0;
}

Test Cases:
Input Output
1 Roots are real and different.
-7 Root 1 = 4.00
12 Root 2 = 3.00

1 Roots are real and equal.


-4 Root 1 = Root 2 = 2.00
4

1 Roots are complex and different.


2 Root 1 = -1.00 + 2.00i
5 Root 2 = -1.00 - 2.00i

3 Roots are complex and different.


2 Root 1 = -0.33 + 1.11i
4 Root 2 = -0.33 - 1.11i

0 Invalid equation.
0
0

0 This is a linear equation.


2 Root = -0.50
1

2 Roots are real and equal.


0 Root 1 = Root 2 = -0.00
0

0 Invalid equation.
0
5
5. Write a C program that allows the user to choose from the following operations:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exponentiation
6. Square Root
7. Natural Logarithm
The program should:
• Prompt the user for input based on the chosen operation.
• Handle errors such as division by zero, square root of a negative number, and
logarithm of non-positive numbers.
• Display appropriate results or error messages for invalid inputs.

Template.c
#include <stdio.h>
/* Include any headers here */

int main() {

/* Put your declarations here */

double num1, num2, result;


int choice;

/* --- Start your solution here --- */

/* --- End of the solution --- */


return 0;
}

Test Cases:

Input Output
Select an operation from the menu: 3.00
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Exponentiation (^)
6. Square Root (r)
7. Logarithm (l)
Enter your choice (1-7): 4
9
3

Select an operation from the menu: 361.00


1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Exponentiation (^)
6. Square Root (r)
7. Logarithm (l)
Enter your choice (1-7): 5
19
2
Select an operation from the menu: 13.00
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Exponentiation (^)
6. Square Root (r)
7. Logarithm (l)
Enter your choice (1-7): 6
169
Select an operation from the menu: 0.69
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Exponentiation (^)
6. Square Root (r)
7. Logarithm (l)
Enter your choice (1-7): 7
2
Select an operation from the menu: Error: Division by zero is not allowed.
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Exponentiation (^)
6. Square Root (r)
7. Logarithm (l)
Enter your choice (1-7): 4
89
0
Select an operation from the menu: Error: Cannot compute square root of a
1. Addition (+) negative number.
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Exponentiation (^)
6. Square Root (r)
7. Logarithm (l)
Enter your choice (1-7): 6
-49
Select an operation from the menu: Error: Logarithm of non-positive numbers is
1. Addition (+) not defined.
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Exponentiation (^)
6. Square Root (r)
7. Logarithm (l)
Enter your choice (1-7): 7
-5
Select an operation from the menu: Error: Invalid choice. Please select a valid
1. Addition (+) option from the menu.
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Exponentiation (^)
6. Square Root (r)
7. Logarithm (l)
Enter your choice (1-7): 8

You might also like