How to design looping number sequence(increment)? [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want make a program to print the number sequence from beginning to end with a negative increment specified.
Input format:
I Put a single line consisting of three integers: the beginning, the negative increment, and end sequence. (ex: scan 9 -2 3
Output format:
Output a sequence of numbers from start to finish is printed per line. End output with a newline character.(it will be:
9
7
5
3
Here is the solution:
#include<stdio.h>
int main(void)
{
int start,step,end,i;
scanf("%d %d %d",&start,&step,&end);
for(i=start;i>=end;i+=step)
printf("%d ",i)
printf("\n");
}

#include<stdio.h>
int main(void)
{
int start,step,end,i;
scanf("%d %d %d",&start,&step,&end);
for(i=start;i>=end;i+=step)
printf("%d ",i)
printf("\n");
}

int start,end,inc;
scanf("%d %d %d",&start,&inc,&end);
for(;start>=end;start-=inc)
printf("%d ",start);

Related

for loop in c from scanf [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am attemping to solve a programming exercice on the CodeChef platform in C, but i'm having trouble reading the input...
Here what i am supposed to read:
2
3
1 4
2 1
0 1
0 1
2
1 3
0 2
1 0
Here is the link to the exercice for more details, but in summary, the first line contains a single integer N, the second line is a integer T, the following T lines all contain 2 space-separated integers X Y, followed by a last line of space separated integers. N is the number of blocks starting by a integer for T.
Here the code that i wrote in order to read this data:
int main(void) {
int T, N, i, j, x, y;
scanf("%d", &T);
for (i=0; i>T; i++) {
scanf("%d", &N);
// ....
for (j=0; j>N; j++) {
scanf("%d %d", &x, &y);
// ....
}
scanf("%d %d",&x, &y);
// ....
}
}
What seems to be happening is the code seems to be only reading the N variable after which the program just exits normally... I've been staring at this for hours and I feel lost, i can't seem to figure out what's going on. Any help is appreciated. Thank ypi

How do you get C to produce output from number inputted in code? [closed]

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 6 years ago.
Improve this question
What code do you have to include to get C to produce an output from number inputted into the system.
#include <stdio.h>
int main()
{
int z;
printf("Please enter a number");
return 0;
}
I want the programme to work out a mathematical equation to the number that the user enters, for example if I enter 5 I want it to work out the exponent 2 of 1 up to that integer input.
You can use scanf function to get input from user like this:
#include <stdio.h>
int main()
{
int z;
printf("Please enter a number");
scanf("%d", &z);
// do your calculations on z
printf("Result: %d", z);
return 0;
}
You can use the scanf function.
For a number (or in your case an integer specifically) the line is
scanf("%d", &z);
where the %d specifies that its reading in an integer and the &z is a pointer to your integer variable z.
Information here: https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm
I'm guessing you're new to C programming, so once you get the basics down I would suggest familiarizing yourself with pointers as they are a critical aspect of the language. Some useful resource here using google-fu : https://www.tutorialspoint.com/cprogramming/c_pointers.htm

Using same integers multiple times in scanf [closed]

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 6 years ago.
Improve this question
is it possible to use same integers in multiple scanf's? For example, I input int i and j, then give them a value in scanf, and print their sum. Then use another scanf to assign different values to the same integers, and now add THEIR sum..
Do you mean this?
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a + b);
scanf("%d %d", &a, &b);
printf("%d\n", a + b);
Of course it would work. The variable's value simply changes. Its the same if you wrote
int a;
a = 4;
. . .
a = 8

Error : Find Largest Number in given numbers [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
This C program helps to find the largest number in given numbers but it is not working. I have highlighted the line where the problem is.
#include<stdio.h>
int main(){
int n,num,i;
int big;
printf("Enter the values of n: ");
scanf("%d",&n);
printf("Enter %d Numbers :",n);
scanf("%d",&big);
for(i=2;i<=n;i++){
scanf("%d",&num); //here is the problem..
//what it is reading as `num` without asking me to entering any thing ?
if(big<num)
big=num;
}
printf("Largest number is: %d",big);
return 0;
}
If you need the program to ask for input in a user-readable way, you can put
printf("Enter number #%d:",i+1);
before that line with scanf.
Anyway, the program will do its job just the same if you remove all printf's (and so, print no prompts, only wait for user input). They are only for users' convenience.
Your first scanf command only reads in your first number from stdin. The one on line 14 reads the rest, one per time around the loop. Then each one gets compared to variable "big", and replaces it it necessary.

C prog that generates/output 3 numbers (any combination from 1 - 20) when added is equal to 20 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
C prog that generates/output 3 numbers (any combination from 1 - 20) when added is equal to 20. Hope you could help me with this one. Thanks in advance.
int main(){
int num1,num2,num3,sum;
do{
printf("%i+%i+%i=%i\n",num1,num2,num3,sum);
} while(sum=20); getch();
}
Try this one. You should be able to modify it according to your needs.
#include<stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=20;i++)
for(j=1;j<=20;j++)
for(k=1;k<=20;k++)
if(i+j+k==20)
printf("%d %d %d\n",i,j,k);
return 0;
}

Resources