C language puzzle [closed] - c

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 1 year ago.
Improve this question
I am not sure if this kind of questions are appropriate here, but...
By changing (or adding) one character make this program print '*' exactly 20 times
void main(){
int i, n=20;
for (i=0; i<n; i--)
printf("*");
}
Any ideas?

It seems you mean the following change from
for (i=0; i<n; i--)
^^^
to
for (i=0; i<n; n--)
^^^
Here is one character i is substituted for one character n.
Another way (if it is allowed by the puzzle) is to add one character '-' like
for (i=0; -i<n; i--)
^^^
Note: By the way pay attention to that according to the C Standard the function main without parameters shall be declared like :)
int main( void )

either do i++ or (i-- & n-=2or any other positive number greater the 1)

That is pretty easy, just change the i-- to n--.
Maybe next Time a Harder Puzzle? ;)

Related

Please tell me what's wrong with this code [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 2 years ago.
Improve this question
#include<stdio.h>
void main()
{
int i=1,s=0;
do{
s+=i;
}while(i<=10);
printf("sum is %d",s);
}
this code is not giving any output, please tell me what is wrong.
You have an infinite loop because the variable i that is used in the condition of the do-while loop is not being changed within the loop. It stays equal to 1 as it was initialized.
do{
s+=i;
}while(i<=10);
It seems you mean
do{
s += i++;
} while( i <= 10 );
or
do{
s += i;
} while( ++i <= 10 );
Pay attention to that according to the C Standard the function main without parameters shall be declared like
int main( void )
Also it is desirable to flush the output buffer by including the new line character '\n' in the output stream like
printf( "sum is %d\n", s );
you didn't increment i;
do
{
s+=i;
i++;
}while(i<=10);

for loop in c not outputting full array [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 6 years ago.
Improve this question
this is a simple question as i'm a new-comer to C. I am trying to write a script for outputting an array of the tangents of radians, of multiples of 5 from 0-60. but for some reason the for loop i have written only does this for the first element, and all other elements in the resulting array are 0.00, and it wont print them for each loop. i'm sure i've done something simple wrong with my loop, but i just can't see it.
#include <stdio.h>
#include <math.h>
float rad(float degree){
return degree*M_PI/180;
}
int main(void){
int i, j, dim=13;
float Tan[dim];
for(i=0; i<13; i++);{
j+=5;
Tan[i]=tan(rad(j));
printf("%f\n", Tan[i]);
}
return 0;
}
First of all, in your code
j+=5;
is undefined behavior, as the intial value of j is indeterminate. To elaborate, j is an automatic local variable and not initialized explicitly, so the content is indeterminate.
Then, the for loop is also buggy.
for(i=0; i<13; i++);
should be
for(i=0; i<13; i++) // no ; here
to have a meaningful loop body to be executed.
1. You have inserted a semi-colon which you shouldn't have. Change your loop to :
for(i = 0; i < 13; i++){ //erase the ; after the parenthesis
j+=5;
Tan[i] = tan(rad(j));
printf("%f\n", Tan[i]);
}
2. Initialize variable j before trying to increase it with the statement j+=5, as this will lead to undefined behaviour.
There are two problems in your code :-
1) You haven't initialized j here int i, j, dim=13;
2) The way you used for loop is as per your requirement.Remove semicolon from the for loop statement.

Ask user to enter an integer value for each element of the array [closed]

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

How do i increment statement or decrement statement 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 7 years ago.
Improve this question
There are actually two ways i can do this. One is to use the increment operator ++ and decrement operator –. For example, the statement “x++” means to increment the value of x by 1. Likewise, the statement “x –” means to decrement the value of x by 1. Another way of writing increment statements is to use the conventional + plus sign or – minus sign. In the case of “x++”, another way to write it is “x = x +1″.
But why am i incrementing like this in my code and what does it mean?
for(i=0; i < numberOfProducts; ++i){
printf("Enter Product Name: ");
scanf("%s", &(pProducts+i)->productName);
printf("Enter Product Price: ");
scanf("%f", &(pProducts+i)->price);
}
My question is why did i use ++i for it to work? i tried i++ but could not print.
for(i=0; i < numberOfProducts; ++i)
and
for(i=0; i < numberOfProducts; i++)
are both equivalent as you are not reading the result of the operation (like in a = i++ vs a = ++i). The latter form is more common.
If you have different results, you probably have issues in the way you are testing your program.
++i is a pre-increment operation, meaning that i is first incremented then the incremented value is used in an expression. i++ is a post-increment operation, meaning that the existing value of i is first used in an expression, then it is incremented.
For example:
i=3;
x = 6 - i++;
printf("x=%d\n",x);
Outputs 3.
i=3;
x = 6 - ++i;
printf("x=%d\n",x);
Outputs 2.

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.

Resources