0% found this document useful (0 votes)
3 views7 pages

Assignment FormatSpecifiers

Format specifiers

Uploaded by

iamtaharaoo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Assignment FormatSpecifiers

Format specifiers

Uploaded by

iamtaharaoo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

University of Southern Punjab

Department of Computer Science and IT

Course Title: Programming Fundamentals


Assignment Topic: Format Specifiers (10 Marks)
Submitted By: ___________________________
Registration No: ___________________________
Section: BSCS 1st / BSIT 1st
Submission Date: 21 APR 2026 / 23 APR 2026

________________________________________________________________________

Assignment: Format Specifiers (10 Marks)

Question No. 1 (CLO1/C2)


Point out the errors in the following programs if any, and display the correct output.

Part (a):
Original Code:
#include <stdio.h>
int main()
{
int code, flag;
if ( code == 1 & flag == 0 )
printf ( "The eagle has landed\n" );
return 0;
}

Error Found:
The condition uses a single & (bitwise AND) instead of && (logical AND). The correct
operator for combining two logical conditions in C is &&.

Corrected Code:
#include <stdio.h>
int main()
{
int code = 1, flag = 0;
if ( code == 1 && flag == 0 )
printf ( "The eagle has landed\n" );
return 0;
}
Output:
The eagle has landed

Part (b):
Original Code:
#include <stdio.h>
int main()
{
int i = 10, j = 20;
if ( i = 5 ) && if ( j = 10 )
printf ( "Have a nice day\n" );
return 0;
}

Errors Found:
1. The if condition uses = (assignment) instead of == (comparison).
2. The two conditions are written as two separate if statements joined by && which is a
syntax error. They must be combined inside a single if statement.

Corrected Code:
#include <stdio.h>
int main()
{
int i = 10, j = 20;
if ( i == 5 && j == 10 )
printf ( "Have a nice day\n" );
return 0;
}

Output:
(No output, because i is 10 and j is 20, so the condition is false.)

Part (c):
Original Code:
#include <stdio.h>
int main()
{
int i = 4, j = -1, k = 0, w, x, y, z;
w = i || j && k;
x = i && j || k;
y = i || j || k;
z = i && j && k;
printf("w=%d x=%d y=%d z=%d\n", w, x, y, z);
return 0;
}
Error Analysis:
No syntax errors in part (c). The program is correct. The output depends on operator
precedence: && binds tighter than ||.

Step-by-step Evaluation:
i = 4 (true), j = -1 (true), k = 0 (false)
w = i || (j && k) = true || (true && false) = true || false = 1
x = (i && j) || k = (true && true) || false = true || false = 1
y = i || j || k = true || true || false = 1
z = i && j && k = true && true && false = 0

Output:
w=1 x=1 y=1 z=0

Question No. 2 (CLO2/C3)


Write a program to check whether a triangle is valid or not. A triangle is valid if the sum of
all three angles equals 180 degrees.

Algorithm:
Step 1: Start
Step 2: Input angle1, angle2, angle3
Step 3: Calculate sum = angle1 + angle2 + angle3
Step 4: If sum == 180, display 'Valid Triangle'
Else display 'Invalid Triangle'
Step 5: Stop

Program (C):
#include<stdio.h>
int main()
{
int angle1, angle2, angle3, sum;

printf("Enter the three angles of the triangle: ");


scanf("%d %d %d", &angle1, &angle2, &angle3);

sum = angle1 + angle2 + angle3;

if(sum == 180)
printf("The triangle is Valid.\n");
else
printf("The triangle is Invalid.\n");
return 0;
}

Sample Output:
Enter the three angles of the triangle: 60 60 60
The triangle is Valid.

Question No. 3 (CLO2/C3)


A certain grade of steel is graded according to the following conditions:
i. Hardness must be greater than 50
ii. Carbon must be less than 0.7
iii. Tensile strength must be greater than 5600

