fpl
fpl
**Flowchart:**
Start
|
V
Read the number
|
V
Is number % 2 == 0?
| No --> Print "Odd"
| Yes --> Print "Even"
|
V
Stop
---
**Algorithm:**
1. Start
2. Read number n.
3. If n <= 1, print "Not Prime" and exit.
4. For i = 2 to sqrt(n), do:
a. If n % i == 0, print "Not Prime" and exit.
5. Print "Prime".
6. End
---
**Importance:**
- Controls the scope and lifetime of variables.
- Ensures optimized memory usage.
- Provides flexibility in managing variable values.
---
**Difference:**
- **Constant**: A fixed value that cannot be modified during program
execution. Example: `const int a = 5;`
- **Variable**: A memory location that can store values and whose value can
change during program execution. Example: `int a = 5;`
---
5. What are arithmetic operators? Explain with examples.
**Arithmetic Operators:**
1. **+**: Addition (e.g., `a + b`)
2. **-**: Subtraction (e.g., `a - b`)
3. **\***: Multiplication (e.g., `a * b`)
4. **/**: Division (e.g., `a / b`)
5. **%**: Modulus (e.g., `a % b`)
Example:
```c
int a = 10, b = 5;
int sum = a + b; // 15
Conditional Operators: A ternary operator that evaluates a condition and returns one of two
values.
Example:
c
Always show details
Copy code
int a = 10, b = 5;
int result = (a > b) ? a : b; // result will be 10
Relational Operators:
1. ==: Equal to
2. !=: Not equal to
3. >: Greater than
4. <: Less than
5. >=: Greater than or equal to
6. <=: Less than or equal to
Example:
c
Always show details
Copy code
int a = 10, b = 5;
if(a > b) {
printf("a is greater than b");
}
8. List and explain logical operators in ‘C’.
Logical Operators:
Example:
c
Always show details
Copy code
int a = 10, b = 5;
if(a > 0 && b > 0) {
printf("Both are positive numbers");
}
112123123412345
•
•
o
Solution:
c
Always show details
Copy code
#include <stdio.h>
int main() {
int i, j;
return 0;
}
Loops in C are used to repeat a block of code multiple times. Types of loops: for, while, do-
while.
c
Always show details
Copy code
#include <stdio.h>
int main() {
for(int i = 1; i <= 5; i++) {
printf("This is line %d\\n", i);
}
return 0;
}
11. Write a program to print a series of numbers from 1 to 100 that are divisible by 5 and 7
both.
Solution:
c
Always show details
Copy code
#include <stdio.h>
int main() {
printf("Numbers divisible by 5 and 7 between 1 to 100 are:\\n");
for(int i = 1; i <= 100; i++) {
if(i % 5 == 0 && i % 7 == 0) {
printf("%d ", i);
}
}
return 0;
}
12. Write a C Program to find the sum and average of elements in a given array.
Solution:
c
Always show details
Copy code
#include <stdio.h>
int main() {
int n, i;
float sum = 0, average;
int arr[n];
printf("Enter %d numbers:\\n", n);
average = sum / n;
return 0;
}
Solution:
c
Always show details
Copy code
#include <stdio.h>
int main() {
int a[3][3], b[3][3], sum[3][3], i, j;
return 0;
}
14. Explain the declaration and initialization of one-dimensional arrays in C with examples.
Solution:
Initialization:
c
Always show details
Copy code
for(int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
Solution:
c
Always show details
Copy code
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number to find its factorial: ");
scanf("%d", &num);
16. Write a C Program that defines a structure to store book (id, author, price) information
and display the same.
Solution:
c
Always show details
Copy code
#include <stdio.h>
struct Book {
int id;
char author[50];
float price;
};
int main() {
struct Book b1;
printf("\\nBook Details:\\n");
printf("ID: %d\\n", b1.id);
printf("Author: %s\\n", b1.author);
printf("Price: %.2f\\n", b1.price);
return 0;
}
17. What is the need for user-defined functions in C? Explain with an example.
Solution:
User-defined functions allow for better modularity, reusability, and readability in programs.
c
Always show details
Copy code
#include <stdio.h>
int main() {
int num1, num2, maximum;
return 0;
}