Creating table for Fibonacci Sequence - c

Thank you in advance. I appreciate any and all feedback. I am new to programming and i am working on an assignment that prints the Fibonacci Sequence based on how many numbers the user asks for. I have most of the code complete, but there is one remaining piece I am having difficulty with. I would like my output in a table format, but something is off with my code and I am not getting all of the data I would like in my output. In grey is my code, my output, and my desired output.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, n;
int sequence = 1;
int a = 0, b = 1, c = 0;
printf("How many Fibonacci numbers would you like to print?: ");
scanf("%d",&n);
printf("\n n\t\t Fibonacci Numbers\n");
printf(" %d \t\t\t%d\n \t\t\t%d\n ", sequence, a, b);
for (i=0; i <= (n - 3); i++)
{
c = a + b;
a = b;
b = c;
sequence++;
printf("\t\t\t%d\n ", c);
}
return 0;
}
Here is my output:
How many Fibonacci numbers would you like to print?: 8
n Fibonacci Numbers
1 0
1
1
2
3
5
8
13
Here is my desired output:
How many Fibonacci numbers would you like to print?: 8
n Fibonacci Numbers
1 0
2 1
3 1
4 2
5 3
6 5
7 8
8 13

I am not getting all of the data
That's because you are not printing the sequence in printf() of the for loop.
printf("\t\t\t%d\n ", c);
and even before 2nd number before for loop
printf(" %d \t\t\t%d\n \t\t\t%d\n ", sequence, a, b);
try making the below changes to your code :
printf(" %d \t\t\t%d\n %d\t\t\t%d\n ", sequence, a, sequence+1, b);
sequence++; //as you've printed 2 values already in above printf
for (i=0; i <= (n - 3); i++)
{
c = a + b;
a = b;
b = c;
printf("%d\t\t\t%d\n ",++sequence, c);
//or do sequence++ before printf as you did and just use sequence in printf
}
sample input : 5
sample output :
How many Fibonacci numbers would you like to print?: 5
n Fibonacci Numbers
1 0
2 1
3 1
4 2
5 3
Edit : you can do it using functions this way... it's nearly the same thing :)
#include <stdio.h>
void fib(int n)
{
int i,sequence=0,a=0,b=1,c=0;
printf("\n n\t\t Fibonacci Numbers\n");
printf(" %d \t\t\t%d\n %d\t\t\t%d\n ", sequence, a, sequence+1, b);
sequence++;
for (i=0; i <= (n - 2); i++)
{
c = a + b;
a = b;
b = c;
printf("%d\t\t\t%d\n ",++sequence, c);
}
}
int main()
{
int n;
printf("How many Fibonacci numbers would you like to print?: ");
scanf("%d",&n);
fib(n);
return 0;
}

You forgot to print sequence in the for loop. Print sequence along with c in the for loop after giving proper number of \t!

This would work, also it is better to properly indentate your code:
int main() {
int i, n;
int sequence = 1;
int a = 0, b = 1, c = 0;
printf("How many Fibonacci numbers would you like to print?: ");
scanf("%d",&n);
printf("\n n\t\tFibonacci Numbers\n");
printf(" %d\t\t\t%d\n", sequence, a);
printf(" %d\t\t\t%d\n", ++sequence, b); // <- and you can use pre increment operator like this for your purpose
for (i=0; i <= (n - 3); i++) {
c = a + b;
a = b;
b = c;
sequence++;
printf(" %d\t\t\t%d\n",sequence, c);
}
return 0;
}
Output:
How many Fibonacci numbers would you like to print?: 4
n Fibonacci Numbers
1 0
2 1
3 1
4 2

Related

Write a C program that accepts two numbers and finds all Armstrong numbers in that range

