Printing Fibonacci numbers up to n in C - c

The following code uses a user define function to print fibonacci numbers up to the user's choosing. The only problem is that the output includes the first n fibonacci numbers, followed by a zero. (e.g. 1,1,2,3,5,8,0) How can I get rid of the zero?
#include <stdio.h>
int fibonacci(int a);
int main()
{
int j, number;
printf("Enter the number of terms: ");
scanf("%d", &number);
printf("Fibonacci Series: ");
j = fibonacci(number);
printf("%d", j);
}
int fibonacci (int a)
{
int num1 = 1, num2 = 1, k, i;
for (i = 1; i <= a; ++i)
{
printf("%d, ", num1);
k = num1 + num2;
num1 = num2;
num2 = k;
}
return 0;
}

I learned that we are supposed to return something when using functions other than main.
If you learned that, you should look for a better learning source. You could well have written:
void fibonacci(int a)
{
int num1 = 1, num2 = 1, k, i;
for (i = 1; i <= a; ++i)
{
printf("%d, ", num1);
k = num1 + num2;
num1 = num2;
num2 = k;
}
}

Related

can someone help me idk which mean to 1 do

I tried to do this:
#include <stdio.h >
int main(void) {
int common = 0;
int num1;
int num2;
int big;
int small;
int i;
printf("Enter the first number : ");
scanf("%d", &num1);
printf("Enter the second number : ");
scanf("%d", &num2);
if (num1 <= num2) {
num1 = small;
num2 = big;
}
else{
num1 = big;
num2 = small;
}
for (i = small ; ; i++)
{
}
I don't know what is meant by to 1 do. I'm beginner in algorithms
but I think it means i++ isn't?
The algorithm says to count from small down to 1. So the repetition should be i--, not i++. Use i >= 1 as the repetition condition to keep looping until you get to 1.
for (i = small; i >= 1; i--)

I think there's something I'm confused about with the variables

#include<stdio.h>
void Multi(int num1, int num2)
{
int a, b;
if (num1 < num2)
a = num1, b = num2;
else
a = num2, b = num1;
for (a; a > b; a++)
{
for (int i = 1; i < 10; i++)
printf("%d * %d = %d \n", a, i, a * i);
}
};
int main(void)
{
int a, b;
printf("enter two numbers :\n");
scanf_s("%d %d", &a, &b);
Dan(a, b);
return 0;
}
Both of these programs do not execute the For statement. I think there's some problem with the variables.
This for loop (the body of the loop)
for (a; a > b; a++)
is never executed because in the preceding code a is set such a way that it is less than or equal to b. So a > b always evaluates to false.
You need to write either
for ( ; a < b; a++)
or
for ( ; a <= b; a++)
Also it would be more safer to write the call of printf the following way
printf("%d * %d = %lld \n", a, i, ( long long )a * i);

Write a program in C to display the n terms of even and odd natural number and their sum using functions

#include <stdio.h>
int sum_even(int n);
int sum_odd(int m);
int main() {
int n;
scanf("%d", &n);
int m;
scanf("%d", &m);
int evensum;
evensum = sum_even(int n);
int oddsum;
oddsum = sum_odd(int m);
printf("the sum of even numbers is %d", evensum);
printf("the sum of odd numbers is %d", oddsum);
return 0;
}
int sum_even(int n) {
int sum = 0, i;
for (i = 2; i <= n; i += 2) {
sum += i;
}
return sum;
}
int sum_odd(int m) {
int SUM = 0, j;
for (j = 1; j <= m; j = j + 2) {
SUM = SUM + j;
}
return SUM;
}
please tell me what is wrong with my code, I am new to coding, I am able to solve questions without using functions but I am confused when I have to use functions
While calling functions you just pass the arguments and not specify its data type.
i.e. You would write it like this :-
evensum = sum_even(n);
oddsum = sum_odd(m);
The general syntax of calling a function which returns a value is :-
return-value = function-name(arg-list);

I need help adding the outputs of a for loop in C

I'm making a code that takes an integer, counts up to the integer, then adds all of the numbers counting up to the integer.
#include <stdio.h>
#include <stdlib.h>
int main () {
int i = 0;
int a = 0;
int sum = 0;
printf("Please enter a number:");
scanf("%i", &i);
for( a = 1; a <= i; a = a + 1 ) {
printf("%i\n", a);
}
sum = sum + a;
printf("The total is %i",a);
return 0;
}
It seems to be adding 1 to whatever the input is.
I want it to add all of the numbers that are produced not just the first
Thanks!
EDIT
#include <stdio.h>
#include <stdlib.h>
int main () {
int i = 0;
int a = 0;
int sum = 0;
printf("Please enter a number:");
scanf("%i", &i);
for( a = 1; a <= i; ++a ) {
printf("%i\n", a);
sum = sum + a;
}
printf("The total is %i",sum);
return 0;
}
Thanks! That is exactly what I wanted to do. I didn't understand that "sum" needed to be part of the for loop to change correctly.
The problem is that your summation is outside of the for-loop here:
for( a = 1; a <= i; a++ ) {
printf("%i\n", a);
}
sum += a;
printf("The total is %i",a);
What this does is print that statement multiple times, then do the sum once.
What you want is this:
for( a = 1; a <= i; a++ ) {
printf("%i\n", a);
sum += a;
}
Also, if you'd like to print the sum, you want printf("The total is %i\n", sum);
rather than printf("The total is %i", a);.

How can I write a program that has an input of n and then calculates the sum of n numbers that the user enters?

I've written this so far:
#include<stdio.h>
int main()
{
int n = 0, i = 0, sum = 0, a = 0;
scanf("%d", &n);
while (i <= n);
{
scanf("\n%d", &a);
sum = sum + a;
i++;
}
printf("%d", sum);
}
but when I enter 8, for example, it won't allow me to add any other numbers.
What's the problem?
while (i <= n); --> while (i <= n). Drop the ;. With the;, the while() loop never ends and { scanf("\n%d", &a); ... is never entered.
Suggest using auto formatting - easy to catch problems like this.
Also, to read n values use < #BLUEPIXY
// while (i <= n)
while (i < n)
#Shabnam You can use this code
#include <stdio.h>
int main()
{
int n, sum = 0, c, value;
printf("Enter the number of integers you want to add\n");
scanf("%d", &n);
printf("Enter %d integers\n",n);
for (c = 1; c <= n; c++)
{
scanf("%d", &value);
sum = sum + value;
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}

Resources