0% found this document useful (0 votes)
3 views4 pages

loop_exercises

The document contains C++ programming exercises focused on loop structures, including tasks for calculating the sum of the first N natural numbers, computing the factorial of a number, and checking if a number is prime. Each exercise includes example inputs and expected outputs to guide implementation. The exercises are designed to enhance understanding of basic programming concepts in C++.
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)
3 views4 pages

loop_exercises

The document contains C++ programming exercises focused on loop structures, including tasks for calculating the sum of the first N natural numbers, computing the factorial of a number, and checking if a number is prime. Each exercise includes example inputs and expected outputs to guide implementation. The exercises are designed to enhance understanding of basic programming concepts in C++.
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/ 4

C++ Programming Exercises: Loop Structures

1. Sum of First N Natural Numbers

Write a program that reads an integer N and calculates the sum of the first N natural numbers.

Example: For N = 5, the sum is 1 + 2 + 3 + 4 + 5 = 15.

Test Cases:

Input: 5, Expected Output: 15

Input: 10, Expected Output: 55

Input: 0, Expected Output: 0

Input: 1, Expected Output: 1

Input: 20, Expected Output: 210

Input: 50, Expected Output: 1275

Input: 100, Expected Output: 5050

Input: 7, Expected Output: 28

Input: 3, Expected Output: 6


C++ Programming Exercises: Loop Structures

Input: 2, Expected Output: 3

2. Factorial of a Number

Write a program that reads an integer N and calculates the factorial of N.

Example: For N = 5, the factorial is 5! = 5 x 4 x 3 x 2 x 1 = 120.

Test Cases:

Input: 5, Expected Output: 120

Input: 0, Expected Output: 1

Input: 1, Expected Output: 1

Input: 3, Expected Output: 6

Input: 7, Expected Output: 5040

Input: 4, Expected Output: 24

Input: 6, Expected Output: 720

Input: 8, Expected Output: 40320


C++ Programming Exercises: Loop Structures

Input: 9, Expected Output: 362880

Input: 10, Expected Output: 3628800

3. Check Prime Number

Write a program that checks whether a given number N is a prime number or not.

Example: For N = 7, the output is True because 7 is a prime number.

Test Cases:

Input: 2, Expected Output: True

Input: 3, Expected Output: True

Input: 4, Expected Output: False

Input: 17, Expected Output: True

Input: 20, Expected Output: False

Input: 19, Expected Output: True

Input: 1, Expected Output: False


C++ Programming Exercises: Loop Structures

Input: 0, Expected Output: False

Input: 23, Expected Output: True

Input: 15, Expected Output: False

You might also like