Unit 2 -2 marks
1. Increment and Decrement Operators
Increment Operator (++):
Increases the value of a variable by 1.
Pre-increment (++a): Increments the value and then returns it.
Post-increment (a++): Returns the current value and then
increments it.
Example:
#include <stdio.h>
int main() {
int a = 5;
int b = ++a; // Pre-increment: a becomes 6, b is 6
int c = a++; // Post-increment: c is 6, a becomes 7
printf("a: %d, b: %d, c: %d\n", a, b, c); // Output: a: 7, b: 6, c: 6
return 0;
}
2. Goto Statement
The goto statement allows jumping to a labeled statement in the code. It's
often discouraged due to making the control flow hard to follow, leading
to "spaghetti code."
Example:
c
Copy code
#include <stdio.h>
int main() {
int count = 0;
start:
if (count < 5) {
printf("%d\n", count);
count++;
goto start; // Jump back to 'start'
}
return 0;
}
Recommendation: Avoid goto when possible. Use structured
programming constructs (like loops and functions) instead for better
readability and maintainability.
3. Pre-increment vs. Post-increment Operators
Pre-increment (++a): Increases the variable value first, then
returns it.
Post-increment (a++): Returns the current value first, then
increases it.
Example:
#include <stdio.h>
int main() {
int a = 5, b;
b = ++a; // b is 6, a is now 6
printf("Pre-increment: a = %d, b = %d\n", a, b);
b = a++; // b is 6, a is now 7
printf("Post-increment: a = %d, b = %d\n", a, b);
return 0;
}
4. C Program to Print Odd Numbers from 1 to 10
c
Copy code
#include <stdio.h>
int main() {
printf("Odd numbers from 1 to 10:\n");
for (int i = 1; i <= 10; i += 2) {
printf("%d\n", i);
}
return 0;
}
5. Nested Loop Example
Nested loops are loops within loops, useful for iterating through multi-
dimensional data.
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
printf("i: %d, j: %d\n", i, j);
}
}
return 0;
}
6. C Expression to Find Maximum of Three Numbers Using If-Else
#include <stdio.h>
int main() {
int a = 5, b = 10, c = 3;
int max;
if (a >= b && a >= c) {
max = a;
} else if (b >= a && b >= c) {
max = b;
} else {
max = c;
}
printf("Maximum: %d\n", max);
return 0;
}
7. Multiplication Table for a Number Using a For Loop
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Multiplication Table of %d:\n", num);
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}
8. Generate Prime Numbers Up to a Given Limit
#include <stdio.h>
int main() {
int limit;
printf("Enter the limit: ");
scanf("%d", &limit);
printf("Prime numbers up to %d:\n", limit);
for (int num = 2; num <= limit; num++) {
int isPrime = 1; // Assume it's prime
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0; // Not prime
break;
}
}
if (isPrime) {
printf("%d ", num);
}
}
printf("\n");
return 0;
}
9. Find the LCM (Least Common Multiple) of Two Numbers
#include <stdio.h>
int main() {
int num1, num2, lcm, max;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
max = (num1 > num2) ? num1 : num2;
do {
if (max % num1 == 0 && max % num2 == 0) {
lcm = max;
break;
}
max++;
} while (1);
printf("LCM of %d and %d is %d\n", num1, num2, lcm);
return 0;
}
10. Calculate Simple Interest
#include <stdio.h>
int main() {
float principal, rate, time, simple_interest;
printf("Enter principal amount: ");
scanf("%f", &principal);
printf("Enter rate of interest: ");
scanf("%f", &rate);
printf("Enter time (in years): ");
scanf("%f", &time);
simple_interest = (principal * rate * time) / 100;
printf("Simple Interest: %.2f\n", simple_interest);
return 0;
}
11. Arithmetic Operators
Arithmetic operators perform basic mathematical operations. Four
examples include:
+: Addition
-: Subtraction
*: Multiplication
/: Division
Example:
c
int a = 10, b = 5;int sum = a + b; // 15
int difference = a - b; // 5
int product = a * b; // 50
int quotient = a / b; // 2
12. Relational Operators vs. Logical Operators
Relational Operators: Used to compare two values. Examples include
==, !=, <, >, <=, >=.
Example:
if (a > b)
{
// Do something
}
Logical Operators: Used to combine multiple conditions. Examples
include &&, ||, !.
Example:
if (a > 0 && b > 0)
{
// Both are positive
}
13. Break vs. Continue
break: Exits the loop entirely.
Example:
for (int i = 0; i < 10; i++)
{
if (i == 5) break; // Exits loop when i is 5
}
continue: Skips the current iteration and proceeds to the next one.
Example:
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0) continue; // Skips even numbers
printf("%d\n", i); // Prints only odd numbers
}
14. Ternary Operator Example
The ternary operator is a shorthand for if-else statements.
Example:
int a = 10, b = 20;
int max = (a > b) ? a : b; // If a > b, max = a, else max = b
printf("Maximum: %d\n", max);
15. Difference Between While and Do..While Statements
while loop: Checks the condition before executing the loop body. If the
condition is false, the body is never executed.
Example:
int i = 0;while (i < 5)
{
printf("%d\n", i);
i++;
}
do..while loop: Executes the loop body at least once before
checking the condition.
Example:
int i = 0;
do {
printf("%d\n", i);
i++;
}
while (i < 5);
16. Left Shift Operator (<<) Example
The left shift operator shifts bits to the left, filling in zeros from the right.
This is equivalent to multiplying by powers of two.
Example:
#include <stdio.h>
int main()
{
int a = 5; // In binary: 0000 0101
int result = a << 1; // Shifts left by 1: 0000 1010 (10 in decimal)
printf("Left Shift: %d\n", result);
return 0;
}
17. Difference Between Logical and Bitwise Operators
Logical Operators: Used with boolean values (true/false). Examples
include &&, ||, !.
Example:
if (a > 0 && b > 0)
{
// Both conditions must be true
}
Bitwise Operators: Operate on bits and perform bit-level operations.
Examples include &, |, ^, ~, <<, >>.
Example:
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
int result = a & b; // Bitwise AND: 0001 (1 in decimal)
18. Order of Precedence of Operators
Operators in C have a defined order of precedence that determines the
order in which they are evaluated. Here’s a simplified list (from highest
to lowest):
1. () (Parentheses)
2. ++, -- (Postfix, Prefix)
3. !, ~ (Logical NOT, Bitwise NOT)
4. *, /, % (Multiplication, Division, Modulus)
5. +, - (Addition, Subtraction)
6. <<, >> (Bitwise Shift)
7. <, <=, >, >= (Relational)
8. ==, != (Equality)
9. & (Bitwise AND)
10. ^ (Bitwise XOR)
11. | (Bitwise OR)
12. && (Logical AND)
13. || (Logical OR)
14. ?: (Ternary)
15. = (Assignment)
19. Evaluate Expression with Given Values
Given the expression:
2 * ((a % 5) * (4 + (b - 3) / (c + 2)))
With:
int a = 8, b = 15, c = 4;
Step-by-step Calculation:
1. a%5→8%5→3
2. b - 3 → 15 - 3 → 12
3. c+2→4+2→6
4. (b - 3) / (c + 2) → 12 / 6 → 2
5. 4 + (b - 3) / (c + 2) → 4 + 2 → 6
6. ((a % 5) * (4 + (b - 3) / (c + 2))) → 3 * 6 → 18
7. 2 * 18 → 36
Final Value: 36
20. C Program for GCD of Two Numbers
#include <stdio.h>
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int main()
{
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
int result = gcd(num1, num2);
printf("GCD of %d and %d is %d\n", num1, num2, result);
return 0;
}