In the above mentioned I wanted to ask that what I have done wrong in my code I have tried debugging it many times but was not able to understand the logical error in my code.
Any help would be appreciated.
#include <stdio.h>
#include <math.h>
int digit(int n);
int digit(int n) {
int a;
double i = 0;
do {
a = n % (int)(pow(10, i));
i++;
} while (a != n);
return i;
}
void is_armstrong(int n);
void is_armstrong(int n) {
int a, b;
double sum;
for (int i = 0; i < digit(n); i++) {
a = n / (int)pow(10, (double)i);
b = a % 10;
sum += pow((double)b, 3);
}
if ((int)sum == n) {
printf("%d is an armstrong number.\n", n);
}
}
int main() {
int a, b;
printf("Please input the left hand limit of range : \n");
scanf(" %d", &a);
printf("Please input the right hand limit of range : \n");
scanf(" %d", &b);
for (int i = a; i <= b; i++) {
is_armstrong(i);
}
is_armstrong(153);
return 0;
}
This code is not even showing 153 an Armstrong number.
Noting the comments about using the power function and what your ultimate outcome is in identifying Armstrong numbers over a given range, I did a bit of refactoring to simplify the process in identifying such numbers. Following is the code snippet that provides the functionality.
#include <stdio.h>
void is_armstrong(int n) {
int a, b, c, d;
int sum = 0;
a = n;
c = 0;
while (a != 0) /* Determine the number of digits to raise to a power */
{
a = a / 10;
c = c + 1;
}
a = n; /* Reset the work number */
while (a != 0) /* Noted from the comments to simplify the test */
{
b = a % 10;
d = b;
for (int i = 1; i < c; i++)
{
d = d * b;
}
sum = sum + d; /* Just mulitply each digit by itself the required number of times */
a = a / 10; /* Divide by 10 along with using the modulo function to evaluate each digit */
}
if (sum == n) {
printf("%d is an armstrong number.\n", n);
}
}
int main() {
int a, b;
printf("Please input the left hand limit of range : \n");
scanf(" %d", &a);
printf("Please input the right hand limit of range : \n");
scanf(" %d", &b);
for (int i = a; i <= b; i++) {
is_armstrong(i);
}
return 0;
}
Following are some key points.
Since the power function is not needed, the math.h include file is not needed and linking the math library is also not needed.
Acquiring each digit is simplified by just utilizing the modulo operation in combination with integer division by "10".
Acquiring the value of each digit raised to the nth power is simplified by just performing a repeated multiplication.
Following is test output at the terminal.
#Dev:~/C_Programs/Console/Armstrong/bin/Release$ ./Armstrong
Please input the left hand limit of range :
1
Please input the right hand limit of range :
10000
1 is an armstrong number.
2 is an armstrong number.
3 is an armstrong number.
4 is an armstrong number.
5 is an armstrong number.
6 is an armstrong number.
7 is an armstrong number.
8 is an armstrong number.
9 is an armstrong number.
153 is an armstrong number.
370 is an armstrong number.
371 is an armstrong number.
407 is an armstrong number.
1634 is an armstrong number.
8208 is an armstrong number.
9474 is an armstrong number.
And as a confirmation, it can be seen that the value "153" was recognized as an Armstrong number.
Give the code snippet a try and see if it meets the spirit of your project.
There is no need to count the number of digits in n, you can just sum the cubes of each digit, one at a time dividing the number by 10 at each iteration.
Here is a simplified version:
#include <stdio.h>
void is_armstrong(int n) {
int sum = n;
while (n != 0) {
int b = n % 10;
n /= 10;
sum -= b * b * b;
}
return sum == 0;
}
int main() {
int a, b;
printf("Please input the left hand limit of range:\n");
if (scanf("%d", &a) != 1)
return 1;
printf("Please input the right hand limit of range:\n");
if (scanf("%d", &b) != 1)
return 1;
for (int i = a; i <= b; i++) {
if (is_armstrong(i)) {
printf("%d is an Armstrong number.\n", i);
}
}
return 0;
}

Printing the number with highest sum of devisors

i have a homework but i cant get the answer
I need to write a program in C...
Here is what is needed: You need to enter "n" natural number as input , and from all the natural numbers smaller than "n" , its needed to print the number which has the highest sum of devisors.
For exp: INPUT 10 , OUTPUT 8
Can anyone help me somehow?
I would really appreciate it !
i tried writing a program for finding the devisor of a number but i cant get far from here
#include <stdio.h>
int main() {
int x, i;
printf("\nInput an integer: ");
scanf("%d", &x);
printf("All the divisor of %d are: ", x);
for(i = 1; i < x; i++) {
if((x%i) == 0){
printf("\n%d", i);
}
}
}
I have implemented using function which will takes input number from user and then return the sum of divisor. hope this is one you looking for
/* function to return of sum of divisor
** input: x: integer number from user input
** return sum: sum of divisor of x
*/
int sum_of_divisor(int x)
{
int sum = 0;
for(int i = 1; i < x; i++)
{
if((x%i) == 0)
{
printf("%d\n", i);
sum = sum+i;
}
}
return sum;
}
int main() {
int x, i;
printf("\nInput an integer: ");
scanf("%d", &x);
printf("All the divisor of %d are: ", x);
printf("the sum of divisor is %d ", sum_of_divisor(x));
return 0;
}
Output:
Input an integer: 10
All the divisor of 10 are: 1
2
5
the sum of divisor is 8
After checking if i is a divisor of x, you should then store that value in another variable, for example m.
Repeat until a new divisor i is higher than that number. Add this new value to m.

