CSC 212 Problems For Chapter 2, Spring 2004
Notre Dame University–Louaize · Program Design and Data Abstraction I · 4 pages
More CSC 212 files
Show the text of this chapter
Exercice Chapter #2
1
}
}
What is the output of the following program?
#include <iostream.h>
void main ( ) {
int x=10;
while ( x )
//>0
cout<< --x << “ “;
}
What is the output of the following program?
#include <iostream.h>
void main ( ) {
int j;
for (j=1; j<=10; j++) {
if (j>5 && j<=7)
cout<< j <<endl;
}
cout<<endl;
}
What is the output of the following program?
#include <iostream.h>
void main ( ) {
int j;
for (j=1; j<=10; j++) {
if (j>5 || j<=7)
cout<< j <<endl;
}
cout<<endl;
}
What is the output of the following program?
#include <iostream.h>
void main ( ) {
int j;
for (j=1; j<=10; j++) {
if (j<5 || j>=7)
cout<< j <<endl;
}
cout<<endl;
}
Exercice Chapter #2
2
What is the output of the following program?
#include <iostream.h>
void main ( ) {
int j;
for (j=1; j<=10; j++) {
if (!(j<5 || j>=7))
cout<< j <<endl;
}
cout<<endl;
}
What is the output of the following program?
#include <iostream.h>
void main ( ) {
int i, j, k;
for (i=1; i<=2 ; i++)
for (j=1; j<=2 ; j++)
for (k=1; k<=3 ; k++)
cout<<k<<” “;
}
What is the output of the following program?
#include<iostream.h>
int main( ) {
int count=0;
while (count++ <=5)
cout<<(count%2 ? ”***” : ”---“ )<<endl; //!=0
cout<<count<<endl;
return 0;
}
What is the output of the following program?
#include<iostream.h>
int main(){
int j=10,b=5;
for (; b<=j; j-=1)
{
if (j % b==0 && j*5<=25)
continue;
if (j<7 || j>=9)
cout<< j << endl;
Exercice Chapter #2
3
}
return 0;
}
What is the output of the following program?
#include <iostream.h>
void main() {
for (int i = 45; i > 1; i = i / 5 )
cout<< i++<<endl;
cout<<"continue";
}
What is the output of the following program?
#include <iostream.h>
void main ( ) {
int i, j, k;
int x=0;
for (i=1; i<=5 ; i++)
for (j=1; j<=7 ; j+=2)
for (k=1; k<15 ; k+=3)
x++;
cout << x <<endl;
}
What is the output of the following program?
#include <iostream.h>
void main() {
int x;
for (x=1; x<=5 ;x++) {
switch(x) {
case 1:
cout << x << endl;
case 2:
cout << x << endl;
case 3:
cout << x << endl;
break;
default:
cout << x+1 << endl;
}
}
}
Exercice Chapter #2
4
Problems:
1- The factorial of a nonnegative integer n is written n! (Pronounced “n factorial”) and is
defined as follows:
n! = n .(n-1) .(n-2). ... .1
and
n! = 1 for n=0
For example 5! = 5.4.3.2.1 which is 120
Write a program that reads a nonnegative integer and computes and prints its factorial
2 - Write a a C++ program that inputs a series of 10 numbers, and determines and prints
the largest of the numbers.
3-Write a program that reads in the size of the side of a square and then prints a hollow
square of that size out of asterisks and blanks. Your program should work for squares of
all side sizes between 1 and 20. For example, if your program reads a size of 5, it should
print
* * * * *
* *
* *
* *
* * * * *