How to output this numbers sequence? - c

Examples:
enter a number:5
output : 5,10,15,20,25,20,15,10,5
**Example 2:**
enter a number :3 output : 3,6,9,6,3
Code:
#include<stdio.h>
void main(){
int a,i ;
a=5;
i=1;
do{
printf("%d\t",a*i);
i++;
}
while(i<=5);
int b,l;
b=3;
l=1;
do{
printf("\n%d\t",b*l);
l++;
}
while (l<=3);
}
Please help me with my codes so that I can improve myself and the showing the correct result that is supposed to first increase an then to decrease the given number.

To print those kinds of sequences you can also use recursion. Your function would look something like this:
void printSequence(int x, int incr)
{
if (x == incr * incr)
{
printf("%d ", x);
return;
}
printf("%d ", x);
printSequence(x + incr, incr);
printf("%d ", x);
}
So you send starting number and increment to the function. In your case they would be the same number, because you want to start, for example, with 5 and increment it by 5 until you hit 5*5. Your base case is when your number x becomes eaqual to x*x which is in code represented as x == incr * incr. If your base case is true, you just print out current value of x and return from the function. And if your base case is not true you want to print out the value of x and call the same function again with incremented x. Then print out the value of x again when you recursively start going back from the last function call to the original one.
If your main looks like this:
int main(void)
{
printSequence(5, 5);
return 0;
}
Output will be:
5 10 15 20 25 20 15 10 5

Related

In C, computing an equation using user input values is not giving the expected result?

I am doing an assignment for my class but I am stuck. The assignment is to:
Write a recursive program to precompute Fibonacci numbers and store them in an array. Fibonacci formula is Fib(0) = 1, Fib(1) = 1 and Fib(i) = Fib(i − 1) + Fib(i − 2). Store the ith Fibonacci number at index i. Have a loop to read i and print i and ith Fibonacci number. Use −1 to quit the loop. My output is wrong but I don't know how to fix it. I have been trying for a while now but I just couldn't pinpoint my mistake.
My code is
#include <stdio.h>
double Fib[50]; //globally declared
int fib(int i)
{
for(i=0; i<50; i++) //loop to scan
{
scanf("%lf", &Fib[i]); //scan and store numbers in an array
if (Fib[i]==-1) //i =-1 will end loop
break;
}
Fib[i]= Fib[i-1]+Fib[i-2];//formula
if(Fib[i]==0||Fib[i]==1) //i=0 and i=1 will print 1
Fib[i]=1;
else if(i>1) //performs the operation with the formula
printf("%d %lf\n", i, Fib[i]);
}
int main()
{
int i=0;
fib(i);
return 0;
}
Expected result:
user input: 4 10 20 15 5 -1
output:
4 5.000000
10 89.000000
20 10946.000000
15 987.000000
5 8.000000
My output:
5 20.000000
A couple points:
Your program isn't recursive
Compute all of Fib first with your recursive function then after
handle user input in a loop
The code below has the structure for dealing with user input, do the recursion:
#include <stdio.h>
// It would make sense for this to store unsigned long long instead of double
// because Fibonacci numbers are always positive integers
unsigned long long Fib[50];
// Your assignment specifically said use a recursive program to compute Fib.
// This is not a recursive function, but it is correct, I will leave the
// recursion for you to work out
void populateFib() {
Fib[0] = 1;
Fib[1] = 1;
unsigned i;
for (i = 2; i < 50; ++i)
Fib[i] = Fib[i - 1] + Fib[i - 2];
}
int main() {
// First compute Fib
populateFib();
// Deal with user input in an infinite loop
for (;;) {
int input;
scanf("%d", &input);
// Condition for breaking the infinite loop
if (input == -1)
break;
// Sanity check the user won't read out of bounds
if (input < 0 || input >= 50) {
printf("No!\n");
continue;
}
// Display what the user wants
printf("%d %llu\n", input, Fib[input]);
}
return 0;
}

Can you reverse the total? (Looping C)

