Advertisement
luizaspan

Soma números pares e ímpares

May 12th, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. // calcular a soma dos numeros pares e dos numeros ímpares contidos em [1,n]
  2.  
  3. #include <stdio.h>
  4.  
  5. int main (void)
  6. {
  7.   int n,i,x,k,y;
  8.   printf("n= ");
  9.   scanf("%d",&n);
  10.  
  11.   i=1;
  12.   k=0;
  13.   y=0;
  14.   while (i<=n)
  15.     {
  16.       x=(i/2)*2;
  17.       if (x==i)
  18.     {
  19.       k++;
  20.     }
  21.       else if (x!=i)
  22.     {
  23.       y++;
  24.     }
  25.       i++;
  26.     }
  27.  
  28.   printf("\nHá %d números pares e %d números ímpares no intervalo [1,%d]",k,y,n);
  29.   printf("\nA soma destes é igual a %d.\n",k+y);
  30.  
  31.   return 0;
  32. }
  33.  
  34.  
  35. // FUNCIOOOONAAAAAAA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement