Wednesday, January 4, 2023

C Programming. Sum sequence :- 1-2+3-4+5-6+7.....n .

Sum sequence :- 1-2+3-4+5-6+7.....n 



#include<stdio.h>
int main(){
       int s=0;
       int i,n;
       printf("Enter a number : ");
       scanf("%d",&n);
       for (i=1; i<=n; i++){
              if(i%2==0){
                     s=s-i;
              }
              else{
                     s=s+i;
              }
       }
       printf("Sum = %d",s);
       return 0;
}

No comments:

Post a Comment

Selection Sort Using C programming.

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the un...