Write a program in C that will take a base and n digits and will output a decimal number represented by those digits

I have to write a program in C that will take a base b from the user (assuming b is between 2 and 10), a natural number n and then n numbers that represent the digits of some number m in base b. The program should print out what decimal number m was input. For example, if you put b=5 and n=4 and then the numbers 3 ,4, 2 and 1 the program should output 486 because m=3*5^3+4*5^2+2*5^1+1*5^0=486
Note: You can assume that the digits will be the numbers between 0 and b-1.
So here's what I've done:
#include<stdio.h>
#include<math.h>
int main(void) {
int x,n,b,k=0,num=0,i,j;
scanf("%d", &b);
scanf("%d", &n);
for(i=1; i<=n; i++) {
scanf("%d", &x);
for(j=1; j<b; j++){
if(j>k){
num=num+x*(pow(b,n-j));
k=j;
break;
}
}
}
printf("m=%d", num);
return 0;
}
Can you tell me why this doesn't work for the numbers given in the example above? It outputs 485 instead of 486, while if I take for example b=7, n=3 and then numbers 5, 6 and 1, I get the correct solution m=288.
I suggest checking the return value of scanf(), Something like this is the right idea:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int
main(int argc, char *argv[]) {
int base, n, i, x, sum = 0, power;
printf("Enter base: ");
if (scanf("%d", &base) != 1) {
printf("Invalid base.\n");
exit(EXIT_FAILURE);
}
printf("Enter n: ");
if (scanf("%d", &n) != 1) {
printf("Invalid n.\n");
exit(EXIT_FAILURE);
}
power = n-1;
printf("Enter numbers: ");
for (i = 0; i < n; i++) {
if (scanf("%d", &x) != 1) {
printf("Invalid value.\n");
exit(EXIT_FAILURE);
}
sum += x * pow(base, power);
power--;
}
printf("Sum = %d\n", sum);
return 0;
}
Input:
Enter base: 5
Enter n: 4
Enter numbers: 3 4 2 1
Output:
Sum = 486
You need some small change to your logic.
#include <stdio.h>
#include <math.h>
int main(void) {
int x, n, b, num = 0, i;
scanf("%d", &b);
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &x);
num += x * pow(b, n - i);
}
printf("m=%d", num);
return 0;
}
Test
gcc -Wall main.c -lm
$ ./a.out
5
4
3
4
2
1
m=486
Test 2
./a.out
7
3
5
6
1
m=288
OK, so given a binary number, we can output a decimal number very easily. Just printf("%d%\n", x);
Next job is to convert a number given digits and base into a binary (machine representation) number.
int basetointeger(const char *digits, int b)
{
assert(b >= 2 && b <= 10);
// code here
return answer;
}
Now hook it all up to main
int main(void)
{
int x;
int base;
char digits[64]; // give more digits than we need, we're not worrying about oveflow yet
/* enter base *?
code here
/* enter digits */
code here
x = basetointger(digits, base);
printf("Number in decimal is %d\n, x);
}

Fibonacci Series in C Program where FIRST 2 numbers are given by user

When the first number is 1 and second number is 2, and the length is 5, it should be 1 2 3 5 8. But then my output is always 1 2 1 3 4. I can't seem to find the problem.
Another input is 2 and 5. Output is 2 5 1 6 7. The 3rd number which is 1 shouldn't be there. What should I change or add?
*This is already a submitted HW and yes its wrong I got the deductions already. Now I just want to fix this so I can study this.
int main()
{
int i, lenght = 0, fib, sum, sum1, sum2, a, b, c;
printf("\nFirst number: ");
scanf("%d", &a);
printf("\nSecond number: ");
scanf("%d", &b);
printf("\nHow long?: ");
scanf("%d", &lenght);
{
while ((a > b) || ((lenght < 2) || (lenght > 100)))
{
printf("\nFirst number: ");
scanf("%d", &a);
printf("\nSecond number: ");
scanf("%d", &b);
printf("\nHow long?: ");
scanf("%d", &lenght);
}
}
printf("%d\t%d\t", a, b);
printf("%d\t", fib);
for (i = 3; i < lenght; i++) {
if (i <= 1) fib = i;
else {
a = b;
b = fib;
fib = a + b;
}
printf("%d\t", fib);
}
}
The first time you print fib (before the for loop), you haven't assigned it anything yet.
Since this is for study, issues with your code: you don't need to duplicate the calls to scanf(), simply initialize one of the variables to fail (which you did: lenght = 0) and let the loop do its thing; pick one indentation style and stick with it; if you're new to C, always include the curly braces, even when the language says they're optional; you (correctly) allow for a length of 2, but then print three numbers; your if (i <= 1) clause is a no-op as the loop starts with for (i = 3; so i is never less than 3.
Putting it all together, we get something like:
#include <stdio.h>
int main() {
int length = 0, a, b;
while (length < 2 || length > 100 || a > b ) {
printf("\nFirst number: ");
(void) scanf("%d", &a);
printf("\nSecond number: ");
(void) scanf("%d", &b);
printf("\nHow long?: ");
(void) scanf("%d", &length);
}
printf("%d\t%d\t", a, b);
for (int i = 2; i < length; i++) {
int fib = a + b;
printf("%d\t", fib);
a = b;
b = fib;
}
printf("\n");
return 0;
}
Note that the input error checking isn't sufficient to prevent problems. E.g. b can be greater than a, but still mess up the sequence if you input random numbers. You're assuming the user knows to put in two adjacent items from fibonacci sequence which is tricky to test.
just add fib=a+b; before printing fib value.
Its a good coding habit to initialize all variable before using it(especially in C).
Your code seems strange to me. I tried to simplify your code a bit. See this once:
int main()
{
int a,b,next,last,i;
printf("Enter the first Value:");
scanf("%d",&a);
printf("Enter the second Value:");
scanf("%d",&b);
printf("Enter the length of Fab. series:");
scanf("%d",&last);
printf("%d,%d,",a,b);
for (i=3; i<= last; i++)
{
next = a + b;
if(i<last)
printf("%d,",next);
else
printf("%d",next);
a = b;
b = next;
}
return 0;
}
Hope it's Helpful!!

Half pyramid of numbers

I need to generate the following output of odd numbers in pyramid pattern.
The output will be like
1
3 3
5 5 5
7 7 7 7
I have written the following code. What portion i should modify?
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
m=1;
for(c=1; c<=m; c++)
printf("%d",r);
printf("\n");
}
}
return 0;
}
Current Output:
Current output is like-
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
You could write:
for(r=1; r <= num; r+=2) //we only need odd numbers
{
times = r/2 + 1; //how many times to print odd number
for(c=1; c <= times; c++)
printf("%d",r); //print one character at a time
printf("\n");
}
You'll probably understand it better if you only iterate through odd numbers. I'm currently doing that and calculating how many times I need to print that number, then I'm just printing it as many times as times is.
you have 2 errors in this code .
m is not declared anywhere .
you are running a infinite loop
try this .
#include<stdio.h>
int main()
{
int num,r,c,m;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
m=r;
for(c=1; c<=m; c++)
printf("%d",r);
printf("\n");
}
}
return 0;
}
In your code, instead of
m=1;
you should write
m= ( (r/2) + 1);
Oterwise, all the time, you'll be iterating in the for loop only once.
Some little modifications and it works:
#include<stdio.h>
int main()
{
int num,r,c,m=0;
printf("Enter structure number : \n");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
m++;
for(c=1; c<=m; c++)
printf("%d ",r);
printf("\n");
}
}
return 0;
}
m is undeclared
line feed at the end of the printf message
m incremented each odd iteration
space between printed unmbers
Here is a demo.
the inner for loop should look like this:
for(c=1; c <= r/2; c++)
printf("%d ",r);
just think about it for a second. you want to print a rounded r/2 of numbers in every line, right?
like:
3/2 -> 1.5 -rounded-> 1 -> prints: 3
5/2 -> 2.5 -rounded-> 2 -> prints: 5 5
and so on.
you can run the code here on ideone.com
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
int m=r;
for(c=1; c<=m; c++)
{
if(c%2 != 0){
printf("%d ",r);
}
}
printf("\n");
}
}
return 0;
}
and test
sh-4.3# main
Enter structure number : 9
1
3 3
5 5 5
7 7 7 7
9 9 9 9 9
In row 1, you should print 1.
In row 2, you should print 3.
In row 3, you should print 5.
In row 4, you should print 7.
............................
............................
In row n, you should print 2*n-1.
You can check this:
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
for(c=1; c<=r; c++)
printf("%d",2*r-1);
printf("\n");
}
return 0;
}
Please take a look at this:
for(r=1; r<=num; r+=2) // increment by 2, work for r= 1,3,5,7...
{
for(c=1; c<=r; c+=2)// increment by 2
printf("%d",r);
printf("\n");
}
If you want alternate numbers like 1,3,5,7... just increment value by 2.

Resources