C program giving double outcome | SoloLearn: Learn to code for FREE!

+1

C program giving double outcome

I have written a c program to print a number series every thing us fine but if i want 2 terms it is giving 4 terms if i want 5 it is giving 10 i hope you understand please try to solve this issue And also the number series is : 1 4 8 11 22 25 50 53 106 109 ..... https://code.sololearn.com/cAsLMfqTHd0Q/?ref=app

2/23/2021 1:31:22 PM

Bharani Kumar

19 Answers

New Answer

+1

Your finding series by 2 variables or numbers then it need to print even number of numbers.. What is the actual series, ..? Is it not able find it by single variable a.. ?

+1

Bharani Kumar else use i = i+2; instead of i++ in for loop..

+1

#include <stdio.h> int main() { int n,a,b,i; printf("enter how many terms"); scanf("%d",&n); printf("the series is: %d", a=1); for(i=0,b=a+3; i<n-1; ++i,b=b*2+3) { printf(" %d ",b); } printf("\n"); return 0; } //it is ok? Bharani Kumar

+1

Bharani Kumar #include <stdio.h> int main() { int n,a=1,b,i; printf("enter how many terms"); scanf("%d",&n); printf("the series is: \n"); for(i=0,b=a; i<n; ++i) { printf(" %d ",b); if(i%2==0) b=b+3; else b=b*2; } return 0; } //it is ok? I think fine now.. And also you can use only single variable a or b now

+1

You're welcome...

0

It is because each for iteration prints two values. If you want you can divide user input by two to avoid the confusion

0

bernborgess if i divide user input by 2 it does not work in all cases ex:3,5,7,9....

0

then you would have to rethink the way you print the elements. This way they will always come in pairs

0

i dont know this series. you could supress the second term of the print if n is odd and it is the last element for example

0

bernborgess the series is first digit is 1 add 3 to it then it becomes 4 them multiply with 2 and add 3 and mutiply with 2 and repeat the same

0

Jayakrishna🇮🇳 the series is also mentioned in question

0

Jayakrishna🇮🇳 i have also tried i=i+2 but it is giving one extra term for odd no.of terms

0

Bharani Kumar print first default value 1 then print b = b*2+3; in loop until i <n;

0

Jayakrishna🇮🇳 it does not work same as i said above it is giving one extra term for odd no.of terms

0

Jayakrishna🇮🇳 your program is giving wrong output once check the series in question

0

Jayakrishna🇮🇳 thanks for your help

0

age of ajay sohan and vijay is enterd through the keyboard write a program to find the eldest one among then.?

0

use if else statements

-1

bernborgess try to give me exact solution