In: Computer Science
Using C Language
Write a program segment that computes 1 + 2 + 3 + ... + ( n - 1) +
n , where n is a
data value. Follow the loop body with an if statement that compares
this value to
(n * (n + 1)) / 2 and displays a message that indicates whether the
values are the
same or different.
Please give me code to just copy and paste
#include <stdio.h>
int main() {
//Declaring variables
int num,sum_of_nos=0,result,i;
//getting the number entered by the user
printf("Enter the number :");
scanf("%d",&num);
/* This for loop will calculate
* the sum of numbers from 1 to
* the user entered number
*/
for(i=1;i<=num;i++)
{
//calculating the sum of numbers manually
sum_of_nos+=i;
}
//calculaitng the sum of numbers using formula
result=(num*(num+1))/2;
/* Comparing the two results.
* Based on that display the result
*/
if(sum_of_nos==result)
{
printf("** The Values are same
**");
}
else
{
printf("** The Values are not same
**");
}
return 0;
}
__________________
output:
____________Thank YOu