Summation of first n odd numbers
Summation of first n odd numbers - C Programming Example
This code is an example for the beginners that sums the first n numbers using C programming language. We will use two different procedures.
- Using For Loop
- Using Mathematical Formula
C Programming Code 1
#include<stdio.h>
#include<conio.h>
main()
{
int sum, a,b,i;
printf("Input the amount of odd numbers you want to sum up \n");
scanf("%d",&a);
sum=0;
b=-1;
for(i=1;i<=a;i++)
{
b=b+2;
printf("\tOdd number %2d is %2d\n",i,b);
sum=sum+b;
}
printf("\n\t The sum is %d",sum);
getch();
}
Output of the code
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 sum,a,b,i;
· 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 “sum”, “a”,”b” and “i” 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 .Anu 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”,&a) is used here. So, this program will read in an integer value of “a” valuable that the user enters on the keyboard (%d is for the integer values. As there is “&a”, the number user enters will be the value of “a” valuable.
For loop
· When for is used in a program, at least 3 conditions are added in order to make a loop. It must be noted that the next part which is bound by the “for loop” must be separated by another pair of second bracket.
Printf(“the sum is %d”,sum)
· After completing the “for loop”, 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 Programming Code 2
#include<stdio.h>
#include<conio.h>
main()
{
int n,sum;
printf("please input the amount of odd numbers you want to add up \n");
scanf("%d",&n);
sum=n*n;
printf("the answer is %d",sum);
getch();
}
#include<conio.h>
main()
{
int n,sum;
printf("please input the amount of odd numbers you want to add up \n");
scanf("%d",&n);
sum=n*n;
printf("the answer is %d",sum);
getch();
}
Output of the code
| Summation of first n odd 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>. 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.
Final Result
This program will show us the summation of the first n odd numbers. What we need to is just input the number of odd numbers we want to add up.
No comments