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

Programming 1

The document contains 46 questions about programming in C++, each asking the reader to write code to solve a programming problem or task. The questions cover a wide range of topics including variables, data types, conditional statements, loops, functions, input/output and more. The reader is asked to provide code snippets or full programs as answers to demonstrate their knowledge of programming concepts in C++.

Uploaded by

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

Programming 1

The document contains 46 questions about programming in C++, each asking the reader to write code to solve a programming problem or task. The questions cover a wide range of topics including variables, data types, conditional statements, loops, functions, input/output and more. The reader is asked to provide code snippets or full programs as answers to demonstrate their knowledge of programming concepts in C++.

Uploaded by

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

Programming

Q 1 : State that a program calculates the sum of two integers

ANS:

Q 2 : Declare the variable c , 76354 and the number to be of type int

ANS:

Q 3 : prompt the user to enter an integer. End your prompting


message with a colon (:) followed by a space and leave the cursor
positioned after the space

ANS :

Q 4 : Read an integer from the user at the keyboard and store the value
entered in integer variable age

Omar Sarwt
ANS :

Q 5 : if the variable number is not equal to 7 , print “The variable


number is not equal to 7 “.

ANS :

Q 6 : print the message “this is a c++ program” on one line

ANS :

Q 7 : print the message “this is a c++ program” on two line . End the
first line with c++

ANS :

Omar Sarwt
Q 8 : print the message “this is a c++ program” with each word on a
separate line

ANS :

Q 9 : print the message “this is a c++ program” with each word


separated from the next by a tab .
ANS :

Q 10 : State that a program calculates the product of three integers

ANS :

Q 11 : Write a program in C++ to calculate the volume of a sphere.

ANS :

Omar Sarwt
Q 12 : Write a program in C++ to convert temperature in Celsius to
Fahrenheit

ANS :

Q 13 : Program to print positive number entered by the user If user


enters negative number, it is skipped

ANS :

Q 14 : Program to check whether an integer is positive, negative or zero

ANS :

Omar Sarwt
Q 15 : Write a program in C++ to calculate Area and volume of circle.

ANS :

Q 16 : Write a program in C++ to find the Area and Perimeter of a


Rectangle

ANS :

Q 17 : Write a program in C++ to convert dollar to LE.

ANS :

Omar Sarwt
Q 18 : Write a C++ code that prompts the user to input two integer
values and find the greatest value.

ANS :

Q 19 : Write a program that determines a student’s grade. The program


will read student scores and determine the grade based on the
following rules: ( if the average score >=90 grade=A , if the average
score >= 70 and <90 grade=B , if the average score>=50 and <70
grade=C and if the average score<50 grade=F )

Ans :

Omar Sarwt
Q 20 : Write a C++ program to check whether a number is even or odd
using if else

ANS :

Q 21 : Write a C++ program to check whether an alphabet is vowel or


not.

ANS :

(OR)

Omar Sarwt
(OR)

Q 22 : write a c++ program to compare between two integers ,using if


statement and comparing with relational and equality operators

ANS :

Omar Sarwt
Q 23 : Write four different c++ statements that each add 1 to integer
variable x

ANS :

x=x+1;

x +=1 ;

x ++ ;

++x ;

Q 24 : in one statement , assign the sum of the current value of x and y


to z and postincrement the value of x

ANS :

z = x++ + y ;

Q 25 : Determine whether the value of the variable count is greater


than 10. If it is , print “count is greater than 10”

ANS :

‫ هنا هندي اي قيمه لل كونت بس بشرط‬: ‫ملحوووظه‬


‫ عشان الشرط يتحقق ويطبع الجمله‬10 ‫تكون اكبر من‬

Q 26 : predecrement the variable x by 1 , then subtract it from the


variable total

ANS :

Omar Sarwt
Q 27 : calculate the remainder after q is divided by divisor and assign
the result to q .Write this statement two different ways

ANS :

(OR)

Q 28 : complete the following tasks

a-Declare variables sum and x to be of type int

b-set variable x to 1

c- set variable sum to 0

d- add variable x to variable sum and assign the result to variable sum

e-print “the sum is: “ followed by the value of variable sum.

