5 - COMP1511 - Programming Fundamentals - 18s1.pdf - COMP1511 Programming Fundamentals 18s1 COMP1511 18s1(webcms Code Examples from Lectures on while

5 - COMP1511 - Programming Fundamentals - 18s1.pdf -...

This preview shows page 1 - 6 out of 14 pages.

02/12/2018 COMP1511 - Programming Fundamentals - 18s1 1/14 thirteen_a.c A silly program which prints first 1000 multiples of 13 #include < stdio . h > int main ( void ) { printf ( "%d\n" , 13 ); printf ( "%d\n" , 26 ); printf ( "%d\n" , 39 ); printf ( "%d\n" , 52 ); printf ( "%d\n" , 65 ); printf ( "%d\n" , 78 ); printf ( "%d\n" , 91 ); printf ( "%d\n" , 104 ); printf ( "%d\n" , 117 ); printf ( "%d\n" , 130 ); printf ( "%d\n" , 143 ); printf ( "%d\n" , 156 ); printf ( "%d\n" , 169 ); printf ( "%d\n" , 182 ); printf ( "%d\n" , 195 ); printf ( "%d\n" , 208 ); printf ( "%d\n" , 221 ); printf ( "%d\n" , 234 ); printf ( "%d\n" , 247 ); printf ( "%d\n" , 260 ); printf ( "%d\n" , 273 ); printf ( "%d\n" , 286 ); printf ( "%d\n" , 299 ); printf ( "%d\n" , 312 ); printf ( "%d\n" , 325 ); printf ( "%d\n" , 338 ); printf ( "%d\n" , 351 ); printf ( "%d\n" , 364 ); printf ( "%d\n" , 377 ); ... thirteen_b.c A silly program which prints first 1000 multiples of 13 COMP1511 18s1 (webcms) Code Examples from Lectures on while COMP1511 18s1 (flask)
Image of page 1

Subscribe to view the full document.

02/12/2018 COMP1511 - Programming Fundamentals - 18s1 2/14 #include < stdio . h > int main ( void ) { if ( 1 % 13 == 0 ) { printf ( "%d\n" , 1 ); } if ( 2 % 13 == 0 ) { printf ( "%d\n" , 2 ); } if ( 3 % 13 == 0 ) { printf ( "%d\n" , 3 ); } if ( 4 % 13 == 0 ) { printf ( "%d\n" , 4 ); } if ( 5 % 13 == 0 ) { printf ( "%d\n" , 5 ); } if ( 6 % 13 == 0 ) { printf ( "%d\n" , 6 ); } if ( 7 % 13 == 0 ) { printf ( "%d\n" , 7 ); } if ( 8 % 13 == 0 ) { printf ( "%d\n" , 8 ); } if ( 9 % 13 == 0 ) { printf ( "%d\n" , 9 ); } if ( 10 % 13 == 0 ) { printf ( "%d\n" , 10 ); ... thirteen_c.c A silly program which prints first 1000 multiples of 13 Note the same 4 lines are repeated 13000 times. This means these lines could be replaced by a while loop which executes 13000 times.
Image of page 2
02/12/2018 COMP1511 - Programming Fundamentals - 18s1 3/14 #include < stdio . h > int main ( void ) { int i ; i = 1 ; if ( i % 13 == 0 ) { printf ( "%d\n" , i ); } i = i + 1 ; if ( i % 13 == 0 ) { printf ( "%d\n" , i ); } i = i + 1 ; if ( i % 13 == 0 ) { printf ( "%d\n" , i ); } i = i + 1 ; if ( i % 13 == 0 ) { printf ( "%d\n" , i ); } i = i + 1 ; if ( i % 13 == 0 ) { printf ( "%d\n" , i ); } i = i + 1 ; if ( i % 13 == 0 ) { printf ( "%d\n" , i ); } i = i + 1 ; if ( i % 13 == 0 ) { printf ( "%d\n" , i ); } ... thirteen_d.c A simple program which uses a while loop to print first 1000 multiples of 13 #include < stdio . h > int main ( void ) { int i ; i = 1 ; while ( i <= 13000 ) { if ( i % 13 == 0 ) { printf ( "%d\n" , i ); } i = i + 1 ; } return 0 ; } thirteen_e.c Another simple program which uses a while loop to print first 1000 multiples of 13
Image of page 3

Subscribe to view the full document.

02/12/2018 COMP1511 - Programming Fundamentals - 18s1 4/14 #include < stdio . h > int main ( void ) { int i ; i = 13 ; while ( i <= 13000 ) { printf ( "%d\n" , i ); i = i + 13 ; } return 0 ; } thirteen_f.c Another simple program which uses a while loop to print first 1000 multiples of 13 #include < stdio . h > int main ( void ) { int i ; i = 1 ; while ( i <= 1000 ) { printf ( "%d\n" , i * 13 ); i = i + 1 ; } return 0 ; } gauss.c A simple program demonstrating the use of a while loop #include < stdio . h > int main ( void ) { int n , upperBound , sum ; sum = 0 ; upperBound = 10 ; n = 1 ; while ( n <= upperBound ) { sum = sum + n ; n = n + 1 ; } printf ( "Sum of integers 1..%d is %d\n" , upperBound , sum ); return 0 ; } sum_42_numbers.c Read 43 numbers and then print the sum of the numbers A simple program demonstrating while & scanf Note for simplicity we are assuming scanf succeeds in reading an integer. A robust program would check that scanf returns 1 to indicate an integer read.
Image of page 4
02/12/2018 COMP1511 - Programming Fundamentals - 18s1 5/14 #define N_NUMBERS 42 #include < stdio . h > int main ( void ) { int x , sum , n ; sum = 0 ; printf ( "Enter %d numbers:\n" , N_NUMBERS ); n = 0 ; while ( n < N_NUMBERS ) { scanf ( "%d" , & x ); sum = sum + x ; n = n + 1 ; } printf ( "Sum of the numbers is %d\n" , sum ); return 0 ; } sum_n_numbers.c
Image of page 5

Subscribe to view the full document.

Image of page 6
  • Spring '18
  • Ahmed Zeki

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes