Advertisement
PROFESSOR_AIH

Sum of Consecutive Odd Numbers III

Apr 10th, 2022
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.67 KB | None | 0 0
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int i,tcase,x,y,sum=0,m,count=0;
  5.     scanf("%d",&tcase);
  6.     for(m=1; m<=tcase; m++)
  7.     {
  8.         scanf("%d %d",&x,&y);
  9.         if(x%2!=0)
  10.         {
  11.             for(i=x;; i+=2)
  12.             {
  13.                 sum=sum+i;
  14.                 count++;
  15.                 if(count==y)
  16.                     break;
  17.             }
  18.         }
  19.         else if(x%2==0)
  20.         {
  21.             for(i=x+1;; i+=2)
  22.             {
  23.                 sum=sum+i;
  24.                 count++;
  25.                 if(count==y)
  26.                     break;
  27.             }
  28.         }
  29.         printf("%d\n",sum);
  30.         count=0;
  31.         sum=0;
  32.     }
  33.     return 0;
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement