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 1 year ago.
Improve this question
I'm working on my school project and having an issue generating random numbers that are multiples of some other numbers(for example generating randoms that are multiples of 25)
Can someone please help me with the codes?(C language)
Simply generate a random int between 1 and a max multiply-by number, and multiply this with your number (25 in your example)
code-
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MULT_NUM 25
int main()
{
srand(time(NULL));
int r = rand() % 20 + 1;
printf("The random multiple of %d is: %d\n", MULT_NUM, r * MULT_NUM);
return 0;
}
Related
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 4 years ago.
Improve this question
I watched a tutorial which explain int:
but I just get 1:
Enter a:999999999999999999999999999999999999999999
Enter b: 9999999999999999999999999999999999999999999999999999999
a * b = 1
$ ./a.out
Enter a:87999999999999999999999999999999999999999999999999999999999
Enter b: 89999999999998798774334999999999994378969869869869458639534934578365
product = 1
What's the problem?
The reason for you getting 1 instead of -129542144 is that you multiply
999999999999999999999999999999999999999999 and 89999999999998798774334999999999994378969869869869458639534934578365
instead of 300000 and 200000.
Try the following short program you will get the result in the example, -129542144.
#include <stdio.h>
int main()
{
int a = 300000;
int b = 200000;
printf("%d\n", a*b);
return 0;
}
This is assuming that the datatype is int and that it is at least 32 bits.
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.
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