Professional Documents
Culture Documents
Ex1: 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". Nested if statements
Input Description: Integer
Output Description: YOU WIN or YOU LOSE
Ex2: Write a program that asks the user to type an integer N and that indicates if N is a prime number
or not.
Note:
Input Description: integer
Output Description: it is a prime number or it is not a prime number
Ex3: Write a program that checks if the angles given can make a triangle or not (their sum must be 180
degrees).
Note:
Input Description:
integer
integer
integer
Note:
If the first number < 1 ---> print ERROR
Output correct to 4 digits after the comma.
Note:
If N<0 print "Error"
Input Description: integer N.
Output Description: N!.
Ex3: Write a program that asks the user to type the value of N and compute N!.
Note:
If N<0 print "Error"
Input Description: integer N.
Output Description: N!.
Ex3: Write a program that asks the user to type the values of x and n and compute the following
expressions:
Note:
Output correct to 4 digits after the comma, each printed on one line.
if n is not a positive integer, print out "Error"
Note:
Output correct to 4 digits after the comma, each printed on one line.
if n is not a positive integer, print out "Error"
expressions:
Ex6: Write a program that asks the user to type the values of x and n and compute the following
expressions:
Note:
Output correct to 4 digits after the comma, each printed on one line.
If n < 1 or x <0 print "Error"
Ex7: Write a program that asks the user to type the values of x and n and compute the following
expressions:
Note:
Output correct to 4 digits after the comma, each printed on one line.
If n < 1 or x <0 print "Error"
Ex8: Write a program that asks the user to type the values of x and n and compute the following
expressions:
Note:
Output correct to 4 digits after the comma, each printed on one line.
If n < 1 or x <0 print "Error"
Ex9: Write a program that asks the user to type the values of x and n and compute the following
expressions:
Note:
Output correct to 4 digits after the comma, each printed on one line.
If n < 1 or x <0 print "Error"
Lesson 5:
Ex1: Read n integers into an array, print the input values from last to first, then sum the elements of the
array.
Note:
If n <= 0 print "Error"
Input Description: The first line contains a positive integer n, which is the length of the input array.
The second line contains n integers.
Output Description: The first output line is the elements of the array printed in reversed order.
The second output line is the sum of elements of the array
Ex2: Compute the maximum value of an array. Return the index of the maximum value in an array.
Input Description: The first line contains a positive integer n, which is the length of the input
array.
The second line contains n elements of the array.
Output Description: The index of the maximum value in an array. If there are 2 or more elements
with the same value as the maximum value, print all the indices of those numbers out, separated
by spaces.
Note:
Value in an array count from 0
If n <= 0 print "Error"
Ex3: Read an integer n and type n integers into array A. Enter an integer V. Write a program that
searches if V is in array A. The program writes “V is in the array” or “V is not in the array”
Input Description: The first line contains a positive integer n, which is the length of the input
array.
The second line contains n integers which are elements of the array.
The third line contains an integer V.
Output Description: A string that has the format “V is in the array” or “V is not in the array”, with
the value of V in place of the letter V.
Lesson 6:
Output Description:
First-line contains N integers – elements of the array after sorting.
Ex2: Write a program that reads an array of N float numbers. Add one element x before the
element.
Note:
The elements of the array are indexed from 1 to N.
If k=n+1, insert x after the last element of the array.
Output Description: -First line contains N+1 elements of the array after inserting number x at
position
Line 2 to line M+1: Each line i (2<=i<=M+1) contains N integers denoting the row
of matrix X.
Line M+2 to line M+N+1: Each line i (M+2<=i<=M+N+1) contains K integers denoting
the row of matrix Y.
Output Description:
Line 1 to line M: Each line I (1<=i<=K) contains K integers denoting the row of matrix Z.
Lesson 7:
Ex1: Write a program that reads a string S and an integer N. Shorten the string so that it only contains
the first N character.
Note: If the string has fewer than N characters then do not change it
Input Description:
Output Description:
String S after being shortened.
Ex2: Write a program that reads two strings S and T. Check if string S contains string T.
Note:
Input Description:
String S
String T
Output Description: True or False
Ex3: Write a program that reads two strings S and T and an integer N. Check if string T occurs in the
suffix of S starting at position N.
Note:
Input Description:
String S
String T
Integer N
Output Description: Print “1” if string T occurs in string S after position N, else print “0”.
Ex4: Write a program that reads two strings S and T. Count the number of times that each character
appears in both strings.
Note First output a string of distinct characters in both strings. Then, for each of those characters
output the number of its occurrences in both strings.
Input Description:
String S
String T
Output Description:
Ex5: Write a program that reads a decimal number and converts it into binary.
Lesson 8:
Ex1: Write a C function that takes a positive integer n as an argument and returns the largest power of
2 less than or equal to n.
Input Description: A positive integer n
Output Description: The largest power of 2 less than or equal to n
Ex2: Write function prime that indicates an integer N is a prime number or not (return 1 if N is prime
and 0 otherwise)
Read m integers. Calculate and print an average of prime numbers using function prime Description
input: The first line contains a positive integer m. The second line contains m integers
Description output: Average of prime numbers in m integers using function prime. If there is no prime
number, print “Error”
a) Save the following numbers -1.5, -1, …, 100 to X. Calculate the values of f with the inputs are
elements of X and save to array Y.
b) Input an integer n (1 ≤ n ≤ 20). Save the elements of array Y greater than n to array Z and print out
their sum.
c) Sort array Z in ascending order.
a) Write the C function Comp_H(double, double) that receives 2 real numbers x, y, computes and
returns the value of H(x, y).
b) Write a program using the C function Comp_H(double) to compute the values of H(x, y) at the
following inputs: (-2, 5), (-1.5, 4), (-1, 3), …, (5, -9), (5.5, -10), (6, -11) (the difference between 2
successive x is 0.5, between 2 successive y is -1), then store all outputs in array B.
c) Display all B's elements in ascending order.
Lesson 9:
Input Description:
Enter 1st distance
Enter feet:
Enter inch:
Enter 2nd distance
Enter feet:
Enter inch:
Output Description:
Sum of distances = Result
Ex3: Use struct to enter information for different cities. Each city (city) has: city name (name), population
(population),total income (income).
Then show the city with the highest total income and the most populous city.
Example:
Input: 2
hanoi
1000
2000
hcm
2000
5000
Output:
The city with the largest population: hcm
The city with the highest total income: hcm