IntAlgorithms Vize Cevapanahtari
IntAlgorithms Vize Cevapanahtari
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.
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;
}
1
2022-2023 Fall Semester
Introduction to Algorithms Midterm Exam - November 9, 2022
Instructor: Assistant Prof. Dr. Şafak DURUKAN ODABAŞI
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";
}
}