0% found this document useful (0 votes)
12 views3 pages

TP-05 DSP Loop Part-II

Uploaded by

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

TP-05 DSP Loop Part-II

Uploaded by

Da Vy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

វិទ្យាស្ថយនបច្ចេកវិទ្យាកម្ពុជា

Institute of Technology of Cambodia

TP-05
Loop (Part-II)
in C/C++

Academic Year: 2023 - 2024


ITC AMS

TP5-Using loop for


Lab’s objectives:
● Using loop for, nested loop
● Using keywords: continue; and break;
Deadline: 6 days
----------------------------------------------------------------------------------------------------------------

Problem1: Write a program to compute summation and subtraction of all numbers


from 500 to 10. Display output on screen.
Examples:
Output:
The summation: 500 + 499 + 498 + … + 10 = 125205
The subtraction: -500 - 499 - 498 - … - 10 = -125205

Problem2: Write a program to sum all numbers from n to 1000 except the number
100, where n is an integer number input by the user.
Examples:
INPUT:
Enter a number: 5
Output:
The summation from 5 to 1000 except the number 100 is: 500390
…………………………………………………………
INPUT:
Enter a number: 250
Output:
The summation from 250 to 1000 except the number 100 is: 469375

Problem3: Write a program to display all even numbers between 1 and 50 using a
for loop.
Examples:
Output:
The summation of all even numbers between 1 and 50: 2 + 4 + 6 + … 48
= 600

Problem4: Create a program that calculates and prints the power of 2 for the first 10
natural numbers using a for loop.
Examples:
Output:
The power of 2 for the first 10 natural numbers are:
21 = 2
22 = 4
23 = 8
24 = 16

Data Structure and Programming-I Page 2


ITC AMS

25 = 32
26 = 64
27 = 128
28 = 256
29 = 512
210 = 1024

Problem5: Create a program to ask a user to input a number. The program will tell
the user whether it is a prime number or not.

INPUT:
Enter a number: 5
Output:
5 is a prime number because it is only divisible by itself and 1.
…………………………………………………………
INPUT:
Enter a number: 12
Output:
12 is not a prime number because it is not only divisible by itself and 1.

Problem6: Create a program to identify and print all prime numbers between n and
m using a for loop, where n and m are integer numbers input by the user.
INPUT:
Enter n: 20
Enter m: 50
Output:
All prime numbers between 20 and 50 are: 23,29,31,37,41,43 and 47.
…………………………………………………………
INPUT:
Enter n: 2
Enter m: 100
Output:
All prime numbers between 2 and 100 are: 2, 3, 5, 7, 11, 13, 17, 19,
23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, and 97.

Data Structure and Programming-I Page 3

You might also like