ANS :

Q 29 : combine the statements that you wrote in Q 28 into a program


That calculate and prints the sum of the integers from 1 to 10 . use the
while statement to loop through the calculation and increment
statements. The loop should terminate when the value of x becomes 11

Omar Sarwt
‫‪ANS :‬‬

‫‪Q 30 : state the values of each variabl after the calculation is performed‬‬
‫‪assume that , when each statement begins executing , all variables have‬‬
‫‪the integer value 5‬‬
‫‪X++‬‬
‫;‪a- product *= x++‬‬
‫يعني الزياده مبتحصلش فوقتها بمعني ان‬
‫;‪b-quotient /=++x‬‬ ‫االكس بتفضل بقيمتها القديمه وهنا‬
‫فالسؤال عندنا معلومه مهمه هي ان اي‬
‫‪ANS OF a :‬‬ ‫متغير قصده علي البرودكت واالكس‬
‫والكوتنينت كل منهم بيساوي ‪ 5‬وبكدا‬
‫‪Product = product * x++ = 5*5=25‬‬ ‫تكون قيمه االكس بكام ؟ هتبقي بتساوي‬
‫‪ 5‬عشان زي ماقولنا مبتحصلش الزياده‬
‫‪X=5‬‬
‫فساعتها‬
‫‪ANS OF b :‬‬

‫‪quotient = quotient / ++x =5/6=0.8333…..‬‬ ‫‪++x‬‬


‫اما ديه بقه بيحصل الزياده فساعتها‬
‫‪x=6‬‬
‫واحنا لسه قايلين ان اي متغير السؤال‬
‫مديلنا قيمته فخمسه بس بعد الزياده القيمه‬
‫هتبقي كام هتبقي بتساوي ‪6‬‬

‫‪Omar Sarwt‬‬
Q 31 : write a c++ program to print integer number from 1 to 10 , when
counter become greater than 10 end program . using for , while and
do..while statement.

ANS :

‫ملحوووووووووووظه‬

‫ اللي فالكود ده‬Double quotion‫ال‬

Omar Sarwt
‫وظيفته الوحيده انه هيعمل مسافه بين كل رقم ورقم عشان زي ماحنا عارفين ان اي حاجه جوا‬
‫عالمه التنصيص دول بتطبع زي ماهي فاحنا سبنا مسافه جوا عالمه التنصيص عشان تطبع‬
‫مسافه فبدون عالمه التنصيص مش هيطبع مسافه كدا وهتظهر االوت بوت كدا‬

‫اما في وجود عالمه التنصيص هتظهر االرقام كدا‬

Q 32 :

a- vary the control variable from 1 to 100 in increment of 1

ANS : for ( int i = 1 ; i <=100 ; i++)

b- vary the control variable from 100 down to 1 in decrement of 1

ANS : for ( int i =100 ; i >= 1; i--)

c- vary the control variable from 7 to 77 in steps of 7

ANS : for ( int I =7 ; i <=77; i+=7)

d- vary the control variable from 20 down to 2 in steps of -2

ANS : for ( int i =20; i >= 2 ; i-=2)

e- vary the control variable over the following squence of values :

2 , 5 , 8 , 11 , 14 , 17 , 20

ANS : For (int i =2; i <=20 ; i += 3)

f- vary the control variable over the following sequence of values :

99 , 88 , 77 , 66 , 55 , 44 , 33 , 22 , 11 , 0

ANS : For (int i =99 ; i>=0 ; i-=11)

Omar Sarwt
Q 33 : write a c++ program to sum the even integers from 2 to 20

ANS :

Q 34 : write a program to print integers from 1 to 10 ,but when count


equal to 5 , end the program and print “ BROKE OUT OF LOOP AT
COUNT “ .

ANS :

(OR)

Omar Sarwt
Q 35 : write a program to print integers from 1 to 10 except 5

,when count become greater than 10 end program and print

“used continue to skip printing 5 “.

ANS :

Q 36 : write a program to print the multiplication table of 8,

using goto statement

ANS :

Omar Sarwt
Q 37 : write a program to compute average of marks of 3 subjects for
“n” students

