Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sum of consecutive integers A) Write a program that prompts for an integer—let’s

ID: 3794110 • Letter: S

Question

Sum of consecutive integers

A) Write a program that prompts for an integer—let’s call it X—and then finds the sum of X consecutive integers starting at 1. That is, if X = 5, you will find the sum of1+2+3+4+5=15

B) Modifyyourprogrambyenclosingyourloopinanotherloopsothatyoucanfind consecutive sums. For example, if 5 is entered, you will find five sums of consecutive numbers:
1=1

1+2=3
1+2+3 = 6 1+2+3+4 = 10 1+2+3+4+5 = 15

Print only each sum, not the arithmetic expression.

C) Modify your program again to only print sums if the sum is divisible by the number of operands. For example, with the sum 1 + 2 + 3 + 4 + 5 = 15, there are five operands and the sum, 15, is divisible by 5, so that sum will be printed. (Do you notice a pattern?)

1=1
1+2+3=6

1+2+3+4+5=15

Explanation / Answer

-->A
int main()
{
   int i,n,sum=0;
   printf("enter a number :");
   scanf("%d",&n);
   for(i=1;i<=n;i++)
   {
       sum=sum+i;
   }
   printf("sum is :%d",sum);
   return 0;
}

-->B
int main()
{
   int i,n,sum=0;
   printf("enter a number :");
   scanf("%d",&n);
   for(i=1;i<=n;i++)
   {
       sum=sum+i;
       printf("sum is :%d ",sum);
   }
   return 0;
}

-->C
int main()
{
   int i,n,sum=0,j=0;
   printf("enter a number :");
   scanf("%d",&n);
   for(i=1;i<=n;i++)
   {
       j++;
       sum=sum+i;
       if( sum%j == 0)
       printf("sum :%d ",sum);
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote