0% found this document useful (0 votes)
18 views

Practical Work 2

The document discusses loops in C programming, providing examples of for, while, and do-while loops. It lists 9 exercises involving writing C programs that use loops to calculate sums, factorials, powers, multiples, and maximum/minimum values. It also includes an algorithm to sum values that are multiples of both 2 and 3 from 1 to 10 and asks the reader to run the algorithm for sample inputs and describe what it does.

Uploaded by

dalila bouras
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Practical Work 2

The document discusses loops in C programming, providing examples of for, while, and do-while loops. It lists 9 exercises involving writing C programs that use loops to calculate sums, factorials, powers, multiples, and maximum/minimum values. It also includes an algorithm to sum values that are multiples of both 2 and 3 from 1 to 10 and asks the reader to run the algorithm for sample inputs and describe what it does.

Uploaded by

dalila bouras
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Faculty of Technology TC/ST Department Computer Science 2 module 2023/2024

Dr Bouras…. Dr Guendouz… Session E.B.H.C.D

THE LOOPS STRUCTURES


Reminder
• A loop is an instruction that allows you to repeat the execution of one or more other instructions a certain number
of times or depending on a certain condition.
• We have three types of loops in C environment: For, While and Do….While.

Activity 1: write a C program that: with: N a positive integer.

1. Write a C program to display the first N odd if it equals to its divisors. for example: 6 is
perfect because 1+2+3=6.
natural numbers and their sum, S=1+3+5+…...
7. Write a C program that successively requests 20
2. Calculates and displays the sum
user's numbers, and then presents what was the
S= 22+42+62+…… by taking N term. maximum among these 20 numbers. Then
modify the program so that it also displays in
3. Reads a positive integer N and then displays its
which position this number was entered.
factorial. N! = N*(N-1) *…..*3*2*1.With 0!=1 8. Rewrite the previous program, but this time, we

4. Calculates the power XN of a real number X. do not know in advance how much numbers the
user wants to enter. The entry of numbers stops
XN = X*X*X*….*X, N times. Home work1
when the user enters a zero.
5. Write an algorithm that displays the integers 9. write a program that asks for a starting number
which are multiples of 3 and less than a number and then displays the next ten numbers for
N given by the user. example, if the user enters the number 17, the
6. write a C program that displays the divisors of program will display the numbers from 18 to
an integer N. modify this program to determine 27. Home work 2
whether a number p entered from the keyboard
is perfect. We say that a given number is perfect

Activity 2: Consider the following algorithm:


Algorithm Activity3; Questions:
Variables i, x, sum : integer;
Start 1- Run the algorithm for the following values
sum ←0; of x: {1, 6, 2, 3, 4, 10, 12, 6, 24, 5}.
For i←1 to 10 2- What does this algorithm do?
Read(x);
if ((x Mod 3=0) and (x Mod 2=0)) then
sum ← sum+x;
EndFor;
Print(sum);
End.
1 /1

You might also like