Freelancer

Summation of first n digits

Summation of first n digits - C Programming Example

A number n will be given as input. This C program example will tell us the summation of that first n digit numbers.

Proving that 1+2+3+...+n is n(n+1)/2

A visual proof that 1+2+3+...+n = n(n+1)/2


We can visualize the sum 1+2+3+...+n as a triangle of dots. Numbers which have such a pattern of dots are called Triangle (or triangular) numbers, written T(n), the sum of the integers from 1 to n :
C Programming Example
1+2+3+......+n = n(n+1)/2
For the proof, we will count the number of dots in T(n) but, instead of summing the numbers 1, 2, 3, etc up to n we will find the total using only one multiplication and one division!

To do this, we will fit two copies of a triangle of dots together, one red and an upside-down copy in green. 

E.g. T(4)=1+2+3+4

O
OO
OOO
OOOO
+
OOOO
OOO
OO
O
=
OOOOO
OOOOO
OOOOO
OOOOO
    Notice that
  • we get a rectangle which is has the same number of rows (4) but has one extra column (5)
  • so the rectangle is 4 by 5
  • it therefore contains 4x5=20 balls
  • but we took two copies of T(4) to get this
  • so we must have 20/2 = 10 balls in T(4), which we can easily check.
This visual proof applies to any size of triangle number.
Here it is again on T(5):
O
OO
OOO
OOOO
OOOOO
+
OOOOO
OOOO
OOO
OO
O
=
OOOOOO
OOOOOO
OOOOOO
OOOOOO
OOOOOO
So T(5) is half of a rectangle of dots 5 tall and 6 wide, i.e. half of 30 dots, so T(5)=15.

The same proof using algebra!


Here's how a mathematician might write out the above proof using algebra:

T(n)+T(n)=1 +2 +3 +... +(n-1) +n
+n +(n-1) +(n-2) +... +2 +1Two copies, one red and the other, reversed, in green
                 =(1 + n) +(2 + n-1) +(3 + n-2) +... +(n-1 + 2) +(n + 1)pair off the terms, a red with a green
                 =(n+1) +(n+1) +(n+1) +... +(n+1) +(n+1)All the n pair-sums are equal to (n+1)
2 T(n)=n (n+1)
T(n)=n (n+1) / 2

C Programming Code 1

#include<stdio.h>
#include<conio.h>
main()
{
    int sum,n,i;
    printf("Enter the amount of number you want to sum up: \n");
    scanf("%d",&n);
    sum=0;
    for(i=1;i<=n;i++)
        sum=sum+i;
    printf("The sum of first %d digit even number is %d",n,sum);
    getch();
}

Output of the code

C programming example
Summation of first n numbers

Explanation of the code

·      #include<stdio.h>
#include<conio.h>
Ø This are called header file. A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files The C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header<stdio.h>.In the other hand  <conio.h> is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C. This header declares several useful library functions for performing "console input and output" from a program.
·       main()
Ø In C, program execution starts from the main() function. The main function can in-turn call other functions. When main calls a function, it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of function is reached.
·       int sum,n,,i;;
Ø int is a variable or data type.  A variable declared to be of type int can be used for storing integernumbers (values which doesn’t contain decimal places). Here we declare “sum”, “n”, and “i” as int type. So this variables can take only integer values.
·       printf() function
Ø C uses printf() function to write from the input devices. This function has been declared in the header file called stdio.h . Any text written within the pair of quotes ("") is displayed as such by printf() function on the screen. 
·       scanf() function
Ø This function is used to get input from the user of the program. scanf(“%d”,&n”) is used here. So, this program will read in a integer value for “n” variable that the user enters on the keyboard (%d is for integer values. As there is “&n”, the number user enters will be the value of “n” variable).
·       For loop
Ø A for-loop (or simply for loop) is a control flow statement for specifying iteration, which allows code to be executed repeatedly. The initialization statement is executed only once. Then, the test expression is evaluated. If the test expression is false, for loop is terminated. But if the test expression is true, codes inside the body of for loop is executed and the update expression is updated. This process repeats until the test expression is false. The for loop is commonly used when the number of iterations is known.
In this for loop, the loop will start when i=1. If  i is less than or equals to the entered number n, the statement in the loop will be executed. After executing once, i will increase one. When i is more than n, the loop will end.
·       getch() function
Ø getch() is used to hold the console(output) window on the screen after the whole program run is completed till the user enters a key from keyboard. This function is present in the header file called conio.h.

C Programming Code 2

#include<stdio.h>
#include<conio.h>
main()
{
    int n,sum;
    printf("please input the amount of numbers you want to add up \n");
    scanf("%d",&n);
    sum=n*(n+1)/2;
    printf("the answer is %d",sum);
    getch();
}

Explanation of the code


#include<stdio.h>

#include<conio.h>
·         This are called header file. A header file is a file with extension .h which contains c function declarations and macro definitions to be shared between several source files. The c programming language provides many standard library functions for file input and output. These functions make up the bulk of the c standard library header <stdio.h>. On the other hand <conio.h>is a c header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C. This header declares several useful library functions for performing “console input and output” form a program.
Main()
·         In C programming, program execution starts from the main() function. The main function can in-turn call other functions. When main calls a function, it passes the execution control to the function. The function returns control to main when a return statement is executed or when end of function is reached.
int n,sum;
·         Int is a variable or data type. A variable declared to be of type int (integer) can be used for storing integer numbers. Here we declare “n” and “sum” variable in int type.
Printf() function
·         C uses printf() function to write from the input devices. This function has been declared in the header file called stdio.h .Any text written within the pair of quotes (“”) is displayed as such by printf() function on the screen.
Scanf() function
·         This function is used to get input from the user of the program. Scanf(“%d”,&n) is used here. So, this program will read in an integer value of “n” valuable that the user enters on the keyboard (%d is for the integer values. As there is “&n”, the number user enters will be the value of “n” valuable.
Printf(“the sum  is  %d”,sum)
·         The integer “sum” is printed on the screen by this line. Here %d represents the integer value of “sum”.
Getch()
Getch() is added in order to stop the program. It is used in order to stop the program while the header file <conio.h> is used.


You may also like


No comments

Powered by Blogger.