ANS :

Q 38 : write a program to find prime number between 2 to 100

ANS :

Omar Sarwt
Q 39 : sum the odd integers between 1 and 99 using for statement.
ASSUME the integer variable sum and count have been declared

ANS :

Q 40 : print the value 333.546372 in a field width of 15 characters with


precisions of 1,2 and 3 . print each number on the same line.
left-justify each number in its field. what three values print?

ANS :

Q 41 : calculate the value of 2.5 raised to the power 3 using function


pow. print the result with a precision of 2 in a field width of 10 position.
what prints ?

Omar Sarwt
ANS :

Q 42 : print the integers from 1 to 20 using a while loop and the


counter variable x. ASSUME that the variable x has been declared , but
not initialized. print only 5 integers per line . [ HINT : use the calculation
x%5. when the value of this is 0 , print a newline character , otherwise ,
print a tab character].

ANS :

Q 43 : reapet Q 43 using for statement.

ANS :

Omar Sarwt
Q 44 : Write a program to read an integer number from the user, and
then print the absolute.

ANS :

Q 45 : Write C++ Program to input number of week's day (1-7) and


translate to its equivalent name of the day of the week. in two ways

ANS :

(and)

Omar Sarwt
Q 46 : Write a C++ program of simple calculation that will get two
numbers and support all those operations (+, - , *, /).

ANS :

Q 47 : Write a program that reads a triangle sides (L1, L2, L3) and prints
these sides are :

• Equilateral, if (L1=L2 and L1=L3 and L2=L3)


• Isosceles, if (L1=L2 or L1=L3 or L2=L3)
• Scalene, if (L1 != L2 and L1 != L3 and L2 != L3)

ANS :

Omar Sarwt
Q 48 : Write program to print all numbers dividable by 10 from 0 to 100

ANS :

(OR)

Q 49 : Write C++ program read numbers from user and each time user
enter number program can decided if this number divided by 5 or 3
and user enter -1 for stop.

a-use while statement.

b-use do...while statement

c-use for statement

ANS :

Omar Sarwt
Q 50 : Write program to print multiplication table from 1 to 4 .

ANS :

Omar Sarwt
Q 51 : write program to print multiplication from user input and if input
is greater than 4 or less than 0 print “out of range” and end program.

ANS :

Q 52 : write program to print multiplication which user input

ANS :

Omar Sarwt
Q 53 : Write a program to display the numbers from 1 to 10, their
squares, and their cubes as a table by using while.

ANS :

Q 54 : To find factorial of a number in C++ programming, you have to


ask to the user to enter the number to find the factorial of the number
using for loop and display the factorial result of the number on the
output screen

ANS :

Q 55 : Write a program that prints ‘*’ according to the entered number


from the user

ANS :

Omar Sarwt
Q 56 : writeone or more statements that perform the folllowing tasks
for and array called fractions :

a- Dfine a constant variable arraysize initialized to 10


b- Declare an arrray with arraysize elements of type double , and
initialize the elements to 0
c- Name the forth element of the array
d- Refer to array element 4
e-assign the value 1.667 to array element 9
f- assign the value 3.333 to the seventh element of the array
g- print array elements 6 and 9 with two digits of precision to the right
of the decimal point , and show the output that is actually displayed
on the screen.
h- print all the array elements using a for statement. Define the integer
variable i as a control variable for the loop. show the output
ANS :

Omar Sarwt
Q 57 : Answer the following questions regarding an array called table:

a. Declare the array to be an integer array and to have 3 rows

and 3 columns. Assume that the constant variable arraySize has

been defined to be 3.

b. How many elements does the array contain?

c. Use a for repetition statement to initialize each element of the

array to the sum of its subscripts. Assume that the integer

variables i and j are declared as control variables.

d. Write a program segment to print the values of each element of

array table in tabular format with 3 rows and 3 columns.

Assume that the array was initialized with the declaration

e. int table[ arraySize ][ arraySize ] = { { 1, 8 }, { 2, 4, 6 }, { 5 } };

and the integer variables i and j are declared as control variables.

Show the output.

Omar Sarwt
ANS :

Omar Sarwt

You might also like