0% found this document useful (0 votes)
7 views2 pages

IntAlgorithms Vize Cevapanahtari

Uploaded by

alikemalerdem2
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)
7 views2 pages

IntAlgorithms Vize Cevapanahtari

Uploaded by

alikemalerdem2
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/ 2

2022-2023 Fall Semester

Introduction to Algorithms Midterm Exam - November 9, 2022


Instructor: Assistant Prof. Dr. Şafak DURUKAN ODABAŞI

Name-Surname:………………………………………………………………… Student ID:…………………………………………


Duration: 75 min. Good Luck!

1. [20 pts] Fill in the blanks in each of the following using given keywords
Keywords: if, else-if, variable, switch-case, void, array, main, constant, int, and, && (logical and), || (logical or),
function
a. A variable is a memory location, with a name and a type, that stores different values in each moment of a
program’s execution.

b. main is the only function all C++ programs must contain.


c. The switch-case statement selects among multiple actions based on the possible values of an integer variable or
expression.
d. A constant is a memory location where a value can be stored, but content of this location cannot be changed.
2. [ 20 pts] State whether each of the following is true or false.
a. [ F ] In C++, x++ is equivalent to x = x + 2;
b. [ F ] C++ just supports single line comments.
c. [ T ] = = is used for comparison, whereas, = is used for assignment of two quantities.
d. [ T ] _MyName is a valid variable name in C++.

3. [12 pts] Identify and correct the errors in each of the following statements.
a. if (c < 7); if (c < 7)
{ {
cout << "c is less than 7\n"; cout << "c is less than 7\n";
} }
b. Int main(){ int main(){
Int a; int a=2;
+a; ++a;
cout<<a; cout<<a;
}
c. else if (c <= 5) { else if (c <= 5) {
product *= c; product *= c;
++c; ++c;
}

4. [12 pts] Answer the following questions


a. Write the output of following piece of code. 1-0
int x = 1, y = 0;
if (x > 0 && y < 0) {
x = y = 23;
}
cout << x << "-" << y << endl;
b. Rewrite the following if statement using int x;
switch-case statement : cin>>x;
int x; switch(x){
cin>>x;
case 3: cout<<”x is 3”;break;
if(x==3)
cout<<”x is 3”; case 4: cout<<”x is 4”;break;
else if(x==4)
cout<<”x is 4”; case 5: cout<<”x is 5”;break;
else if(x==5) default: cout<<”Please enter a number between 3-5”;
cout<<”x is 5”;
else }
cout<<”Please enter a number between 3-5”;

1
2022-2023 Fall Semester
Introduction to Algorithms Midterm Exam - November 9, 2022
Instructor: Assistant Prof. Dr. Şafak DURUKAN ODABAŞI

Name-Surname:………………………………………………………………… Student ID:…………………………………………


Duration: 75 min. Good Luck!

5. [36 pts] Write a complete C++ program to prompt the user to enter an integer . Your program determines
whether this integer is a prime number or not.

A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural
numbers.
Example output:
> Enter an integer: > Enter an integer:
> 13 > 12
> 13 is a prime number. > 12 is not a prime number.

#include <iostream>
using namespace std;

int main() {
int num,isPrime=0;
cout<<"Enter an integer:";
cin>>num;

if(num==1 || num==2)
cout<<num<<" is a prime number";
else{
for(int i=2;i<num;++i)
if(num%i==0)
isPrime=1;
if(isPrime==0)
cout<<num<<" is a prime number";
else
cout<<num<<" is not a prime number";

}
}

You might also like