The number of Fibonacci numbers lesser than a given n - c

#include <stdio.h>
int fibonacci(int n) {
int count, n1 = 0, n2 = 1, fib = 0;
printf("Given number: ");
scanf("%d", &n);
count = 0;
while (count < n) {
fib = n1 + n2;
n1 = n2;
n2 = fib;
++count;
if (n > fib)
printf("%d ", fib);
}
return 0;
}
int main() {
int szam;
fibonacci(szam);
return 0;
}
I've gotten this far, I just don't know how to count the numbers.
for example:
input: 10
output: 1 2 3 5 8
but it should be:
in: 10
out: 5

The stop condition in your code is incorrect: you stop after n fibonacci numbers have been computed instead of stopping when you have computed a fibonacci number larger than n.
Here is a corrected version:
#include <stdio.h>
int count_fibonacci(unsigned long long int n) {
int count = 0;
unsigned long long n1 = 1, n2 = 1, fib = 1;
while (fib < n) {
count++;
fib = n1 + n2;
n1 = n2;
n2 = fib;
}
return count;
}
int main(void) {
unsigned long long n = 0;
printf("Given number: ");
scanf("%llu", &n);
printf("%d\n", count_fibonacci(n));
return 0;
}
It prints 5 for an input of 10, because your fibonacci sequence is: 1 2 3 5 8....
But the standard sequence is usually defined as 1 1 2 3 5 8..., and it should return 6. You can get this behavior bu changing the initial state to n1 = 0, n2 = 1, fib = 1.

