Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Assingment Pf 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Q no 1: Write a statement or a set of statements to accomplish each of the following tasks:

a) Sum the odd integers between 1 and 99 using a for statement. Use the unsigned integer
variables sum and count.

#include<stdio.h>
int main()
{
int sum=0;
for(int i=1;i<=99;i+=2)
{
sum=sum+i;
}
printf("sum is %d",sum);
}

b) Print the value 333.546372 in a field width of 15 characters with precisions of 1, 2, 3, 4

and 5. Left justify the output. What are the five values that print?

#include<stdio.h>
int main()
{
printf("%-15.1f\n", 333.546372);
printf("%-15.2f\n", 333.546372);
printf("%-15.3f\n", 333.546372);
printf("%-15.4f\n", 333.546372);
printf("%-15.5f\n", 333.546372);
}
b) Calculate the value of 2.5 raised to the power of 3 using the pow function. Print the re sult with a
precision of 2 in a field width of 10 positions. What is the value that prints?

#include<stdio.h>

int main()

printf("%10.2f\n", pow(2.5, 3)); // prints 15.63

c) Print the integers from 1 to 20 using a while loop and the counter variable x. Print only five
integers per line. [Hint: Use the calculation x % 5. When the value of this is 0, print a newline
character, otherwise print a tab character.]

#include<stdio.h>

int main()

unsigned int x = 1;

while (x <= 20) {

if (x % 5 == 0) {

printf("%u\n", x++);

else {

printf("%u\t", x++);

d) Repeat Exercise 4.3(d) using a for statement.

#include<stdio.h>

int main()

int x;
for (x = 1; x <= 20; x++) {

printf("%d\t", x);

if (x % 5 == 0) {

printf("\n");

Q no 2: Find the error in each of the following. (Note: There may be more than one error.)

a) for (a = 25, a <= 1, a--);


{
printf("%d\n", a);
}
Ans: The syntax of the for loop is incorrect. The correct syntax should be for(initialization; condition;
update). In this case, the initialization, condition, and update should be separated by semicolons (;).
However, in the given code, commas (,) are used instead of semicolons.

The condition a <= 1 in the for loop is always false because a is initialized to 25 and decremented by
a--. This means the loop condition will never be true, so the loop will not execute.

b). The following code should print whether a given integer is odd or even: switch (value) { case
(value % 2 == 0): puts("Even integer"); case (value % 2 != 0): puts("Odd integer"); }

1. Ans: Syntax Error in Switch Case: The case statements in a switch


statement in C cannot contain expressions. They must be constant
integer values.
2. Missing Break Statements: After each case block, there should be a
break; statement to exit the switch statement. Otherwise, if the
condition of the first case is true, it will continue executing the
subsequent cases.
b) The following code should calculate incremented salary after 10 years: for (int year = 1; year
<= 10; ++year) { double salary += salary * 0.05; } printf("%4u%21.2f\n", year, salary);
1. Ans: Variable Scope: The variable year is declared within the for loop,
so it's not accessible outside of the loop. Therefore, it cannot be used
in the printf statement outside of the loop.
2. Initialization of salary: The salary variable is being incremented within
the loop, but it's not initialized before the loop starts. This will result in
undefined behavior.

d) for (double y = 7.11; y != 7.20; y += .01) printf("%7.2f\n", y);


d) for (double y = 7.11; y != 7.20; y += .01) printf("%7.2f\n", y);
Ans: The error in the provided code snippet is in the termination condition of
the for loop:
for (double y = 7.11; y != 7.20; y += 0.01)

e) The following code should output all multiples of 3 from 1 to 100: for (int x = 3; x <=
100; x%3 == 0; x++ ) { printf("%d\n", x); }
Ans: The for loop syntax in the given code has an error in the condition part.
Instead of using x%3 == 0 as the condition, it should be x % 3 == 0 (using
the equality operator == instead of the assignment operator =), and it
should be separated by a semicolon ; not a comma ,.

f) x = 1; while ( x <= 10 ) { printf("%d\n", x); }


1. Ans: The loop will run indefinitely because x is not being incremented
inside the loop. So, x will always remain 1, and the condition x <= 10
will always be true.
2. The code is only printing the value of x itself, not its multiples or
anything related to multiples of 3 as intended.

g) The following code should sum the squares of all numbers from 1 to 50 (assume sum is
initialized to 0): for (x = 1; x == 50; ++x) { sum =+ x * x; }
1. Ans; In the for loop condition, it should be x <= 50 instead of x == 50.
The loop should continue as long as x is less than or equal to 50.
2. The assignment sum =+ x * x; should be sum += x * x; to correctly
accumulate the sum of squares. The += operator adds the value on
the right to the variable on the left, whereas =+ is not a valid operator.
3.
Q no 3: State which values of the control variable x are printed by each of the following for
statements:
a) for (x = 20; x >= 3; x –= 3) { printf("%u\n", x); }

Ans: 20,17,14,11,8,5

c) for (x = 7; x <= 27; x += 5) { printf("%u\n", x); }

Ans: the values of x that are printed by this loop are 7, 12, 17, 22, and 27.

d) for (x = 2; x <= 20; x += 4) { printf("%u\n", x); }


Ans:2,6,10,14,18

e) for (x = 30; x >= 15; x –= 6) { printf("%u\n", x); }

Ans:30,24,18

f) for (x = 22; x >= 2; x –= 5) { printf("%d\n", x); }

Ans:22,17,12,7,2

Qno4: Write for statements that print the following sequences of values:

a) 1, 3, 5, 7, 9, 11, 13

#include <stdio.h>

int main() {

int x;

for (x = 1; x <= 13; x += 2) {

printf("%d ", x);

printf("\n");

b) 2, 5, 8, 11, 14, 17

#include <stdio.h>

int main() {

int x;

for (x = 2; x <= 17; x += 3) {

printf("%d\n", x);

}
c) 30, 20, 10, 0, –10, –20, –30

#include <stdio.h>

int main() {

int x;

for (x = 30; x >= -30; x -= 10) {

printf("%d\n", x);

d) 15, 23, 31, 39, 47, 55

#include <stdio.h>

int main() {

int x;

for (x = 15; x <= 55; x += 8) {

printf("%d\n", x);

Qno5: Write a program to calculate and print a list of all prime numbers from 1 to 100.

#include <stdio.h>

bool isPrime(int n) {

if (n <= 1) {

return false;

}
for (int i = 2; i * i <= n; i++) {

if (n % i == 0) {

return false;

return true;

int main() {

printf("Prime numbers from 1 to 100:\n

int number = 1;

while (number <= 100) {

if (isPrime(number)) {

printf("%d\n", number);

number++;

You might also like