PASSING PACKAGE — MODULE 2
Programming in C (1BEIT205) — VTU 2025 Scheme
City Engineering College, Bengaluru | Dept. of AIML/ISE
Q9. Explain Reading and Writing characters with a suitable example.
Reading and writing characters means taking a single character as input from the keyboard and
displaying a single character as output. These are unformatted I/O functions (no %d, %f etc. used).
• getchar() – Reads a single character from the keyboard. Standard function, but waits for Enter
(line-buffered), so not suitable for instant interactive input.
• putchar() – Displays a single character on the screen.
• getch() – Reads a character without displaying it (non-standard, needs conio.h).
• putch() – Displays a single character (non-standard, needs conio.h).
Example:
#include <stdio.h>
int main() {
char ch = 'A';
putchar(ch); // Output: A
return 0;
}
Final Answer: getchar()/putchar() and getch()/putch() are unformatted functions used to read and
display a single character; getchar() needs Enter while getch() does not.
Q10. Explain Reading and Writing Strings with a suitable example.
A string is a group of characters. The unformatted console I/O functions used to handle strings are
gets() and puts().
• gets(char *str) – Reads a string from the keyboard, including spaces, until Enter is pressed. Enter
is not stored; instead a null character ('\0') is added at the end. Drawback: it does not check array
size, so entering more characters than the array can hold causes memory overflow.
• puts(const char *str) – Displays a string on the screen and automatically adds a new line. It runs
faster than printf() but can only print strings, not numbers.
Example:
#include <stdio.h>
int main() {
char str[80];
printf("Enter a string: ");
gets(str);
puts(str);
return 0;
}
Sample I/O: Input → Hello | Output → Hello
Final Answer: gets() reads a full string (with spaces) from the keyboard, and puts() displays it; gets() is
unsafe for large input since it does not check array bounds.
Q11. With a suitable example, explain Formatted Input and Output statements.
Formatted I/O statements read and display data in a specific format using format specifiers like %d,
%f, %c, etc.
• printf() – Formatted output function. Syntax: printf("format string", variable1, variable2, ...);
Displays text along with variable values in the given format.
• scanf() – Formatted input function. Syntax: scanf("format specifiers", &variable1;, &variable2;, ...);
Reads values from the keyboard into variables using the & (address) operator.
Example:
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum = %d", a + b);
return 0;
}
Final Answer: printf() formats and displays output using specifiers like %d/%f/%c; scanf() reads
formatted input into variables using the same specifiers with &.
Q12. Explain Statements with their types, and the selection/conditional branching
statements with examples.
A statement is a part of a program that specifies an action to be executed. C statements are of 5 types:
Selection (if, switch), Iteration (while, for, do-while), Jump (break, continue, goto, return), Label (case,
default), and Block ({ }).
A selection (branching) statement executes different blocks of code based on a condition. The main
types are:
1. if statement – Executes a block only if the condition is true; otherwise skips it.
if (num1 < num2)
{
printf("num1 is smaller");
}
2. if-else statement – Chooses between two blocks based on whether the condition is true or false.
if (num % 2 == 0)
printf("Even");
else
printf("Odd");
3. switch-case statement – Executes one block among many options by matching an expression to
constant values; break stops further case execution and default runs when no case matches.
switch(day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
default: printf("Invalid day");
}
4. Ternary operator (?:) – A short conditional operator that returns one of two values based on a
condition.
variable = condition ? Expression1 : Expression2;
int x = (a > b) ? 40 : 30;
Final Answer: C statements include selection, iteration, jump, label, and block types. Selection
statements (if, if-else, switch, ternary operator) are used for decision-making in a program.
Q13. Mention and explain different types of iteration statements with suitable examples.
An iteration statement (loop) executes a block of code repeatedly based on a condition. C has three
loops:
1. for loop – Used when the number of iterations is known. Follows Initialization → Condition →
Updation (ICU). Entry-controlled.
for(a = 5; a <= 10; a++)
{
printf("%d", a);
}
2. while loop – Entry-controlled loop; condition is checked before the body runs, so it may execute zero
times. Used when the number of iterations is unknown.
int num = 1;
while(num <= 10) {
printf("%d\n", num);
num++;
}
3. do-while loop – Exit-controlled loop; condition is checked after the body runs, so it always executes
at least once. Commonly used for menu-driven programs.
do {
// statements
} while(condition);
Feature for while do-while
Type Entry-controlled Entry-controlled Exit-controlled
Condition check Before execution Before execution After execution
Min. executions 0 or more 0 or more At least once
Final Answer: for and while are entry-controlled (condition checked first); do-while is exit-controlled
(runs at least once before checking the condition).
Q14. Explain different jump, block, and label statements with syntax and suitable
examples.
Jump statements transfer the flow of execution from one point to another instead of following the
normal sequence.
• break – Terminates the loop or switch immediately and moves control outside it.
• continue – Skips the current iteration and moves to the next one (loops only).
• return – Ends a function's execution and sends control (and optionally a value) back to the caller.
• goto – Transfers control directly to a labelled statement anywhere in the program.
Example (break vs continue):
while(expr) {
statement1;
break; // exits the loop
}
while(expr) {
statement1;
continue; // skips to next iteration
}
Example (goto):
goto label;
...
label:
... // execution jumps here
A block statement is a group of statements enclosed in { } and treated as a single unit (e.g., the body of
a loop or function). A label statement (like case, default, or a goto label) marks a point in the program
that control can jump to.
Final Answer: break exits a loop/switch, continue skips to the next iteration, return exits a function with
a value, and goto jumps to a labelled line. Block { } groups statements, and label marks a jump target.
Q15. Develop a simple calculator program for addition, subtraction, multiplication, and
division.
#include <stdio.h>
int main() {
int a, b, choice;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("\[Link] [Link] [Link] [Link]\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1: printf("Addition = %d", a + b); break;
case 2: printf("Subtraction = %d", a - b); break;
case 3: printf("Multiplication = %d", a * b); break;
case 4: printf("Division = %d", a / b); break;
default: printf("Invalid Choice");
}
return 0;
}
Sample Input: 10 5, choice = 1 → Output: Addition = 15
Final Answer: The switch statement is the suitable selection statement here, since it cleanly handles
multiple fixed options (1–4) compared to nested if-else.
Q16. Develop a C program to print Floyd's Triangle for N rows (N > 0).
#include <stdio.h>
int main() {
int n, i, j, num = 1;
printf("Enter number of rows: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
for(j = 1; j <= i; j++) {
printf("%d ", num);
num++;
}
printf("\n");
}
return 0;
}
Sample Input: 4
1
2 3
4 5 6
7 8 9 10
Final Answer: Nested for loops are used — the outer loop controls the row number, and the inner loop
prints that many increasing numbers in each row.
Q17. Develop a program to find the roots of a quadratic equation.
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, d, r1, r2;
printf("Enter a, b and c: ");
scanf("%f %f %f", &a, &b, &c);
d = b * b - 4 * a * c;
if (d >= 0) {
r1 = (-b + sqrt(d)) / (2 * a);
r2 = (-b - sqrt(d)) / (2 * a);
printf("Root 1 = %.2f\n", r1);
printf("Root 2 = %.2f\n", r2);
} else {
printf("Roots are imaginary");
}
return 0;
}
Sample Input: 1 -5 6 → Output: Root 1 = 3.00, Root 2 = 2.00
Final Answer: Roots are found using the formula (-b ± √d)/2a, where d = b² - 4ac is the discriminant. If d
< 0, the roots are imaginary.