0% found this document useful (0 votes)
65 views12 pages

Exercise C++

This document contains 31 programming exercises involving concepts like loops, conditionals, functions, and arrays. For each exercise, the problem is described and a sample solution is provided. The exercises cover a range of skills from basic input/output and conditionals to more complex topics like prime number checks, factorials, and mathematical sequences.

Uploaded by

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

Exercise C++

This document contains 31 programming exercises involving concepts like loops, conditionals, functions, and arrays. For each exercise, the problem is described and a sample solution is provided. The exercises cover a range of skills from basic input/output and conditionals to more complex topics like prime number checks, factorials, and mathematical sequences.

Uploaded by

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

Exercise 1

Write a program that asks the user to type an integer and writes
"YOU WIN" if the value is between 56 and 78 (both included). In
the other case it writes "YOU LOSE".

Solution

Exercise 2

Write a program that asks the user to type all the integers
between 8 and 23 (both included) using a for loop.

Solution

Failed solutions (correct them as an extra exercise)

Exercise 3

Same as Exercise 2 but you must use a while.

Solution

Exercise 4
Write a program that asks the user to type 10 integers and writes
the sum of these integers.

Solution

Author: jimmymass;

Exercise 5

Write a program that asks the user to type 10 integers and writes
the smallest value.

Solution

Exercise 6

Write a program that asks the user to type an integer N and


computes the sum of the cubes from 53 to N3.

Solution

Exercise 7
Write a program that asks the user to type an integer N and
compute u(N) defined with :

u(0)=3

u(n+1)=3*u(n)+4

Solution

Exercise 8

Write a program that asks the user to type an integer N and


compute u(N) defined with :

u(0)=1

u(1)=1

u(n+1)=u(n)+u(n-1)

Solution

Exercise 9

Write a program that asks the user to type an integer between 0


and 20 (both included) and writes N+17. If someone types a
wrong value, the program writes ERROR and he must type
another value.

Solution
Exercise 10

Write a program that is able to compute some operations on an


integer. The program writes the value of the integer and writes
the following menu :

1. Add 1

2. Multiply by 2

3. Subtract 4

4. Quit

The programs ask the user to type a value between 1 and 4. If


the user types a value from 1 to 3 the operation is computed, the
integer is written and the menu is displayed again. If the user
types 4, the program quits.

Solution

Exercise 11

Write a program that asks the user to type a positive integer.


When the user types a negative value the program writes ERROR
and asks for another value. When the user types 0, that means
that the last value has been typed and the program must write
the average of the positive integers. If the number of typed
values is zero the program writes 'NO AVERAGE'.
Solution

Exercise 12

Write a program that asks the user to type an integer N and


compute u(N) defined with :

u(0)=3

u(1)=2

u(n)=n*u(n-1)+(n+1)*u(n-2)+n

Solution

Exercise 13

Write a program that asks the user to type 10 integers and write
the number of occurrence of the biggest value.

Solution

Exercise 14

Write a program that asks the user to type the value of N and
computes N! .
Solution

Exercise 15

Write a program that asks the user to type an integer N and that
indicates if N is a prime number or not.

Solution

Exercise 16

Write a program that asks the user to type an integer N and that
writes the number of prime numbers lesser or equal to N.

Solution

Exercise 17

Write a program that asks the user to type the value of an


integer N and compute the N-st prime number.

Solution

Exercise 18
Write a program that asks the user to type the value of N and
writes this picture :

N=1

N=2

**

N=3

***

**

and so on...

Solution

Exercise 19

Write a program that asks the user to type the value of N and
display an output like the following:

N=1
*

N=2

**

N=3

***

**

and so on ...

Solution

Exercise 20

u(n) is defined with:

u(0) = a (a is an integer)

if u(n) is even then u(n+1)=u(n)/2, else u(n+1)=3*u(n)+1

Conjecture: For all value of a, there exists a value N such that


u(N)=1.
a)Write a program that asks the user to type the value of an
integer a and writes all the values of u(n) from n=1 to n=N.

Solution

b) Write a program that asks the user to type a value M and


computes the value of a from 2 and M that maximizes the value
of N. This value is called A. The program must write the value of
A and N.

Solution

Exercise 21

Request the user to type numbers, each time printing its triple,
until the user enters -999.

Solution

Exercise 22

Request the user to type positive numbers until either a zero or a


negative is typed, and then show the user how many positives
were typed in.

Solution

Exercise 23

Request the user to type positive numbers, or to stop by typing a


number smaller than 1. Print the average.

Solution

Exercise 24

Request the user to type numbers, or type 0 to stop. Show how


many numbers were between 100 and 200 (both included).

Solution

Exercise 25

The country A has 50M inhabitants, and its population grows 3%


per year. The country B, 70M and grows 2% per year. Tell in how
many years A will surpass B.
Solution

Exercise 26

Write a program that asks the user to input 5 sequences of


characters. Then it will ask the user for a character to search for
and will output the maximum number of times that it occurred
between the 5 sequences. Example: Sequence 1: aabc Sequence
2: aaaa If the user chooses to search for character 'a', the
program will output "Character a occurred a maximum of 4
times" because it occurred 4 times in the second sequence, and
only twice in the first sequence.

Solution

Exercise 27

Write a program that asks the user to type a random number.


Write in the output if this number is a prime number or not.
Write which numbers your number can be divided by.

Solution

Exercise 28

Write a program for finding the amount of a specific character


inside a string.
Solution

Exercise 29

Write a Program to find out no. of Even & Odd no. in a given
Data Series.

Solution

Exercise 30

Write a Program to count Zero, Positive, Negative no. in a given


Data Series.

Solution

Exercise 31

Write a program to count the numbers which are divisible by 3 in


a given Data Series.

Solution

You might also like