Grades: 10 (all three), 9 (i & ii), 8 (ii & iii), 7 (i & iii), 6 (only one), 5 (none).

Program (C):
#include<stdio.h>
int main()
{
float hardness, carbon, tensile;
int i, ii, iii, grade;

printf("Enter Hardness: ");


scanf("%f", &hardness);
printf("Enter Carbon content: ");
scanf("%f", &carbon);
printf("Enter Tensile Strength: ");
scanf("%f", &tensile);

i = (hardness > 50);


ii = (carbon < 0.7);
iii = (tensile > 5600);

if(i && ii && iii)


grade = 10;
else if(i && ii)
grade = 9;
else if(ii && iii)
grade = 8;
else if(i && iii)
grade = 7;
else if(i || ii || iii)
grade = 6;
else
grade = 5;
printf("The grade of the steel is: %d\n", grade);
return 0;
}

Sample Output:
Enter Hardness: 55
Enter Carbon content: 0.5
Enter Tensile Strength: 6000
The grade of the steel is: 10

Question No. 4 (CLO2/C3)


The weight class of a boxer is decided per the following table:

Boxer Class Weight in Pounds


Flyweight < 115
Bantamweight 115 - 121
Featherweight 122 - 153
Middleweight 154 - 189
Heavyweight >= 190

Program (C):
#include<stdio.h>
int main()
{
float weight;

printf("Enter weight in pounds: ");


scanf("%f", &weight);

if(weight < 115)


printf("Boxer Class: Flyweight\n");
else if(weight >= 115 && weight <= 121)
printf("Boxer Class: Bantamweight\n");
else if(weight >= 122 && weight <= 153)
printf("Boxer Class: Featherweight\n");
else if(weight >= 154 && weight <= 189)
printf("Boxer Class: Middleweight\n");
else
printf("Boxer Class: Heavyweight\n");

return 0;
}

Sample Output:
Enter weight in pounds: 130
Boxer Class: Featherweight
Question No. 5 (CLO2/C3)
The Body Mass Index (BMI) is defined as the ratio of the weight of a person (in kilograms)
to the square of the height (in meters). Write a program that receives weight and height as
input, calculates the BMI, and reports the BMI category.

BMI Category BMI


Starvation < 15
Anorexic 15.1 to 17.5
Underweight 17.6 to 18.5
Ideal 18.6 to 24.9
Overweight 25 to 25.9
Obese 30 to 30.9
Morbidly Obese >= 40

Algorithm:
Step 1: Start
Step 2: Input weight (kg) and height (m)
Step 3: Calculate BMI = weight / (height * height)
Step 4: Compare BMI to table ranges and display category
Step 5: Stop

Program (C):
#include<stdio.h>
int main()
{
float weight, height, bmi;

printf("Enter weight in kilograms: ");


scanf("%f", &weight);
printf("Enter height in meters: ");
scanf("%f", &height);

bmi = weight / (height * height);


printf("Your BMI is: %.2f\n", bmi);

if(bmi < 15)


printf("BMI Category: Starvation\n");
else if(bmi <= 17.5)
printf("BMI Category: Anorexic\n");
else if(bmi <= 18.5)
printf("BMI Category: Underweight\n");
else if(bmi <= 24.9)
printf("BMI Category: Ideal\n");
else if(bmi <= 25.9)
printf("BMI Category: Overweight\n");
else if(bmi <= 30.9)
printf("BMI Category: Obese\n");
else
printf("BMI Category: Morbidly Obese\n");

return 0;
}

Sample Output:
Enter weight in kilograms: 70
Enter height in meters: 1.75
Your BMI is: 22.86
BMI Category: Ideal

________________________________________________________________________

References:
1. Brian W. Kernighan & Dennis M. Ritchie, "The C Programming Language", Prentice Hall.
2. Yashavant Kanetkar, "Let Us C", BPB Publications.
3. Class Lecture Notes – Programming Fundamentals, Dept. of CS & IT, USP.

--- End of Assignment ---

You might also like