Searching number without reminder [closed] - c

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.

Related

Generate random multiple numbers in c [closed]

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;
}

Is there any chance to store many variables without array? [closed]

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;
}

Program to find sum up-to .....nth number [closed]

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
1.2^2+2.3^2+3.4^2+4.5^2+ need to calculate the sum up-to nth number with C programming.
But I can't find any way to solve the program.
When the user in put 4 as the value of n, the sum will be the total of 1.2^2+2.3^2+3.4^2+4.5^2.
Can anyone help me get the algorithm?
A simple for loop would do it:
int compute(int n) {
int i, sum=0;
for(i=1; i<=n; i++) {
int val = i*(i+1)*(i+1);
sum += val;
}
return sum;
}
for(int i=0; i<n; i++). This is a for loop.
Inside the loop, store the loop iterator i in a double variable.
Add 1.2 to it.
Multiply it by itself.
Do something with the result: print it, and/or add it to a sum variable etc.
Do not use the xor operator ^ for this.

Debug C program to sum up all even terms of the Fibonacci Sequence under 4 Million [closed]

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

How do I do this with C? Fibonnacci Sequence [closed]

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

Resources