Problem Analysis
Odd numbers are those that can not be distributed in two parts equally. Odd numbers are integers that can not be paired into groups of two. Examples of odd numbers are 1, 3, 5, 7, etc.
Odd numbers are not multiples of two. Odd numbers possess a unique property that one’s place of odd numbers will always be 1, 3, 5, 7, or 9.
Problem Description
Objective is to develop a C program to print 1 3 5 7 n. The series follows a property that the difference between two odd numbers is 2., this 1 3 5 7 has a difference of two. Since we have to display the series, the task of printing the odd number has to be repeated again and again, hence it has to be in a loop. To achieve the desired objective we have to put the program logic in loop. The number upto which odd number series is to be printed is given by the user.
Solution to Problem
Following is the program to print odd number series:
#include <stdio.h> int main() { int i, n; printf("\n Input number upto which you want to print odd number series "); scanf("%d", &n); printf("\n Odd number series upto %d is: ",n); for(i=0; i<n; i++) { i = i + 1; printf("%d ", i); } return 0; }
Output: Input number upto which you want to print odd number series 20 Odd number series upto 20 is: 1 3 5 7 9 11 13 15 17 19
Code Analysis In the code limit upto which odd number series has to be displayed is taken from the user. Input is taken in user defined integer type variable n. Code for this is: printf("\n Input number upto which you want to print odd number series "); scanf("%d", &n); Value of variable n is used to set the terminating condition of the for loop. Inside the for loop loop variable i is incremented by 1 as we have to print odd number series. The code to dp this: for(i=0; i<n; i++) { i = i + 1; printf("%d ", i); }
Conclusion
The objective is to display Odd number series 1 3 5 7 …n. The desired objective is achieved by developing a program in C language.
Leave a Reply