C program to find nth odd number

C program to find nth odd number. Take a number as input.

Sol.

Input  =  5
Output = 9 //nth odd positive number = 2*n-1
First three odd numbers are 1, 3, 5, 7, 9.

/* C program to find nth odd number */

#include<stdio.h>
int main()
{
    int nOddNumber,oddNum,loop, nthOddNum=0;
    printf("Enter a number: \n");
    scanf("%d",&nOddNumber);
    printf("%dth odd numbers is:\n",nOddNumber);
    for(loop=1,oddNum=1;loop<=nOddNumber;loop=loop+1,oddNum=oddNum+2)
    {
        nthOddNum = oddNum;
    }
    printf("%d",nthOddNum);
    return 0;
}

Output :



Share:

Post a Comment

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.