Advertisement
ben1939

series

Nov 17th, 2015
2,820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.  
  6. float a1, d, sum;
  7. int num, code , i;
  8.  
  9. printf("Enter rhe first element (real), the difference (real) and the number of elements:");
  10.  
  11. do {
  12.        scanf("%f%f%d", &a1,&d, &num);
  13.     if (scanf("%f%f%d", &a1,&d, &num)==1)
  14.       break;
  15.     else
  16.          printf("iilegal input, try agian");
  17.            
  18.    } while(1);
  19.  
  20.  
  21. do  {
  22.  printf("Enter a code for desired operation from the menu :\n   1-display an aritmetic progression\n   2-display an aritmetic series\n    3 -display a geometric progression\n   4-display a  geometric series\n    5 - exit the program\n");      
  23.  
  24.   scanf("%d", &code);
  25.    if (code==1) {
  26.     for(i=0; i<num; i++)
  27.       printf("%f" ,a1+i*d);
  28.            }
  29.  
  30.         else  if (code==2) {
  31.               sum=0;
  32.                for(i=0; i<num; i++)
  33.                sum+= a1+i*d;
  34.                printf("%f", sum);  
  35.                       }
  36.  
  37.                   else  if (code==3) {
  38.                    printf("%f" ,a1);
  39.                      for (i=1; i<num; i++)
  40.                       printf("%f" ,a1*=d);
  41.                      }
  42.  
  43.                              else if (code==4) {
  44.                              sum=a1;
  45.                                for (i=1; i<num; i++) {
  46.                                 a1*=d;
  47.                                 sum+= a1;  }
  48.                                 printf("%f", sum);
  49.                             }
  50.  
  51.                                        else if (code==5){
  52.                                         printf("bye");
  53.                                         break; }
  54.  
  55.                                            else
  56.                                             printf("iilegal input, try agian");  
  57.                                          
  58.  
  59.   } while (1);
  60.  
  61.  
  62. retuern 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement