






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This is code assigned in lab of course. It was assigned by Jay Singh at Aligarh Muslim University for Data Structure course. It includes: Array, Class, Objects, Data, Type, Position, Static, members, Variables, Global, Initialization
Typology: Exercises
1 / 10
This page cannot be seen from the preview
Don't miss anything!







1 # include < iostream > 2 3 using namespace std ; 4 5 int main () 6 { 7 const char * str = " Hello , World! " ; 8 cout << str << " \ n " ; 9 return 0; 10 }
1 # include < iostream > 2 3 using namespace std ; 4 5 int main () 6 { 7 int N ; 8 cin >> N ; 9 for (; N - - > 0;) 10 { 11 cout << " Hello , World !\ n " ; 12 } 13 return 0; 14 }
1 # include < iostream > 2 3 using namespace std ; 4 5 int main () 6 { 7 int N ; 8 cin >> N ; 9 while (N - - > 0) 10 { 11 cout << " Hello , World !\ n " 12 } 13 return 0; 14 }
using namespace std ;
int main () { int N ; cin >> N ; do { cout << " Hello , World !\ n " ; } while ( - - N > 0) ; return 0; }
29 maxVal = a ; 30 } 31 } 32 33 cout << " Mean : " << ( double ) acc / N << " \ n " ; 34 cout << " Max : " << maxVal << " \ n " ; 35 cout << " Min : " << minVal << " \ n " ; 36 cout << " Range : " << ( maxVal - minVal ) << " \ n " ; 37 38 return 0; 39 }
2.3 Prime Numbers (10 points)
1 # include < iostream > 2 3 using namespace std ; 4 5 int main () 6 { 7 int N ; 8 cin >> N ; 9 for ( int i = 2; N > 0; ++ i ) 10 { 11 bool isPrime = true ; 12 for ( int j = 2; j < i ; ++ j ) 13 { 14 if ( i % j == 0) 15 { 16 isPrime = false ; 17 break ; 18 } 19 } 20 if ( isPrime ) 21 { 22 --N ; 23 cout << i << " \ n " ; 24 } 25 } 26 return 0; 27 }
2.4 Multiples of numbers (10 points)
1 # include < iostream > 2 3 using namespace std ; 4 5 int main () 6 { 7 while (1) 8 { 9 int N ; 10 cin >> N ; 11 cout << (( N % 5 == 0 && N >= 0)? N /5 : -1) << " \ n " ; 12 } 13 return 0; 14 }
1 # include < iostream > 2 3 using namespace std ; 4 5 int main () 6 { 7 while (1) 8 { 9 int N ; 10 cin >> N ; 11 if ( N % 5 > 0) 12 { 13 cout << " -1\ n " ; 14 continue ; 15 } 16 cout << N /5 << " \ n " ; 17 } 18 return 0; 19 }
3 Factorials Gone Wrong (40 points)
3.1 Writing the factorial program (5 points)
3.2 Breaking the program (5 points)
3.3 Breaking the program II (5 points)
3.4 Rewriting Factorial (10 points)
1 # include < iostream > 2 3 using namespace std ; 4 5 long long accumulator = 1; 6 7 int main () 8 { 9 int number ; 10 cout << " Enter a number : " ; 11 cin >> number ; 12 if ( number < 0) 13 { 14 cout << " No negative numbers allowed !\ n " ; 15 return 1; 16 } 17 if ( number > 20) 18 { 19 cout << " Program will not produce correct result !\ n " ; 20 } 21 for (; number > 0; accumulator *= number - -) ; 22 cout << " The factorial of " << number << " is " << accumulator << " .\ n " ; 23 return 0; 24 }
3.5 Rewriting Factorial II (10 points)
using namespace std ;
int main () { int number ; cout << " Enter a number : " ; cin >> number ; switch ( number ) { case 0: case 1: cout << " 1\ n " ; break ; case 2: cout << " 2\ n " ; break ; case 3: cout << " 6\ n " ; break ; case 4: cout << " 24\ n " ; break ; case 5: cout << " 120\ n " ; break ; case 6: cout << " 720\ n " ; break ; case 7: cout << " 5040\ n "; break ; case 8: cout << " 40320\ n " ; break ; case 9: cout << " 362880\ n " ; break ; case 10: cout << " 3628800\ n " ; break ; default :