Assingment Pf 2
Assingment Pf 2
Assingment Pf 2
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);
}
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()
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;
if (x % 5 == 0) {
printf("%u\n", x++);
else {
printf("%u\t", x++);
#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.)
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"); }
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 ,.
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
Ans: the values of x that are printed by this loop are 7, 12, 17, 22, and 27.
Ans:30,24,18
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;
printf("\n");
b) 2, 5, 8, 11, 14, 17
#include <stdio.h>
int main() {
int x;
printf("%d\n", x);
}
c) 30, 20, 10, 0, –10, –20, –30
#include <stdio.h>
int main() {
int x;
printf("%d\n", x);
#include <stdio.h>
int main() {
int x;
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() {
int number = 1;
if (isPrime(number)) {
printf("%d\n", number);
number++;