๐Ÿ  Session 2 Homework

printf() & scanf() Functions

๐Ÿ“š What You'll Practice

This homework focuses on mastering printf() for output and scanf() for input. You'll learn format specifiers and create interactive programs.

Goal: Become comfortable with input/output operations in C programming!

๐Ÿ“‹ Instructions

๐Ÿ“ Program 1: Basic Input/Output

Get input from user and display it!

What it does:

Asks the user to enter their name and age, then displays the information back to them.

#include <stdio.h> int main() { int age; char name[50]; printf("Enter your name: "); scanf("%s", name); printf("Enter your age: "); scanf("%d", &age); printf("Hello %s! You are %d years old.\n", name, age); return 0; }

๐Ÿงช Test Cases

  • Input: Rahul, 20 โ†’ "Hello Rahul! You are 20 years old."
  • Input: Priya, 18 โ†’ "Hello Priya! You are 18 years old."

๐Ÿงฎ Program 2: Number Calculator

Get two numbers and display them!

What it does:

Asks for two numbers and displays them in a formatted way.

#include <stdio.h> int main() { int num1, num2; printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); printf("First number: %d\n", num1); printf("Second number: %d\n", num2); printf("You entered: %d and %d\n", num1, num2); return 0; }

๐Ÿงช Test Cases

  • Input: 10, 20 โ†’ Shows both numbers separately and together
  • Input: 5, 15 โ†’ Shows both numbers separately and together

๐Ÿ”ค Program 3: Character Input

Work with single characters!

What it does:

Gets a single character from the user and displays it along with its ASCII value.

#include <stdio.h> int main() { char letter; printf("Enter a character: "); scanf(" %c", &letter); printf("You entered: %c\n", letter); printf("ASCII value: %d\n", letter); return 0; }

๐Ÿงช Test Cases

  • Input: A โ†’ "You entered: A, ASCII value: 65"
  • Input: z โ†’ "You entered: z, ASCII value: 122"
  • Input: 5 โ†’ "You entered: 5, ASCII value: 53"

๐Ÿ”ข Program 4: Float Numbers

Work with decimal numbers!

What it does:

Gets a decimal number from the user and displays it with different decimal places.

#include <stdio.h> int main() { float number; printf("Enter a decimal number: "); scanf("%f", &number); printf("You entered: %f\n", number); printf("With 2 decimal places: %.2f\n", number); printf("With 1 decimal place: %.1f\n", number); return 0; }

๐Ÿงช Test Cases

  • Input: 3.14159 โ†’ Shows number with different decimal places
  • Input: 2.5 โ†’ Shows number with different decimal places

๐Ÿ“‹ Program 5: Student Information Form

Create a complete information form!

What it does:

Creates a student information form that collects name, age, grade, and displays all information in a formatted way.

#include <stdio.h> int main() { char name[50]; int age; char grade; float gpa; printf("=== STUDENT INFORMATION FORM ===\n"); printf("Enter your name: "); scanf("%s", name); printf("Enter your age: "); scanf("%d", &age); printf("Enter your grade (A/B/C/D/F): "); scanf(" %c", &grade); printf("Enter your GPA: "); scanf("%f", &gpa); printf("\n=== STUDENT DETAILS ===\n"); printf("Name: %s\n", name); printf("Age: %d\n", age); printf("Grade: %c\n", grade); printf("GPA: %.2f\n", gpa); return 0; }

๐Ÿงช Test Cases

  • Input: Rahul, 20, A, 3.8 โ†’ Complete student information display
  • Input: Priya, 19, B, 3.2 โ†’ Complete student information display