Added the variable fib_count that counts the Fibonacci numbers (did not test this...)
#include <stdio.h>
int fibonacci(int n)
{
int n1=0, n2=1, fib=0, fib_count;
printf("Given number: ");
scanf("%d",&n);
fib_count = 0;
while (fib<n)
{`
fib=n1+n2;
n1=n2;
n2=fib;
fib_count += 1;
printf("%d ",fib);
}
printf("Fibonacci numbers smaller than %d : %d ",n, fib_count);
return 0;
}
int main(){
int szam;
fibonacci(szam);
return 0;
}

Related

multiplying number from 1 to N while adding 2 every time

I have to write a C program that multiplies numbers from 1 to N.
N is scanned. Before multiplication, I have to increase each number by 2.
For example: N = 3 => (1+2)(2+2)(3+2) = 60
I have to only use while loop and print and scan function.
Example program:
Enter the value of N: 4
The result of multiplication: 360
This is my code and I am not sure what is wrong with this. Please help.
#include <stdio.h>
int N;
int count=1, ii, result;
printf("Enter the value of N:");
scanf("%d", &N);
while (count<=N)
{
count ii = count + 2;
ii = ii * ii ; //three
count++;
}
result = ii;
printf("The result of multiplication: %d", result);
return 0;
}
If you're looking for that series as a sum:
const int N = 3;
int c = 1;
for (int i = 1; i <= N; ++i) {
c *= (i + 2);
}
Or in a more C-esque form:
const int N = 3;
int c = 1;
for (int i = 0; i < N; ++i) {
c *= (i + 1 + 2);
}
int main()
{
int N;
int count=1, ii = 1, result;
printf("Enter the value of N:");
scanf("%d", &N);
while (count<=N)
{
ii = ii * ( count + 2 };
count++;
}
result = ii;
printf("The result of multiplication: %d", result);
return 0;
}

Extract a number from a position in C

In this exercise he asks me to create a function
Number_pos (N, pos, m) which allows to extract a number composed of m digits
from position pos using functions.
Example:
N = 12345, pos = 2, m = 3
Number_pos (N, pos, m) = 234
I use an Extraxt_from_position function which extracts a number from a given position, then I use a function which calculates the number of digits of the number to extract, then I have a mirror function which inverts the number and I do the successive division until the number of digits are equal to the number of digits of the number we want to extract.
The problem is: forbidden to use mirror function, can you help me
int Extract_from_position(int n, int r)
{
int m = 0, s = 0;
while (n != 0)
{
m = n % 10;
s++;
if (s == r)
{
return n;
}
n = n / 10;
}
}
int Number_of_digits(int n)
{
int m = 0, s = 0;
while (n != 0)
{
m = n % 10;
s++;
n = n / 10;
}
return s;
}
int Mirror(int n)
{
int m = 0, s = 0;
while (n != 0)
{
m = n % 10;
s = s * 10 + m;
n = n / 10;
}
return s;
}
int Number_Pos(int N, int pos, int m)
{
int x = Extract_from_position(N, pos);
int y = 0;
int R = Mirror(x);
int T = Number_of_digits(R);
while (T >= m + 1)
{
y = R % 10;
R = R / 10;
T--;
}
return Mirror(R);
}
int main()
{
int n, pos, nbcx;
printf("Give n :");
scanf("%d", &n);
printf("Give the position :");
scanf("%d", &pos);
printf("give the number of digits of the number to extract :");
scanf("%d", &nbcx);
printf("\nThe result after the amber extract from position %d on the right and the number of digits %d is : %d \n", pos, nbcx, Number_Pos(n, pos, nbcx));
}
UPDATE: Count from right
If you want count the digits from right, the NumberPos function will be just:
#include <stdio.h>
#include <math.h>
int NumberPos(int N, int pos, int m)
{
int trc = (int)(N / (int)pow(10, pos - 1));
trc = trc % (int)pow(10, m);
return trc;
}
int main()
{
int n, pos, nbcx;
printf("Give n :");
scanf("%d", &n);
printf("Give the position :");
scanf("%d", &pos);
printf("give the number of digits of the number to extract :");
scanf("%d", &nbcx);
printf("\nThe result after the amber extract from position %d on the right and the number of digits %d is : %d \n", pos, nbcx, NumberPos(n, pos, nbcx));
}
And the output will be, for example:
Give n :1234567
Give the position :3
give the number of digits of the number to extract :2
The result after the amber extract from position 3 on the right and the number of digits 2 is : 45
OLD: This could be a solution (in basically 4 line):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int NumberPos(int N, int pos, int m)
{
int digit = floor(log10(abs(N))) + 1;
int trc = N % (int)pow(10, digit - pos + 1);
digit = floor(log10(abs(trc))) + 1;
trc = (int)(trc / (int)pow(10, digit - m));
return trc;
}
int main()
{
int n, pos, nbcx;
printf("Give n :");
scanf("%d", &n);
printf("Give the position :");
scanf("%d", &pos);
printf("give the number of digits of the number to extract :");
scanf("%d", &nbcx);
printf("\nThe result after the amber extract from position %d on the right and the number of digits %d is : %d \n", pos, nbcx, NumberPos(n, pos, nbcx));
}
The output will be:
Give n :12345
Give the position :2
give the number of digits of the number to extract :2
The result after the amber extract from position 2 on the right and the number of digits 2 is : 23
UPDATE: Library restriction
If for whatever reason you are not allowed to use math.h or stdlib.h you can:
Re-implement pow reading: Write Pow Function Without math.h in C
Re-implement abs reading: this
Re-implement the digit counter: C program to count number of digits in an integer
Something like this might work. I trim the digits you don't want on the right then mod to mask off the digits on the left you don't want.
Based on your sample I assume that pos is 1-based. If not there's a comment on the code you would need to remove.
You'd probably want to add error checking to make sure that pos and num_digits are valid for the given N, but that's an exercise for you.
#include <stdio.h>
int Number_of_digits(int n)
{
int count = 0;
while (n != 0)
{
n = n / 10;
++count;
}
return count;
}
int Number_Pos(int N, int pos, int num_digits)
{
int len = Number_of_digits(N);
pos -= 1; //pos is 1 based.
//trim right side
for (int i = 0; i < len - num_digits - pos; ++i)
{
N /= 10;
}
//calculate mod to keep num_digits.
int m = 10;
for (int i = 0; i < num_digits - 1; ++i)
{
m *= 10;
}
return N % m;
}
int main()
{
int n = 1234567;
int pos = 2;
int num_digits = 3;
int result = Number_Pos(n, pos, num_digits);
printf("Num: %d, Pos: %d, Digits: %d - Result: %d\n", n, pos, num_digits, result);
pos = 3;
num_digits = 4;
result = Number_Pos(n, pos, num_digits);
printf("Num: %d, Pos: %d, Digits: %d - Result: %d\n", n, pos, num_digits, result);
pos = 1;
num_digits = 4;
result = Number_Pos(n, pos, num_digits);
printf("Num: %d, Pos: %d, Digits: %d - Result: %d\n", n, pos, num_digits, result);
pos = 6;
num_digits = 2;
result = Number_Pos(n, pos, num_digits);
printf("Num: %d, Pos: %d, Digits: %d - Result: %d\n", n, pos, num_digits, result);
return 0;
}
Output:
Num: 1234567, Pos: 2, Digits: 3 - Result: 234
Num: 1234567, Pos: 3, Digits: 4 - Result: 3456
Num: 1234567, Pos: 1, Digits: 4 - Result: 1234
Num: 1234567, Pos: 6, Digits: 2 - Result: 67

is this program correct to calculate the sum of 5 digits

Program to calculate the sum of five digits
This program is showing error in the compiler even though I think its factually correct
#include<stdio.h>
int main()
{
int i,a,num=32765,n;
int sum=0;
a=num%10;
n=num/10;
sum=sum+a;
for(i=0;i>4;i++)
{
a=n%10;
n=n/10;
sum=sum+a;
}
printf("the sum of five digits is %d", sum);
}
The loop in your code is never entered because i=0 and then you check if i>=3 which is never true.
You could use something like this:
int digit_sum(int num){
int sum=0;
while (num !=0){
sum += num%10;
num = num/10;
}
return sum;
}
int main()
{
int num = 12346;
/*
if (num <0) // add this block if negative number is posible
num = -num; // and its ok to change num or use some temp instead
*/
int sum = digit_sum(num);
printf("the sum of five digits is %d",sum);
return 0;
}
Or use recursion:
int digit_sum(int num){
if (num)
return num%10 + digit_sum(num/10);
}
Your loop is never entered because i=0 and cant be greater then 3!!! so the solution is:
int number=12345;
int total=0;
int remainder=0;
while(number>0){
remainder=number%10;
total=total+remainder;
number=number/10;
}
Your code is almost correct, just needed correct loop condition. Added comments so that you can see what is going on:
#include <stdio.h>
int main()
{
int i, a, num = 32765, n;
int sum = 0;
// extract 1st digit
a = num % 10; // a is 5 (% returns the remainder of the division)
n = num / 10; // n is 3276 (should be 3276.5, but int eats 0.5)
sum = sum + a; // sum is 5 which is (0 + 5)
// extract the remaining 4 digits
for (i = 0; i < 4; i++) // i is 0, 1, 2, 3
{
a = n % 10; // a is 6, 7, 2, 3
n = n / 10; // n is 327, 32, 3, 0
sum = sum + a; // sum is 11, 18, 20, 23
}
printf("the sum of five digits is %d", sum);
return 0;
}
https://ideone.com/EI9tgM

Sum of Digits Program not giving correct answer for negative number in c

printf("Sum Digit Program\n");
int n,re, sum = 0;
printf("Enter an integer n="); scanf("%d", &n);
while(n){
re = n % 10;
sum = sum + re;
n = n / 10;
}
printf("Sum digit = %d", sum);
return 0;
}
I try this and it works well with positive integer but when I enter a negative integer like: -323 => -8
It's supposed to be -3+2+3 =2 not -3-2-3=-8
I try using abs function but it still doesn't work right
OP almost had it. Simply treat MSDigit as signed. All other digits, use abs(rem). Works for INT_MIN
printf("Sum Digit Program\n");
int sum = 0;
printf("Enter an integer n=");
scanf("%d", &n);
while (n) {
int re = n % 10;
n = n / 10;
sum += n ? abs(re) : re; // At MSDigit when n==0
}
printf("Sum digit = %d", sum);
Well, you may use conditional operator to store the sign value like int sign = (n >= 0 ? 1 : -1); as shown below -
#include <stdio.h>
#include <stdlib.h>
/*
* #brief Logic for returning sum of digits
*/
int digi_sum(int n)
{
int sign = (n >= 0 ? 1 : -1);
int sum = 0;
n *= sign;
while (n)
{
if (n < 10)
sum += (sign * (n % 10));
else
sum += n % 10;
n /= 10;
printf("Sum: %d, n: %d\n", sum, n);
}
printf("sum: %d, n: %d\n", sum, n);
return sum;
}
/*
* #brief Driver function
*/
int main(int argc, char *argv[])
{
int num = -323;
printf("Sum: %d\n", digi_sum(num));
return 0;
}
The idea is to store the sign of the number into a separate variable and use it when n < 10.
Use n=abs(n/10) instead of n=n/10
#include <stdio.h>
#include <math.h>
main()
{
printf("Sum Digit Program\n");
int n,re, sum = 0;
printf("Enter an integer n="); scanf("%d", &n);
while(n)
{
re = n % 10;
sum = sum + re;
n =abs(n/10);
}
printf("Sum digit = %d", sum);
return 0;
}
You can add a condition to the first line of your loop to make sure that the sum so far is positive when n < 10, after that it will make the subtraction for the least digit if it has too. Then your loop should look like this:
while(n){
if (abs(n) < 10) {
sum = abs(sum);
}
re = n % 10;
sum = sum + re;
n = n / 10;
}
I think that you need a precondition for the first number.
with an if then else.
Solved
I changed the output to see the values
include<stdio.h>
int main(void)
{
int re,n;
int sum =0 ;
printf("Sum Digit Program \n");
printf("Enter an integer n= ");
scanf("%d", &n);
while(n)
{
if (abs(n) < 10) {
sum = abs(sum);
}
re= (n % 10);
sum = sum + re;
n= n / 10;
printf ("\n re = %d , n= %d \n", re, n);
}
printf ("\n sum= %d \n",sum);
return 0;
}

Loop doesn't end

int main(void){
int range = 0, i = 0;
printf("Enter the number of digits of the number: ");
scanf("%d", &range);
int a[range];
int b[range];
printf("Enter the number: ");
for(i = 0; i < range; i++){
scanf("%1d", &a[i]);
}
replace(a, b, range);
swap(&b[0],&b[range]);
printf("Output: ");
for(i = range; i > 0; i--){
printf("%d", b[i]);
}
return 0;
}
void replace(int *a, int *b, int n){
int *p;
int temp;
for(p = a; p < a + n; p++){
temp = ((*p + 6) % 10) / 10;
p = b;
*p = temp;
}
}
void swap(int *p, int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}
My problem is with the first for loop, the loop seems to just keep taking in input and doesn't end. I tried putting a print statement before the replace method and it didn't print so I knew the problem was with the for loop. How do I fix this issue?
The replace() function is a disaster
You've now posted the replace function and it is a disaster.
void replace(int *a, int *b, int n)
{
int *p;
int temp;
for (p = a; p < a + n; p++)
{
temp = ((*p + 6) % 10) / 10;
p = b;
*p = temp;
}
}
You attempt to iterate over the array a by making p point to each element in turn. But you then, in the body of the loop, assign b to p, which places it outside the array a, which means all bets are off. It's not clear whether b is less than a + n or not — it simply isn't defined. But given that you get an infinite loop, it probably is, so your code goes reading from and writing to the same couple of locations over and over (b[0], b[1]) and p never progresses nearer to a + n.
A simple fix uses indexes:
void replace(int *a, int *b, int n)
{
for (int i = 0; i < n; i++)
b[i] = ((a[i] + 6) % 10) / 10;
}
If you want to use pointers, then:
void replace(int *a, int *b, int n)
{
for (int *p = a; p < a + n; p++)
*b++ = ((*p + 6) % 10) / 10;
}
Note that the expression evaluates to zero. The modulo 10 operation produces a value in the range 0..9 (or -9..+9), and that divided by 10 is always 0. You'll need to work on that expression.
The call to swap() is broken
You have:
swap(&b[0], &b[range]);
This definitely accesses data out of the bounds of the b array. To be safe, you need to use:
swap(&b[0], &b[range-1]);
Your output loop is broken
You have:
printf("Output: ");
for(i = range; i > 0; i--){
printf("%d", b[i]);
}
You need to avoid accessing b[range] again, and you need to output a newline at the end:
printf("Output: ");
for (i = range; i > 0; i--)
printf("%d", b[i-1]);
putchar('\n');
The input code works
The input code works, as demonstrated by this minimal adaptation of what you've got:
#include <stdio.h>
int main(void)
{
int range = 0, i = 0;
printf("Enter the number of digits in the number: ");
if (scanf("%d", &range) != 1)
{
fprintf(stderr, "Oops 1\n");
return 1;
}
printf("Number of digits: %d\n", range);
int a[range];
printf("Enter the number: ");
for (i = 0; i < range; i++)
{
if (scanf("%1d", &a[i]) != 1)
{
fprintf(stderr, "Oops 2\n");
return 2;
}
printf("Digit %d: %d\n", i, a[i]);
}
printf("Reversed input: ");
for (i = range; i > 0; i--)
printf("%2d", a[i-1]);
putchar('\n');
return 0;
}
The 'reversed input' loop is an adaptation of the 'output' loop in the question — bug-fixed to avoid accessing the array out of bounds, and using a instead of b. The error messages are very uninformative (not suitable for production work), but they're adequate to identify which statement caused an error while you're debugging.
Example run:
$ ./example-input
Enter the number of digits in the number: 12
Number of digits: 12
Enter the number: 1234 5678 9101112
Digit 0: 1
Digit 1: 2
Digit 2: 3
Digit 3: 4
Digit 4: 5
Digit 5: 6
Digit 6: 7
Digit 7: 8
Digit 8: 9
Digit 9: 1
Digit 10: 0
Digit 11: 1
Reversed input: 1 0 1 9 8 7 6 5 4 3 2 1
$
Now, adapt this into your program and see where the problem really is.
Working code
#include <stdio.h>
static void swap(int *a, int *b);
static void replace(int *a, int *b, int n);
int main(void)
{
int range = 0, i = 0;
printf("Enter the number of digits in the number: ");
if (scanf("%d", &range) != 1)
{
fprintf(stderr, "Oops!\n");
return 1;
}
printf("Number of digits: %d\n", range);
int a[range];
int b[range];
printf("Enter the number: ");
for (i = 0; i < range; i++)
{
if (scanf("%1d", &a[i]) != 1)
{
fprintf(stderr, "Oops 2\n");
return 2;
}
printf("Digit %d: %d\n", i, a[i]);
}
printf("Reversed input: ");
for (i = range; i > 0; i--)
printf("%2d", a[i-1]);
putchar('\n');
replace(a, b, range);
swap(&b[0], &b[range-1]);
printf("Output: ");
for (i = range; i > 0; i--)
printf("%2d", b[i-1]);
putchar('\n');
return 0;
}
static void swap(int *p, int *q)
{
int t = *p;
*p = *q;
*q = t;
}
static void replace(int *a, int *b, int n)
{
for (int *p = a; p < a + n; p++)
*b++ = ((*p + 6) % 10);
}
Example output
$ ./example-input
Enter the number of digits in the number: 9
Number of digits: 9
Enter the number: 123 456 789
Digit 0: 1
Digit 1: 2
Digit 2: 3
Digit 3: 4
Digit 4: 5
Digit 5: 6
Digit 6: 7
Digit 7: 8
Digit 8: 9
Reversed input: 9 8 7 6 5 4 3 2 1
Output: 7 4 3 2 1 0 9 8 5
$

Resources