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 5 months ago.
Improve this question
#include<stdio.h>
int main(){
int n,m;
int count = 0;
scanf("%d" "%d", &n, &m);
for(n=m;n>0;n=n/10);
printf("%d\n",count++);
return 0;
}
How can I do this with this main code or I just need something edit? I struggled how to can it be count like this sample:
Sample Input 1 (standard input)
1 5
Sample Output 1 (standard output)
1
2
3
4
5
It seems you want a simple counter that counts from one number to some other large number.
#include <stdio.h>
int main(){
int lowerbound, upperbound;
scanf("%d %d", &lowerbound, &upperbound); // string simplyfied
int counter;
for (counter = lowerbound; counter <= upperbound; counter++)
{
printf("%d\n",counter);
}
return 0;
}
That's the classic introductory sample of the for loop that you can find in about every beginner's C text book.
Note the new selfdescribing variable names which makes your code easier to read and to understand.
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 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 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 5 years ago.
Improve this question
I want to know if the format of user defined function i wrote i.e return(xxx) is correct or not .
Because when i compile my code, i have to enter the input 2 times.It might be a silly mistake because i just began learning C language
****MY CODE:****``
#include<stdio.h>
long cube(long x );
long input,answer;
int main (void )
{
printf("Enter a number:");
scanf("%ld ",&input);
answer = cube(input);
printf(" The cube of %ld is %ld",input ,answer);
return 0;
}
long cube(long x )
{
return (x*x*x);
}
****ANswer****
#include <stdio.h>
long cube(long x);
long input, answer;
int main( void )
{
printf("Enter an integer value: ");
scanf("%d", &input);
answer = cube(input);
printf("\nThe cube of %ld is %ld.\n", input, answer);
return 0;
}
long cube(long x)
{
long x_cubed;
x_cubed = x * x * x;
return x_cubed;
}
remove the space after '%ld' it will take one input.
according your code,
scanf("%ld '&input) ;
here compiler at first wants one input for '%ld' then it waits for blank space you used after '%ld'. remove it then it will go next step after one input.
you should use,
scanf("%ld%",&input);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
• Declare an array type of int and size of 5
• Ask user to enter an integer value for each element of the array
• Then display every element of the array user entered
please help me where to start i don't know much about array.
One advice, if you don't learn for yourself, then nobody can teach you. So, please try researching a bit on the internet, or reading a good book before you post a question.
Assuming this is your first mistake, and you wont repeat this without working a little on your own, i'll provide the code and the explanation..
#include<stdio.h>
int main(void)
{
int arr[5], i;
printf("please enter the numbers one by one");
for(i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}
printf("The numbers you entered are\n");
for(i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
}
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.