You are on page 1of 1

Sheet

LOOP 1:
1) Find the error in the following code segments and
correct it
1- for i=0; i<5; i++
cout>>”Hello”;
2- for (i=0; i<5; i++) ;
cout>>”Hello”;
3- for (i=0; i>5; i++)
cout<<”Hello”;
4- for (i=0; i<5; i--)
cout<<”Hello”;

2) Write a program to print the sum of all squares between 1 and n, where n
is a number accepted from the keyboard (i.e. 1+ 4 + … + (n*n) )
3) Write a program to compute the factorial of a number using loops

4) Write a code segment that computes 1 + 2 + 3 + …… + (n – 1) + n. Where n


is an integer. Then compare the result with ( n * ( n – 1 ) ) / 2, and displays
a message that indicates whether the values are the same or different.
5) Write a program to calculate.
1 1 1 1
𝑆𝑢𝑚 = 1 + + + + ⋯. +
2 3 4 20
6) Write a program segments that displays the following use for loops:
a. Even integer from 1 to 100
b. Sum of odd numbers from 1 t0 99
c. All numbers divisible by 3 in range 1 to 60
7) Write a complete program that contains an input loop which reads two
integers until the first integer evenly divides the second

You might also like