C PROGRAMMING SUGGESTIONS
Q1. Utility of header file math.h + Program + Short Notes on Library Function & Header File
Utility of math.h:
math.h is a header file in C that contains declarations of mathematical functions like sqrt(), pow(), sin(), cos(), log(), etc.
These functions perform mathematical operations that go beyond the basic arithmetic operations.
Example Program:
#include <stdio.h>
#include <math.h>
int main() {
double a = 9.0;
double b = 2.0;
printf("Square root of %.2f is %.2f\n", a, sqrt(a));
printf("%.2f raised to the power %.2f is %.2f\n", a, b, pow(a, b));
return 0;
}
Short Notes:
Library Function: These are built-in functions provided by C Standard Library to perform specific tasks like
printf(), scanf(), sqrt(), strlen(), etc.
Header File: Header files contain definitions and declarations of functions and macros. For example, stdio.h,
math.h, string.h.
Q2. Unary Plus, Minus & Logical Operators (with examples)
Unary Plus (+) and Minus (-):
o Unary plus (+) indicates a positive number.
o Unary minus (-) negates a number.
int a = +5; // Unary plus
int b = -5; // Unary minus
Logical Operators:
o && (Logical AND): true if both operands are true.
o || (Logical OR): true if at least one operand is true.
o ! (Logical NOT): inverts the truth value.
Example:
int a = 5, b = 10;
if (a > 0 && b > 0) printf("Both are positive.\n");
if (!(a == b)) printf("a is not equal to b.\n");
Q3. What is an Algorithm? Representation & Examples
Definition:
An algorithm is a step-by-step procedure or formula to solve a problem.
Representation:
Algorithms are usually represented in simple English (pseudocode), or through flowcharts.
Examples:
Algorithm 1: To add two numbers
Step 1: Start
Step 2: Input A and B
Step 3: Sum = A + B
Step 4: Output Sum
Step 5: Stop
Algorithm 2: To find the largest of two numbers
Step 1: Start
Step 2: Input A and B
Step 3: If A > B, Print A is greater, else Print B is greater
Step 4: Stop
Q4. Flowchart to Print Even Numbers from 1 to 100 and Compute Sum
Flowchart Description: You need to draw it by using symbols, as I taught in class.
1. Start
2. Initialize i = 2, sum = 0
3. While i <= 100
o Print i
o Add i to sum
o Increment i by 2
4. Print sum
5. End
6.
Q5. Algorithm to Find the Smallest of Three Distinct Numbers P, Q, R
BY: PRASENJIT ROY (GUEST LECTURER OF CS & IT) 1
Algorithm:
Step 1: Start
Step 2: Input P, Q, R
Step 3: If P < Q and P < R, then print P is smallest
Step 4: Else if Q < P and Q < R, then print Q is smallest
Step 5: Else print R is smallest
Step 6: Stop
Q6. What is a Flowchart? Use of Flowcharts with Example
Definition:
A flowchart is a graphical representation of an algorithm using different symbols like rectangles, diamonds, and arrows.
Uses of Flowcharts:
Helps visualize the flow of logic in a program.
Makes debugging easier.
Assists in documentation and communication between team members.
Example:
Flowchart to check if a number is even or odd:
Start → Input N → Is N%2==0? → Yes → Print "Even"
↓
No
↓
Print "Odd" → End
Q7. Explain with examples: a) if-else b) do-while
a) if-else Statement: b) do-while Loop:
Used for decision making. Executes the loop body at least once before checking the
int a = 10; condition.
if (a > 0) int i = 1;
printf("Positive"); do {
else printf("%d\n", i);
printf("Not Positive"); i++;
} while (i <= 5);
Q8. Short Notes: for(), while, switch, break, continue
1. for() loop: i++;
}
Used when number of iterations is known.
for (int i = 1; i <= 5; i++) 3. switch Statement:
printf("%d ", i); Used for multi-way branching.
2. while loop: int day = 2;
switch(day) {
Used when condition is evaluated before entering the case 1: printf("Sunday"); break;
loop. case 2: printf("Monday"); break;
int i = 1; default: printf("Invalid");
while (i <= 5) { }
printf("%d ", i);
4. break: Exits from loop or switch immediately.
5. continue: Skips the current iteration and continues with the next one.
Q9. Utilities of Functions with Examples
Function Utility Example
clrscr() Clears the console screen (used in Turbo C) clrscr();
getch() Waits for user to press a key (used in Turbo C) getch();
pow(x, y) Returns x raised to power y pow(2, 3) returns 8.0
sqrt(x) Returns square root of x sqrt(16) returns 4.0
scanf() Takes input from user scanf("%d", &a);
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main() {
double a = 4;
printf("Square root of %.2f is %.2f\n", a, sqrt(a));
printf("2^3 = %.2f\n", pow(2, 3));
getch(); // waits for key press
return 0;
}
Q10. Concept of Array and Pointers with Examples
Array:
A collection of similar data types stored in contiguous memory locations.
int arr[5] = {1, 2, 3, 4, 5};
printf("%d", arr[0]); // prints 1
Pointer:
A variable that stores the address of another variable.
BY: PRASENJIT ROY (GUEST LECTURER OF CS & IT) 2
int x = 10;
int *p = &x;
printf("%d", *p); // prints 10 (value at address)
Q11. Different Data Types in C with Examples Q12. Flowchart to Compute Factorial of a
C language supports several data types which define the Positive Integer n
type of data a variable can hold: Steps: You need to draw it by flowchart as I taught in
Data Type Size Example class.
Int 2 or 4 bytes int a = 10; 1. Start
2. Input n
Float 4 bytes float pi = 3.14;
3. Initialize fact = 1, i = 1
Double 8 bytes double x = 3.141592; 4. Repeat while i <= n:
Char 1 byte char ch = 'A'; o fact = fact * i
long int 4 or 8 bytes long int b = 123456;
o i = i + 1
5. Output fact
Each data type has a different range and format specifier 6. Stop
like %d, %f, %c.
Q13. Operator and Expression + Types of Operators with Examples
Operator: A symbol that performs operations (like +, *, =)
Expression: A combination of operators and operands (like a + b)
Types of Operators in C:
1. Arithmetic Operators: +, -, *, /, %
→ int c = a + b;
2. Relational Operators: ==, !=, >, <, >=, <=
→ if (a > b)
3. Logical Operators: &&, ||, !
→ if (a > 0 && b > 0)
4. Assignment Operators: =, +=, -=, *=, /=
5. Unary Operators: +, -, ++, --
→ i++, --j
6. Bitwise Operators: &, |, ^, ~, <<, >>
7. Ternary Operator: condition ? value1 : value2
Q14. Conditional and Unconditional Branching
Conditional Branching:
Branches in program occur only when a condition is true.
Example:
if (a > b)
printf("A is greater");
Unconditional Branching:
Jumps from one part of code to another without condition.
Example:
goto label;
...
label: printf("Jumped here");
Q15. Define Constant, Variable, and Formatting Source File
Constant:
A value that doesn’t change during program execution.
#define PI 3.14
const int MAX = 100;
Variable:
A named memory location used to store data.
int age = 20;
Formatting Source File:
Use consistent indentation and spacing.
Add comments for clarity.
Group logically related sections.
Example formatting:
// Program to add two numbers
#include <stdio.h>
int main() {
int a = 5, b = 10;
int sum = a + b;
printf("Sum = %d", sum);
BY: PRASENJIT ROY (GUEST LECTURER OF CS & IT) 3
return 0;
}
Q16. Write an Algorithm to Find the Average of t Numbers
Algorithm:
Step 1:
Start
Step 2:
Input value of t (total numbers)
Step 3:
Initialize sum = 0, count = 0
Step 4:
Repeat while count < t
→ Input number
→ Add to sum
→ Increment count
Step 5: avg = sum / t
Step 6: Print avg
Step 7: Stop
Q17. Utilities of %d, %ld, %c, %f with Example printf("Long Integer: %ld\n", b);
printf("Character: %c\n", ch);
Program printf("Float: %f\n", f);
Format Specifier Utility return 0;
%d Format for int }
%ld Format for long int Q18. Algorithm to Find the Sum of First 20
%c Format for char Natural Numbers
%f Format for float or double Algorithm:
Step 1: Start
Example Program: Step 2: Initialize sum = 0, i = 1
#include <stdio.h> Step 3: Repeat while i <= 20
→ sum = sum + i
int main() { → i = i + 1
int a = 10; Step 4: Print sum
long int b = 100000L; Step 5: Stop
char ch = 'A';
float f = 3.14; Alternatively, use the formula Sum = n*(n+1)/2 =
20*21/2 = 210.
printf("Integer: %d\n", a);
Q19. Precedence and Associativity of Operators
Precedence:
The order in which operators are evaluated in an expression.
Example: * has higher precedence than +
int a = 2 + 3 * 4; // Output: 14
Associativity:
When operators have the same precedence, associativity determines the direction (left-to-right or right-to-left).
Example:
int a = 2 * 3 / 2; // Left to right => (2*3)=6, then 6/2=3
Operator Associativity
+, -, *, / Left to Right
=, +=, -= Right to Left
Q20. Difference Between i++ and i = i + 1
i++ i = i + 1 Q21. What are 1-D and 2-D Arrays? Explain
Post-increment operator Assignment expression with Examples
Shorthand to add 1 Long form of the same action 1-D Array (One-Dimensional):
Stores a list of elements in a single row.
More concise More readable sometimes int arr[5] = {1, 2, 3, 4, 5};
printf("%d", arr[0]); // prints 1
Can be used in expressions Only updates the value
2-D Array (Two-Dimensional):
Example: Stores data in rows and columns (matrix form).
int i = 5; int mat[2][2] = {{1, 2}, {3, 4}};
i++; // i becomes 6 printf("%d", mat[1][0]); // prints 3
i = i + 1; // i becomes 7
Both do the same thing but i++ is more compact.
Q22. What is Keyword, Variable, Identifier? Explain with Example
Keyword: Reserved word used by C (e.g., int, return, if)
int a = 5; // 'int' is a keyword
Variable: A named memory location to store data.
float rate = 7.5;
Identifier: The name used for variables, functions, arrays, etc.
BY: PRASENJIT ROY (GUEST LECTURER OF CS & IT) 4
o Must begin with a letter or _
o Cannot be a keyword
Q23. Algorithm to Find Area of Triangle Q24. Algorithm to Check Prime Number
Algorithm: Algorithm:
Step 1:
Start Step 1:
Start
Step 2:
Input base and height Step 2:
Input number n
Step 3:
area = 0.5 × base × height Step 3:
If n <= 1, print Not Prime
Step 4:
Print area Step 4:
Set flag = 0
Step 5:
Stop Step 5:
For i = 2 to n/2:
Formula: area = 0.5 * base * height if n % i == 0:
set flag = 1
break
Step 6: If flag == 0 → Print Prime
Else → Print Not Prime
Step 7: Stop
Q25. All Discussed C Programs in Class:- int a[2][2] = {{1,2},{3,4}}, b[2][2] =
{{5,6},{7,8}}, sum[2][2];
i) Sum of Digits for(int i=0;i<2;i++)
int n = 123, sum = 0; for(int j=0;j<2;j++)
while(n > 0) { sum[i][j] = a[i][j] + b[i][j];
sum += n % 10; ix) Binary to Decimal
n /= 10;
int bin = 1011, dec = 0, base = 1;
}
while(bin > 0) {
printf("Sum = %d", sum);
dec += (bin % 10) * base;
ii) Fibonacci Series bin /= 10;
int a = 0, b = 1, c, i; base *= 2;
for(i = 0; i < 10; i++) { }
printf("%d ", a); Decimal to Binary
c = a + b;
a = b; int dec = 13;
while(dec > 0) {
b = c;
printf("%d", dec % 2);
}
dec /= 2;
iii) Reverse Number }
int n = 1234, rev = 0; x) Sum of First n Natural Numbers
while(n > 0) {
int sum = 0;
rev = rev * 10 + n % 10;
for(int i = 1; i <= n; i++)
n /= 10;
sum += i;
}
printf("Reverse = %d", rev); xi) Roots of Quadratic Equation
iv) Palindrome Check float a=1,b=-3,c=2, d, r1, r2;
d = b*b - 4*a*c;
int n = 121, temp = n, rev = 0;
r1 = (-b + sqrt(d)) / (2*a);
while(n > 0) {
r2 = (-b - sqrt(d)) / (2*a);
rev = rev * 10 + n % 10;
n /= 10; xii) Ascending Order (Bubble Sort)
} for(i=0; i<n-1; i++)
if (rev == temp) printf("Palindrome"); for(j=0; j<n-i-1; j++)
else printf("Not Palindrome"); if(arr[j] > arr[j+1])
v) Check Prime or Not swap(arr[j], arr[j+1]);
xiii) Descending Order
(Already given in Q24)
if(arr[j] < arr[j+1])
vi) Even Number Series up to n swap(arr[j], arr[j+1]);
for(int i = 2; i <= n; i += 2) xiv) Check Leap Year
printf("%d ", i);
if ((year % 400 == 0) || (year % 4 == 0 && year %
vii) Odd Number Series up to n 100 != 0))
for(int i = 1; i <= n; i += 2) printf("Leap Year");
printf("%d ", i); else
viii) Matrix Sum, Transpose, Multiplication printf("Not Leap Year");
(Example: 2×2 Matrix addition)
ALL THE VERY BEST 6TH SEM. CHAMPS
BY: PRASENJIT ROY (GUEST LECTURER OF CS & IT) 5