Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Perhaps someone could help me out with this.
Using the concept of cycle generate the Fibonacci series until reaching 10000 or a little more than that.
So I have this code and it's supossed to work and show me what I want but it doesn't.
Can somebody tell me what's wrong with it? It opens but it doesn't work #_#
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0,j=0,sum=1,num;
while(sum>=1000){
{
printf("%d\n",sum);
i=j;
j=sum;
sum=i+j;
}
system("pause");
}
The code I made for calculating the Fibonacci sequence is the following:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0,j=0,sum=1,num;
printf("Introduce the limit for the Fibonacci sequence: ");
scanf("%d",&num);
while(sum<num)
{
printf("%d\n",sum);
i=j;
j=sum;
sum=i+j;
}
system("pause");
}
In the first snippet, you have a typo
while(sum>=1000){
should be
while (sum < 10000){
I said 'less than' rather than 'less than or equal to' because of the wording of your assignment.
You want to print out Fn where Fn is the first such number > 10000. Since j is really Fn-1 change the while loop condition to
while (j <= 10000)
{
while sum >= 1000 means it will never start because sum = 1. I think you want <=. The second is an infinite loop because sum is always greater than num
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
There are N students in a class, and Teacher wants to divide these students into some groups. Teacher says that groups consisting of two or less students is not allowed, so Teacher wants to us to have as many groups consisting of three or more students as possible.
Divide the students so that the number of groups consisting of three or more students is maximized.
I have written the code up to the following, but it is not giving the correct result for some test cases. Could anyone please tell me what's wrong
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
if(n%3 == 0){
printf("%d", n/3);
}
else if(n%4 == 0 && n/4 == n/3){
printf("%d", n/4);
}
else if((n-4)%3 == 0){
printf("%d", ((n - 4)/3)+1);
}
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
printf("%d", n/3);
}
Let me know it outs right answer
I believe this is the most simple one
💎
You're not printing anything for (e.g.) n = 20.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Why does when I run the random() function in a while loop it does not need a seed, but when I run it outside of a while loop it requires a seed? I am trying to generate random numbers in rapid succession.
code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int min;int max;
printf("Enter the random min and random max: ");
scanf("%d %d",&min,&max);
srand(time(NULL));
int ran_num = rand() % max + min;
printf("the random number is %d \n",ran_num);
return 0;
}
You can set the seeds like this:srand(time(NULL));,and why you use random function?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I wanna get some numbers from keyboard. But how to store that number without array[] ? Have i chance to do that ? I don't know exact how many numbers come from keyboard. If i had permission of array, its simple. But array is not allowed.
In your situation, I'd still go with arrays, but if you insist on using pointers, this code below will help you. Regardless of whether you need arrays or pointers, you still need to define an upper limit on how many elements can be stored in memory.
It is now up to you to modify the code to make it efficient and pretty to your assignment needs.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int numelements=10;
int curelement=0;
int* data=calloc(1,numelements*sizeof(int));
int* p=data;
int* res=data;
while (curelement < numelements){
scanf("%d",p);
if (*p==0){break;} //exit if number entered is zero.
p++;
curelement++;
}
//print results
while(*res != 0){
printf("%d ",*res);
res++;
}
free(data);
return 0;
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
#include <stdio.h>
int main()
{
long int first=1,second=2,sum=0,a[4000000],i=0;
while(second<4000000)
{
a[i]=first;
second=first+second;
first=second-first;
i++;
}
for(i=0;i<1999999;i++)
{
sum=sum+ a[2*i+1];
}
printf("The required sum is : %d",sum);
}
I am not able to detect the error, it is running an infinite loop I guess
No Compile time errors found but no result obtained
You do not need to store all the numbers under 4 million - just calculate the numbers on the fly in your first and second variables, and add every other one into your sum.
Based on mc110's answer:
#include <stdio.h>
int main()
{
const int LIMIT = 4000000;
long int first=0,second=1;
long int sum=0;
while(second<LIMIT)
{
if( (second % 2) == 0) // we only want even numbers
sum += second;
// could be optimized even more by simply swapping first/second
long int tmp = first;
first = second;
second += tmp;
}
printf("The required sum is : %d",sum);
}
gives me (spoiler alert)
The required sum is : 4613732
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
What does the main problem in my algorithm,I need to find the smallest positive number which divided from 1 to 20 with out divider...
#include <stdio.h>
#include <stdbool.h>
int main(int argc,char* argv[])
{
int num,j=2;
int saveNum=20;
bool flag = false;
while(!flag)
{
num = saveNum;
while(num%j==0 && j<=20)
{
num /= j;
j++;
}
if(j>20)
flag = true;
else
{
saveNum++;
j=1;
}
}
printf("Done");
printf("%d",saveNum);
}
Are you missing a printf to see what your intermediate results are? That might help you get an idea as to what is going on internally.
But I don't really understand what you're trying to solve. Do you want the result: 2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20?
Because I think that's what you are currently computing. However, you'll overflow before you get there and iterating to that point will take you a while.
If instead you're trying to find the smallest number that is divisible by every number less than or equal to 20, then you may want to revisit your update of num /= j.