Unit 2-2 Marks
Unit 2-2 Marks
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
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;
}
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;
}
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;
}
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;
}
#include <stdio.h>
int main() {
int a = 5, b = 10, c = 3;
int max;
#include <stdio.h>
int main() {
int num;
#include <stdio.h>
int main() {
int limit;
#include <stdio.h>
int main() {
int num1, num2, lcm, max;
#include <stdio.h>
int main() {
float principal, rate, time, simple_interest;
return 0;
}
+: 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
Example:
if (a > b)
{
// Do something
}
Example:
Example:
continue: Skips the current iteration and proceeds to the next one.
Example:
Example:
while loop: Checks the condition before executing the loop body. If the
condition is false, the body is never executed.
Example:
Example:
int i = 0;
do {
printf("%d\n", i);
i++;
}
while (i < 5);
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;
}
Example:
Example:
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)
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
#include <stdio.h>
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int main()
{
int num1, num2;
return 0;
}