Programming in C 312303 Manual practical no.5 answer
Hello, students welcome to our site, in this post we discuss and perform Programming in C 312303 Manual Practical No.5 in which we Write well commented C programs in c programming.
This practical is useful for students to use different format specifiers for different data types. After the completion of this practical students will be able to use formatted input-output statements in the program.
Exercise
Write a program in ‘c’ to develop a code for the following formatted structure:
Answer:
#include <stdio.h>int main() {// Define table headerschar header1[] = “Name”;char header2[] = “Roll No”;char header3[] = “Percentage”;char header4[] = “Date of Birth”;char header5[] = “Branch/College”;// Define table data (replace with actual data)char name1[] = “Alice”;char name2[] = “Bob”;int roll_no1 = 123;int roll_no2 = 456;float percentage1 = 85.5;float percentage2 = 92.3;char dob1[] = “1999-01-01”;char dob2[] = “2000-02-02”;char branch_college1[] = “Computer Science”;char branch_college2[] = “Electronics Engineering”;// Print table headers with fixed widthsprintf(“| %-15s | %-9s | %-14s | %-16s | %-20s |n”, header1, header2, header3, header4, header5);printf(“|——————-|—|——————-|——————–|n”);// Print first student data with formatted outputprintf(“| %-15s | %-9d | %-14.2f | %-16s | %-20s |n”, name1, roll_no1, percentage1, dob1, branch_college1);// Print second student data with formatted outputprintf(“| %-15s | %-9d | %-14.2f | %-16s | %-20s |n”, name2, roll_no2, percentage2, dob2, branch_college2);return 0;}
Algorithm
Flowchart
+—————–+
| Start |
+—————–+
|
v
+—————–+
| Define Headers |
| (header1, header2,…)|
+—————–+
|
v
+—————–+
| Define Data |
| (name1, roll_no1,…)|
+—————–+
|
v
+—————–+
| Print Headers (printf)|
| with Formatting |
+—————–+
|
v
+—————–+
| Print Separator Line |
| (dashes and pipes) |
+—————–+
|
v (Loop starts)
+—————–+
| Print Student Data | (printf)
| (name, roll_no,…)|
| with Formatting |
+—————–+
|
v (Loop continues)
+—————–+
| Check for More Data |
+—————–+
|
v (Yes)
+—————–+ +—————–+
| Loop back to | | End |
| Print Student Data | +—————–+
v (No)
+—————–+
1. State significance of format specifier.
Answer:
Format specifiers in C are used to take inputs and print the output of a type. The symbol we use in every format specifier is %. Format specifiers tell the compiler about the type of data that must be given or input and the type of data that must be printed on the screen.
2. Write different data types and their format specifiers.
Answer:
Data Type | Description | printf Format Specifier |
scanf Format Specifier |
---|---|---|---|
char |
Character (single letter or symbol) | %c |
%c |
int |
Integer (whole number) | %d |
%d |
float |
Single-precision floating-point number (decimal value) | %f |
%f |
double |
Double-precision floating-point number (more precise than float ) |
%lf |
%lf |
short |
Short integer (smaller range than int ) |
%hd |
%hd |
long |
Long integer (larger range than int ) |
%ld |
%ld |
unsigned int |
Unsigned integer (positive values only) | %u |
%u |
3. What is the purpose of “%d” & “% ld”? How are they used?
Answer:
- %d
Purpose: The format specifier %d is used to print or read signed decimal integers in C. It represents a base-10 (decimal) integer value that can be positive, negative, or zero.
- %ld
Purpose: The format specifier %ld is used to print or read long signed decimal integers in C. It represents a larger range of signed integers compared to %d.
4. Write a C program to read integer, character, float, double values from the user and display it. Mention different format specifiers used in program in comment.
Answer:
#include <stdio.h>int main() {int num;char ch;float fnum;double dnum;// Prompt user for integer inputprintf(“Enter an integer: “);// Read integer using %d format specifierscanf(“%d”, &num); // %d for signed decimal integer// Prompt user for character inputprintf(“Enter a character: “);// Read character using %c format specifier (reads a single character)scanf(” %c”, &ch); // Space before %c to avoid issues with newline character// Prompt user for float inputprintf(“Enter a float number: “);// Read float using %f format specifierscanf(“%f”, &fnum); // %f for single-precision floating-point number// Prompt user for double inputprintf(“Enter a double number: “);// Read double using %lf format specifierscanf(“%lf”, &dnum); // %lf for double-precision floating-point number// Display the entered values with labelsprintf(“nYou entered:n”);printf(“Integer: %dn”, num);printf(“Character: %cn”, ch);printf(“Float: %.2fn”, fnum); // %.2f specifies 2 decimal places for floatprintf(“Double: %.4lfn”, dnum); // %.4lf specifies 4 decimal places for doublereturn 0;}
5. Write a C program to print the address of variable. mention format specifier used in comment.
Answer:
#include <stdio.h>int main() {int num = 10;// Print the address of the variable ‘num’ using the %p format specifierprintf(“Address of num: %pn”, &num);return 0;}
Conclusion
We successfully completed Programming in C 312303 Manual Practical No.5 in which we learned how to Write well commented C programs. in above article you will find answers of all questions of this practical.