"can you reverse the number" is the number already reverse by using Mod(%). My question, can it be reversed to normal?
For example, if you enter the number "2552" it'll change to "2+5+5+2", which is correct, but, when you enter another number like "4125" it will change to "5+2+1+4" instead of "4+1+2+5"
Ok, I just entered the programming world, a newcomer
With the "if" can it add "+" without exceeding the number like "4+1+2+5+"
there are "+" after "5", how can I delete this extra "+"?
#include <stdio.h>
main(){
int a, b, h=0;
printf("Enter the number : ");
scanf("%d",&a);
printf("%d = ", a);
while(a != 0)
{
b=a%10;
a=a/10;
printf("%d",b);
if(b != a)
{
printf("+");
}
h=h+b;
}
printf(" = %d\n", h);
}
Instead of scanning for an actual number, you can scan a string from the user. Strings are easy to reverse:
char num[5]; // Has the input
char rev[5]; // Will have the reverse
int len = strlen(num);
rev[len] = 0; // End the string
for(int i = 0; i < len; i++){
rev[i] = num[len-i-1];
}
You can go backwards by taking the highest digits in your number first instead of the lowest, e.g. like so:
int currentLog10;
int powerOfTenForCurrentDigit;
...
while(a != 0)
{
currentLog10 = (int)log10(a);
powerOfTenForCurrentDigit = (int)pow(10, currentLog10);
b = a/powerOfTenForCurrentDigit;
a = a - b * powerOfTenForCurrentDigit;
...
}
How does it work:
log10 of 4125 will give us 3.
b is set to 4125 devided by 1000 (10^3) = 4.
a is set to 4125 - 4 * 1000 = 125
Next step log10 of 125 gives 2, we devide by 10^2 (100) and so on.
Last step 5 gives 0, 5 devided by 10^0 (1) gives 5, a becomes 5 - 5 * 1, which is 0, we are done.
You have to be careful in edge cases, where log10 returns something like (n-1).999999999 instead of n, but it shouldn't happen for small numbers as are entered in your program. Maybe add some sanity check for the input.

Modified Fibonacci in C

I need to generate a modified Fibonacci series and it must be completely dynamic. Here f0 and f1 will be given, i.e f0=1 and f1=3 after generating the series. I should print the resulting value at a particular index.
Ex: f0 = 1, f1 = 3, testcase(n) = 3 (This can change not a particular value)
t1 = 4 t2 = 8 t3 = 11 and so on. Series should be generated for 11 elements by adding current element and previous element using: f[i] = f[i-1] + f[i-2]
It can be represented as:
0=>1
1=>3
2=>4
3=>7
4=>11
5=>18
6=>29
7=>47
8=>76
9=>123
10=>199
11=>322
I should print the values at indices 4,8 and 11 (which must be the output of my program), i.e. 11 76 322.
Input Format:
f0,f1 and n (where n is the no of indices)
where ti=[t1,t2,....tn-1] (which specifies the index for R-Fibonacci series).
Output Format:
Print the values from the R-fibonacci series based on the given indices.
Sample Input:
1 3 3 4 8 11
Sample Output:
11 76 322
I have the code that generates the Fibonacci series for the above program but I want to display the value at 4,8,11 indices. Here is the code:
int fib(int n)
{
int f[n+1];
int i;
f[0]=1;
f[1]=3;
for(i=2;i<=n;i++)
{
f[i]=f[i-1]+f[i-2];
}
return f[n];
}
int main()
{
int n=11
printf("%d ",fib(n));
getchar();
return 0;
}
Like this? The array is defined in main and passed to the function as an argument. The function fills in the array, returns nothing, and then in main you can print the elements you want.
You will need a loop to do that, with another dynamic array holding the indices you are asked to print.
#include <stdio.h>
void fib(int n, int *f)
{
int i;
f[0] = 1;
f[1] = 3;
for(i = 2; i <= n; i++)
{
f[i] = f[i-1] + f[i-2];
}
}
int main()
{
int n = 11;
int f[n+1];
fib(n, f);
printf("%d ", f[8]);
printf("%d ", f[11]);
printf("\n");
getchar();
return 0;
}
Program output:
76 322
I will leave you some code to write, but suppose you make a dynamic array of the index values required, such as
int index[m];
index[0] = 4;
index[1] = 8;
index[2] = 11;
you can print the series term with such as
printf("%d ", f[ index[i] ]);
If I understand correctly, this question really has little to do with fibonacci and is about scope in C. You are declaring and defining an array in a function, fib, and filling it within that function (and returning a single element value). What you want is to have access to the entire array from the caller.
A straightforward way of doing this is to declare the array in the calling method, and pass a pointer to it to the fib function:
#include <stdio.h>
int fib(int f[], int n)
{
int i;
f[0]=1;
f[1]=3;
for(i=2;i<=n;i++)
{
f[i]=f[i-1]+f[i-2];
}
return f[n];
}
int main()
{
int n=11;
int f[12];
fib(f, n);
printf("%d ", f[4]);
printf("%d ", f[8]);
printf("%d ", f[11]);
getchar();
return 0;
}
Here is a code to generate a set of fibonacci numbers less than a given number N using recursive algorithm :
#include<stdio.h>
int fibo(int n)
{
if(n<2)
return n;
else
return (fibo(n-1)+fibo(n-2));
}
void main()
{
int n,i;
printf("\n Enter number : ");
scanf("%d",&n);
printf("\n Fibonacci series is : ");
for(i=0;i<n;i++)
printf("\n %d",fibo(i));
}

