dmilicev

alternate_number_series_v1.c

Sep 18th, 2020 (edited)
125
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     alternate_number_series_v1.c
  3.  
  4.     Write a c Program that will print the number series vertically on screen:
  5.  
  6.     10 1 9 2 8 3 7 4 6 5
  7.  
  8.     There, two series of numbers appear alternately
  9.  
  10.     first 10 9 8 7 6
  11.  
  12.     and second 1 2 3 4 5.
  13.  
  14. There is no difference between loops in C,
  15. except that the do-while loop must be performed at least once.
  16.  
  17.  
  18. for loop
  19. https://www.tutorialspoint.com/cprogramming/c_for_loop.htm
  20.  
  21. for ( init; condition; increment ) {
  22.    statement(s);
  23. }
  24.  
  25.  
  26. while loop
  27. https://www.tutorialspoint.com/cprogramming/c_while_loop.htm
  28.  
  29. init;
  30. while(condition) {
  31.    statement(s);
  32.    increment;
  33. }
  34.  
  35.  
  36. do-while loop
  37. https://www.tutorialspoint.com/cprogramming/c_do_while_loop.htm
  38.  
  39. init;
  40. do {
  41.    statement(s);
  42.    increment;
  43. } while( condition );
  44.  
  45.  
  46.     You can find all my C programs at Dragan Milicev's pastebin:
  47.  
  48.     https://pastebin.com/u/dmilicev
  49.  
  50. */
  51.  
  52. #include <stdio.h>
  53.  
  54. int main(void)
  55. {
  56.     int i, j, number_of_pairs=5;
  57.  
  58.     printf("\n Using for loop, the first way \n\n");
  59.     for( i=0; i<number_of_pairs; i++ )
  60.         printf(" %2d \n %2d \n", 10-i, i+1 );
  61.  
  62.     printf("\n Using for loop, the second way \n\n");
  63.     for( i=10,j=1; j<=number_of_pairs; i--,j++ )
  64.         printf(" %2d \n %2d \n", i, j );
  65.  
  66.     printf("\n Using while loop \n\n");
  67.     i=10;                               // initial values
  68.     j=1;
  69.     while(j<=number_of_pairs)           // condition
  70.     {
  71.         printf(" %2d \n %2d \n", i, j );
  72.         i--;                            // decrement
  73.         j++;                            // increment
  74.     }
  75.  
  76.     printf("\n Using do-while loop \n\n");
  77.     i=10;                               // initial values
  78.     j=1;
  79.     do
  80.     {
  81.         printf(" %2d \n %2d \n", i, j );
  82.         i--;                            // decrement
  83.         j++;                            // increment
  84.     } while(j<=number_of_pairs);        // condition
  85.  
  86.  
  87.     return 0;
  88. }
  89.  
RAW Paste Data Copied