0% found this document useful (0 votes)
10 views

fpl

The document provides various programming concepts and examples in C, including flowcharts for even/odd checks, algorithms for prime number verification, and explanations of storage classes, constants vs variables, arithmetic, conditional, relational, and logical operators. It also includes C programs for loops, patterns, arrays, matrix addition, recursion, structures, and user-defined functions. Each section is designed to illustrate fundamental programming principles and practical applications in C.

Uploaded by

ssaattyyaamm2006
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)
10 views

fpl

The document provides various programming concepts and examples in C, including flowcharts for even/odd checks, algorithms for prime number verification, and explanations of storage classes, constants vs variables, arithmetic, conditional, relational, and logical operators. It also includes C programs for loops, patterns, arrays, matrix addition, recursion, structures, and user-defined functions. Each section is designed to illustrate fundamental programming principles and practical applications in C.

Uploaded by

ssaattyyaamm2006
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

1. Draw a flowchart to find whether the number is even or odd.

**Flowchart:**
Start
|
V
Read the number
|
V
Is number % 2 == 0?
| No --> Print "Odd"
| Yes --> Print "Even"
|
V
Stop

---

2. Write an algorithm to check whether a number is a prime number or not.

**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

---

3. Define storage classes in C. Write the importance of storage classes.

**Storage Classes in C:**


1. **auto**: Default storage class for local variables.
2. **register**: Stores variable in the CPU register for faster access.
3. **static**: Preserves the value of a variable between function calls.
4. **extern**: Used to declare a variable that is defined outside the
function.

**Importance:**
- Controls the scope and lifetime of variables.
- Ensures optimized memory usage.
- Provides flexibility in managing variable values.

---

4. What is the difference between constants and variables in C?

**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

6. Describe conditional operators with examples.

Conditional Operators: A ternary operator that evaluates a condition and returns one of two
values.

Syntax: condition ? value_if_true : value_if_false;

Example:

c
Always show details
Copy code
int a = 10, b = 5;
int result = (a > b) ? a : b; // result will be 10

7. List and explain relational operators in ‘C’.

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:

1. &&: Logical AND (both conditions must be true)


2. ||: Logical OR (at least one condition must be true)
3. !: Logical NOT (negates the condition)

Example:

c
Always show details
Copy code
int a = 10, b = 5;
if(a > 0 && b > 0) {
printf("Both are positive numbers");
}

9. Write a C program to print the following patterns:

112123123412345



o

Solution:

c
Always show details
Copy code
#include <stdio.h>

int main() {
int i, j;

// Print first pattern


for(i = 1; i <= 5; i++) {
for(j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\\n");
}
printf("\\n");

// Print second pattern


for(i = 1; i <= 5; i++) {
for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\\n");
}

return 0;
}

10. What are loops in C? Explain any one with examples.

Loops in C are used to repeat a block of code multiple times. Types of loops: for, while, do-
while.

Example of for loop:

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;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];
printf("Enter %d numbers:\\n", n);

for(i = 0; i < n; i++) {


scanf("%d", &arr[i]);
sum += arr[i];
}

average = sum / n;

printf("Sum = %.2f\\n", sum);


printf("Average = %.2f\\n", average);

return 0;
}

13. Write a C program to perform the addition of two 3x3 matrices.

Solution:

c
Always show details
Copy code
#include <stdio.h>

int main() {
int a[3][3], b[3][3], sum[3][3], i, j;

printf("Enter elements of first 3x3 matrix:\\n");


for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of second 3x3 matrix:\\n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
scanf("%d", &b[i][j]);
}
}

for(i = 0; i < 3; i++) {


for(j = 0; j < 3; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}

printf("Resultant matrix after addition:\\n");


for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
printf("%d ", sum[i][j]);
}
printf("\\n");
}

return 0;
}

14. Explain the declaration and initialization of one-dimensional arrays in C with examples.

Solution:

Declaration: data_type array_name[size];


Example: int arr[5];

Initialization:

• At declaration: int arr[5] = {1, 2, 3, 4, 5};


• Using loops:

c
Always show details
Copy code
for(int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}

15. Write a recursive function in C to find the factorial of any number.

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);

int result = factorial(num);


printf("Factorial of %d = %d\\n", num, result);
return 0;
}

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("Enter book id: ");


scanf("%d", &b1.id);

printf("Enter book author: ");


scanf(" %[^\n]s", b1.author);

printf("Enter book price: ");


scanf("%f", &b1.price);

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.

Example to find the maximum of two numbers:

c
Always show details
Copy code
#include <stdio.h>

int findMax(int a, int b) {


if (a > b)
return a;
else
return b;
}

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

printf("Enter two numbers: ");


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

maximum = findMax(num1, num2);


printf("The maximum of %d and %d is %d\\n", num1, num2, maximum);

return 0;
}

You might also like