Ad Space
C Program
#include <stdio.h>
int main() {
int number1;
int number2;
int sum;
printf("Enter the first integer: ");
scanf("%d", &number1);
printf("Enter the second integer: ");
scanf("%d", &number2);
sum = number1 + number2;
printf("\nThe sum of %d and %d is: %d\n", number1, number2, sum);
return 0;
}
Explanation
This program follows an Input-Process-Output (IPO) structure:
printf to prompt the user and scanf to read two integers, storing them in number1 and number2.sum = number1 + number2;.printf again to display a formatted message showing the original two numbers and their calculated sum.Decision-making statements are used to execute parts of code based on specific conditions.
if (x > 0) {
printf("Positive number");
}if (x % 2 == 0) {
printf("Even");
} else {
printf("Odd");
}if statement inside another if statement.
if (x > 0) {
if (x < 10) {
printf("x is between 1 and 9");
}
}if (marks >= 90)
printf("Grade A");
else if (marks >= 75)
printf("Grade B");
else
printf("Grade C");
switch (choice) {
case 1: printf("Addition"); break;
case 2: printf("Subtraction"); break;
default: printf("Invalid choice");
}Looping statements execute a block of code repeatedly.
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
// Output: 1 2 3 4 5
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 5);
A C program is organized into several sections:
#include).#include <stdio.h> // Preprocessor section
// Global declarations
int globalvar = 10;
void display(); // Function prototype
int main() { // main function section
int a = 5, b = 10; // Local declarations
int sum;
sum = a + b; // Statements
printf("Sum = %d\n", sum);
display(); // Function call
return 0;
}
// Subprogram / function
void display() {
printf("This is a user-defined function.\n");
}
ii. C Program: Largest of Three Numbers (Ternary Operator)
#include <stdio.h>
int main() {
int a, b, c, largest;
// Input three numbers
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
// Using nested ternary operator
largest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
// Output result
printf("Largest number = %d\n", largest);
return 0;
}
Output:
Enter three numbers: 10 20 5
Largest number = 20
Explanation:
a > b.a > c. If true, a is largest, else c is largest.b >= a), it checks if b > c. If true, b is largest, else c is largest.Loops are used to execute a block of code repeatedly as long as a condition is true. C has three main types of loops:
1. for loop (Entry-controlled)Used when the number of iterations is known. The condition is tested before entering the loop.
Syntax:for (initialization; condition; update) {
// body of loop
}
Example:
for(int i = 1; i <= 5; i++) {
printf("%d ", i);
}
// Output: 1 2 3 4 5
2. while loop (Entry-controlled)
Used when the number of iterations is not fixed. The condition is tested before the loop body executes.
Syntax:initialization;
while (condition) {
// body of loop
update;
}
Example:
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
// Output: 1 2 3 4 5
3. do-while loop (Exit-controlled)
The loop body is executed at least once because the condition is tested *after* the body executes.
Syntax:initialization;
do {
// body of loop
update;
} while (condition);
Example:
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 5);
// Output: 1 2 3 4 5
Loops are used to execute a block of code repeatedly as long as a condition is true. C provides three main types of loops:
1. for loop (Entry-controlled)Used when the number of iterations is known. Condition is tested before entering.
Syntax:for (initialization; condition; update) { ... }
Example:
for(int i = 1; i <= 5; i++) {
printf("%d ", i);
}
// Output: 1 2 3 4 5
2. while loop (Entry-controlled)
Used when iterations are not fixed. Condition is tested before executing.
Syntax:while (condition) { ... update; }
Example:
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
// Output: 1 2 3 4 5
3. do-while loop (Exit-controlled)
Executes at least once. Condition is tested after the body.
Syntax:do { ... update; } while (condition);
Example:
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 5);
// Output: 1 2 3 4 5
Loop Control Statements
C also has statements to control loop execution:
for(int i = 1; i <= 5; i++) {
if(i == 3) break;
printf("%d ", i);
}
// Output: 1 2
for(int i = 1; i <= 5; i++) {
if (i == 3) continue;
printf("%d ", i);
}
// Output: 1 2 4 5
int i = 1;
loop:
if(i <= 3) {
printf("%d ", i);
i++;
goto loop;
}
// Output: 1 2 3
Decision-making statements execute code based on conditions.
1. if statementExecutes a block if the condition is true.
Syntax:if (condition) { ... }
Example:
int x = 10;
if (x > 0) {
printf("x is positive");
}
// Output: x is positive
2. if-else statement
Executes one block if true, another if false.
Syntax:if (condition) { ... } else { ... }
Example:
int x = 5;
if (x % 2 == 0) {
printf("Even");
} else {
printf("Odd");
}
// Output: Odd
3. Nested if statement
An if statement inside another if.
if (x > 0) {
if (x < 10) {
printf("x is between 1 and 9");
}
}
// Output: x is between 1 and 9
4. if-else-if ladder
Used to check multiple conditions sequentially.
int marks = 80;
if (marks >= 90)
printf("Grade A");
else if (marks >= 75)
printf("Grade B");
else
printf("Grade C");
// Output: Grade B
5. switch statement
Used for multiple choices on a single variable.
switch (choice) {
case 1: printf("Addition"); break;
case 2: printf("Subtraction"); break;
default: printf("Invalid choice");
}
// Output: Subtraction
A C program follows a well-defined structure:
// Program to add two numbers).#include <stdio.h>).int x;, void display();).int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d", sum);
return 0;
}main().
void display() {
printf("This is a function.");
}#include <stdio.h> // Preprocessor section
// Global Declaration
int globalvar = 10;
void display();
int main() { // main function section
int a, b, sum; // Declarations
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
// Statement
printf("Sum = %d\n", sum);
display(); // Function call
return 0;
}
// Subprogram
void display() {
printf("This is a user-defined function.\n");
}
Decision-making statements alter the flow of program execution based on conditions, allowing the program to "decide" which code to run.
1. if statementExecutes a block if the condition is true.
Syntax:if (condition) { ... }
Example:
int x = 10;
if (x > 0) {
printf("x is positive");
}
// Output: x is positive
2. if-else statement
Executes one block if true, another if false.
Syntax:if (condition) { ... } else { ... }
Example:
int x = 5;
if (x % 2 == 0) {
printf("Even");
} else {
printf("Odd");
}
// Output: Odd
3. Nested if statement
An if statement inside another if.
if (x > 0) {
if (x < 10) {
printf("x is between 1 and 9");
}
}
// Output: x is between 1 and 9
4. if-else-if ladder
Used to check multiple conditions sequentially.
int marks = 80;
if (marks >= 90)
printf("Grade A");
else if (marks >= 75)
printf("Grade B");
else
printf("Grade C");
// Output: Grade B
5. switch statement
Used for multiple choices on a single variable.
switch (choice) {
case 1: printf("Addition"); break;
case 2: printf("Subtraction"); break;
default: printf("Invalid choice");
}
// Output: Subtraction
Ad Space