Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to write a program in C language to realize odd sum?
How to write a program in C language to realize odd sum?
1, topic analysis First of all, we know that the odd numbers within 100 are 1, 3,7 ... 97,99, that is, from 1 increase by 2.

This problem requires an odd sum, that is, sum = 1+3+7+...+97+99.

Start the variable I from 1 and assign each odd number in turn until i< does not meet the condition (I

Accumulate each I value in turn, and the sum obtained is the odd sum obtained by the topic.

2. The block diagram of the design program is as follows:

3. Write program # include according to the following program block diagram.

int? main()?

{?

int? I,sum = 0; ? //Define two integer variables.

{?

for(I = 1; I<= 100; i=i+2)? //The for loop of variable I starts at 1 and ends at 99.

sum = sum+I; ? //variable I accumulation

}?

printf(" 1+3+ - +99=%d\n ",sum); ? //Output odd sum result

Return? 0; ?

}?

Extended data:

1, understand C language.

C language is a general computer programming language, which is widely used in the bottom development. The design goal of C language is to provide a programming language that can compile and process low-level memory in a simple way, generate a small amount of machine code and run without any support of running environment.

Although C language provides many low-level processing functions, it still maintains good cross-platform characteristics. C language programs written in standard specifications can be compiled on many computer platforms, even including some embedded processors (single chip microcomputer or MCU) and supercomputers.

C language is a process-oriented computer programming language, which is different from object-oriented programming languages such as C++ and Java.

Its compilers mainly include Clang, GCC, WIN-TC, SUBLIME, MSVC, Turbo C and so on.

2. Definition of odd numbers

Among integers, numbers that are not divisible by 2 are called odd numbers. In daily life, people usually call odd numbers singular, as opposed to even numbers. Odd numbers can be divided into positive and negative odd numbers. The mathematical expression of odd numbers is:?

Positive odd numbers: 1, 3, 5, 7, 9, 1 1, 13, ......

Negative odd numbers:-1, -3, -5, -7, -9,-1 1,-13. ......

3. Other programs for odd sum in C language.

Arithmetic series method:

void main(){

int sum =( 1+99)/2 * 50;

printf("%d ",sum);

}?

References:

C language _ Baidu Encyclopedia

Weird _ Baidu Encyclopedia