Best Cell Phones

Summation of first n digit of even numbers

Summation of first n digit of even numbers (Using Formula) - C programming example

In this code a number n will be given as input. The C program example will tell us the summation of that first n digit of even numbers.

C program code 1

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

Output of the code

C Programming Example
Summation of first n digit of even numbers 

Explanation of the C program 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. 

C program code 2



#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();
}

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 integer numbers (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.


Final result

This program will show us the summation of the first n even numbers. What we need to is just input the number of even numbers we want to add up.

You may also like

No comments

Powered by Blogger.