Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

code example of sum of the first n odd numbers using for loop

def OddSummation(n):
    total = 0
    for i in range(1,n,2):
        total+=i
    return total
    
    
def main():
    number = int(input("Enter the number: "))
    print("The summation of all the odd numbers between 1 and ",number, " is ",         OddSummation(number))
    
main()
Comment

find sum of all odd numbers from 1 to n using for loop

/**
 * C program to print the sum of all odd numbers from 1 to n
 */

#include <stdio.h>

int main()
{
    int i, n, sum=0;

    /* Input range to find sum of odd numbers */
    printf("Enter upper limit: ");
    scanf("%d", &n);

    /* Find the sum of all odd number */
    for(i=1; i<=n; i+=2)
    {
        sum += i;
    }

    printf("Sum of odd numbers = %d", sum);

    return 0;
}
Comment

summing all Odd Numbers from 1 to N

# Python Program to Calculate Sum of Odd Numbers from 1 to N
 
maximum = int(input(" Please Enter the Maximum Value : "))
Oddtotal = 0

for number in range(1, maximum+1, 2):
    print("{0}".format(number))
    Oddtotal = Oddtotal + number

print("The Sum of Odd Numbers from 1 to {0} = {1}".format(number, Oddtotal))
Comment

PREVIOUS NEXT
Code Example
Python :: python test module 
Python :: python how to use rnage 
Python :: count occurrences of one variable grouped by another python 
Python :: python first 
Python :: create tuples in pandas 
Python :: matplotlib remove drawn text 
Python :: xlrd documentation 
Python :: dynamically create python dictionary 
Python :: how to unimport a file python 
Python :: install python package 
Python :: tkinter radio button default selection 
Python :: python dictionary add item 
Python :: python capitalize 
Python :: unicode error python 
Python :: python pip 
Python :: Python operator to use for set union 
Python :: python all but the last element 
Python :: How to add all the numbers of a list using python? 
Python :: how to iterate row wise using 2d integer array in python 
Python :: {"message": "401: Unauthorized", "code": 0} discord 
Python :: tensorflow Dense layer activatity leaklyrelu 
Python :: continue statement in python 
Python :: logger 
Python :: Label enconding code with sklearn 
Python :: import sentence transformers 
Python :: upgrade python version windows 
Python :: python list equality 
Python :: tensorflow data augmentation 
Python :: phyton "2.7" print 
Python :: how to store categorical variables in separate dataframe 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =