C程序计算1-2 + 3-4 + 5-6 + 7-8 ... N个项的和

news/2024/7/20 2:13:42 标签: 算法, 小程序, c语言, python, c++

The series is: 1-2+3-4+5-6+7-8...N terms, we have to find out the sum up to Nth terms.

该序列是: 1-2 + 3-4 + 5-6 + 7-8 ... N个项 ,我们必须找出第N个项之和。

Solution:

解:

Let's analyse this problem,

让我们分析这个问题,

    If we want Sum of this series up to 2nd term then sum will be:
        1-2 =-1
    Up to 3rd term:
        1-2+3 =2
    Up to 4th term 
        1-2+3-4 = -2
    Up to 5th term:
        1-2+3-4+5= 3
        .
        .
        .

Now we can conclude that if we want sum up to an Odd term then we always get sum as ((N+1)/2) and if we want sum up to an Even term the sum is in the form of (-1*(N/2)).

现在我们可以得出结论,如果要对一个奇数项求和,则总和为((N + 1)/ 2),如果要对一个偶数项求和,则总和为(-1 * (N / 2))。

Now Let's make logic for this using c programming,

现在,让我们使用C编程为此逻辑,

#include <stdio.h>

//function for creating the sum of the 
//series up to Nth term
int series_sum(int n)
{
    if (n % 2 == 0)
        return (-(n / 2));
    else
        return ((n + 1) / 2);
}

// main code
int main()
{
    int n;
    printf("Series:1-2+3-4+5-6+7-8.....N\n");
    printf("Want some up to N terms?\nEnter the N term:");
    scanf("%d", &n);
    
    printf("Sum is:%d", series_sum(n));
    
    return 0;
}

Output

输出量

Series:1-2+3-4+5-6+7-8.....N
Want some up to N terms?
Enter the N term:10
Sum is:-5

Read more...

...

  • C programs by categories

    C程序分类

  • Sum of the series programs C

    系列程序的总和C

  • Star/Asterisks Pattern programs in C

    C中的星号/星号模式程序

翻译自: https://www.includehelp.com/c-programs/calculate-the-sum-of-the-series-1-2-3-4-5-6-7-8-n-terms.aspx


http://www.niftyadmin.cn/n/1256350.html

相关文章

android 动画 同步,如何为Android同步状态图标设置动画?

我找到了答案……这显示了如何设置和取消stat_notify_sync图标.private void showNotification(String authority) {Object service getSystemService(NOTIFICATION_SERVICE);NotificationManager notificationManager (NotificationManager) service;int icon android.R.dr…

c语言编程1+2+3_C ++编程概述和历史适用性问题与解答

c语言编程123In this section, there are Multi Choice Questions and Answers related to C programming overview and history. 在本节中&#xff0c;有与C 编程概述和历史记录相关的多项选择问题和解答。 C 历史记录列表&#xff0c;概述相关的问答 (List of C History, Ov…

android 备份ubuntu,ubuntu16.04备份后关于android studio

2016&#xff0c;年&#xff0c;4,21号&#xff0c;ubuntu16.04LST版本终于发布了&#xff0c;小白的我体验了一把&#xff0c;可惜的是&#xff0c;在升级过程中&#xff0c;电脑卡死了&#xff0c;结果&#xff0c;重启后&#xff0c;不能进去系统了&#xff0c;无赖之下重装…

randomisation()函数生成随机向量| 使用Python的线性代数

Prerequisite: numpy.random.random( ) function with no input parameter 先决条件&#xff1a; numpy.random.random()函数&#xff0c;无输入参数 Numpy is the library of function that helps to construct or manipulate matrices and vectors. The function numpy.rand…

no android sdk found,No Android SDK found - Android Studio

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效&#xff0c;请关闭广告屏蔽插件后再试):问题:I have a problem with Android 0.4.2 Studio, when creating a new application, it tells me there is a problem with the rendering and shows me the follo…

java 获取list索引_从Java中的List的给定索引获取元素/对象

java 获取list索引Given a list of integers and we have to access/retrieve element/object from given index from the list in Java. 给定一个整数列表&#xff0c;我们必须从Java列表中的给定索引访问/检索元素/对象。 To get the element from given index - we use Lis…

cibc app for android,‎App Store 上的“CIBC Mobile Banking”

CIBC MOBILE IS ON TOPCIBC received the overall highest customer satisfaction ranking from J.D. Power for our mobile banking app in a study that ran during March 2020. We’re always striving to make our app to do more for you.HOW SIMPLE?SIGN IN, YOUR WAY S…

java数组分离奇数和偶数_以最小的时间复杂度分离偶数和奇数

java数组分离奇数和偶数Problem statement: 问题陈述&#xff1a; Given an array of integers. Write a function to segregate the Even and Odd numbers. Put the even numbers first. 给定一个整数数组。 编写一个函数来分隔偶数和奇数 。 将偶数放在第一位。 Solution: …