How to fix program printing a 0 after the print statement executes in C?

This program tests Goldbach's Conjecture, printing a given even integer as the sum of two primes. After printing the first one, the given integer is to iterate by 2 , then find the sum of two primes for that integer. And so on until the program is interrupted by the user.
This problem is that the program prints a '0' after every executed print statement.
The code:
#include <stdio.h>
#include <math.h>
int GC(int goldsum); //function prototype
int main()
{
int goldsum;
printf("Enter an even integer greater than 5: ");
scanf("%d", &goldsum);
printf("%d\n", GC(goldsum));
goldsum = goldsum + 2;
printf("%d\n", GC(goldsum));
}
int GC(int goldsum) //function definition
{
int i, j; //prime addends
int div1, div2; //divisors
char prime1, prime2;
for (i=2 ;i<goldsum ;i++) //when number is less than goldsum, run this loop iterating by 1
{
prime1 = 1;
for (div1=2 ;div1<i ;div1++) //this loop determines if "i" is prime.
if (i % div1 == 0) //if yes, the prime number is stored in "i"
prime1 = 0;
if (prime1)
{
for (j=3; j<goldsum; j+=2) //when number is less than goldsum, run this loop iterating by 2
{
prime1 = 1;
for (div2=2; div2<j; div2++) //this loop determines if "j" is prime.
if(j % div2 == 0) //if yes, the prime number is stored in "j"
prime1 = 0;
if (prime1)
if (i + j == goldsum) //If i + j = goldsum, it prints the result.
{
printf("%d + %d = %d\n",i ,j , goldsum);
return 0;
}
}
}
}
return 0;
}
The output:
Enter an even integer greater than 5: 10
3 + 7 = 10
0
5 + 7 = 12
0
What I want it to look like:
Enter an even integer greater than 5: 10
3 + 7 = 10
5 + 7 = 12
use just GC(goldsum);
instead of printf("%d\n", GC(goldsum));
Maybe you misunderstood the return 0; we put in main(). Every function has to return the value we desire. In the case of main we want 0 because that means everything was OK. In the case of your function the return value should be what YOU want from it.
Edit: re-read the question and adding this: Since you don't want a return value from you function you should declare it as void and not have it in a printf() because all the printing you want is in your function.
Void functions do not return a value, so that is what you want.

Recursion, possible error in algo

i am doing one of the simple programin C, sum of digits of 5 digit number.Though i had done it using a simple function but i need to do it with recursion also.I had read many solution on net regarding this problem using recursion and had implemented one of mine.But that is giving error and i cant figure out what mesh i am doing in my algo.
#include<stdio.h>
int sum5(int x); //function for sum of digits of 5 digit number
int main()
{
int x;
int result;
printf("Enter a 5 digit number : ");
scanf("%d",&x);
printf("Number entered by you is %d",x);
result = sum5(x);
printf("Sum of digits of 5 digit number is = %d",&result);
return 0;
}
int sum5(int x)
{
int r;
int sum=0;
if(x!=0){
r=x%10;
sum=sum+r;
x=x-r; //doing this so that 0 come in the last and on diving it by 10, one digit will be removed.
sum5(x/10);
}
return sum;
}
but after its execution i am getting wrong result.It is dumping some anonymous value on the output.
Also, your sum5 function is incorrect. You have to add the value of sum5 to the sum variable of the caller function.
int sum5(int x)
{
int r;
int sum = 0;
if (x != 0) {
r = x % 10;
sum = r;
//x = x - r; - this isn't required. integer division will floor x
sum += sum5(x / 10);
}
return sum;
}
This is incorrect as it is printing the address of result and not its value:
printf("Sum of digits of 5 digit number is = %d",&result);
Change to:
printf("Sum of digits of 5 digit number is = %d", result);
Always check the result of scanf() to ensure a valid value was read:
/* Returns number of assignments made. */
if (scanf("%d", &x) == 1 && x > 9999 && x < 100000)
{
}
Plus the error in the implementation of sum5() as pointed out by Osiris
.

Resources