Can I run a for loop inside a if statement [closed] - c

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 4 years ago.
Improve this question
Code written in c language trying to get desired output but else condition is not running
#include<stdio.h>
int main()
{
int i;
if (i <= 10)
{
for (scanf("%d", &i);i<=10;i++)
{
printf("%d\n",i);
}
}
else
{
printf("Please enter a valid number");
}
}

You are not initialized your variable named i. You must have to initialize it before using.

Related

Codeforces's problem 151A I've wrote this program in C language , I think it has some problem. Help me out please [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 1 year ago.
Improve this question
151A codeforces link
Here is my code written in C language. It has some problem
#include<stdio.h>
#include<conio.h>
int main ()
{
int n,k,l,c,d,p,nl,np,mm,per,tl,to,sum;
scanf("%d%d%d%d%d%d%d%d",&n,&k,&l,&c,&d,&p,&nl,&np);
mm = k*l;
per = mm/nl;
tl = c*d;
to = p/np;
sum=(per,tl,to)/n;
printf("%d",sum);
return 0;
}
You have to find the minimum value among per, tl, and to and divide that with n instead of just calculating to/n, which you are currently calculating with sum=(per,tl,to)/n;. (per and tl are ignored according to the definition of the comma operator)

Comparing hashses in C [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 4 years ago.
Improve this question
Hi I am comparing the hashes and I can't get the correct output. So please can you guys help me out.
for(int i=0;i<len;i++)
{
if(hash1[i]==hash2[i])
{
return 1;
break;
}
else
return 0;
break;
}
You are using the return keyword to early, once the code hits a return, it goes out of the for-loop. So what you are doing there is comparing just the first element of both hashes.
The break statement also breaks the loop cycle, but in your code it never actually reaches this statement, because there is always a return before.
You should probably try something like:
for (int i=0; i<len; i++) {
if (hash1[i] != hash2[i]) {
return 0;
}
}
return 1;

Recursive function to get sequence not working correctly [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 5 years ago.
Improve this question
I am trying to program a sequence x(n) in C with the following conditions:
x(0)=x
x(n)=1 if x(n-1)=1
x(n)=3*x(n-1)+1 if x(n-1)!=1 and x(n-1) not even
x(n)=x(n-1)/2 if x(n-1) even
I tried the following:
int sequence(int x, int n){
if(n==0){
return x;
}
if (sequence(x,n-1)==1){
return 1;
}
if((sequence(x,n-1)!=1)&&((sequence(x,n-1)%2)!=0)){
return 3*sequence(x,n-1)+1;
}
if((sequence(x,n-1)%2)==0){
return sequence(x,n-1)/2;
}
}
It should give me the n-th element of the sequence with the starting point x. However, it does not work...
Your code does what you proposed it to do. Maybe your original logic is flawed.
f(x,0)=x
f(x,n)=1 if f(x,n-1)=1
f(x,n)=3*f(x,n-1)+1 if f(x,n-1)!=1 and f(x,n-1) not even
f(x,n)=f(x,n-1)/2 otherwise
You can clean up your code somewhat to improve its readability and make it slightly faster/better.
int sequence(int x, int n){
if(n==0){
return x;
}
if (sequence(x,n-1)==1){
return 1;
}
if((sequence(x,n-1)%2)!=0){
return 3*sequence(x,n-1)+1;
}
return sequence(x,n-1)/2;
}

Why is the result of this short C program "3 2"? [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 7 years ago.
Improve this question
Here is the source codeļ¼š
#include <stdio.h>
enum coordinate_type{ RECTANGULAR = 1,POLAR };
int main(void)
{
int RECTANGULAR;
printf("%d %d\n",RECTANGULAR,POLAR);
return 0;
}
Why is the result the following:
3 2
You are redefining what RECTANGULAR is in the main function. It gets initialized with a "random" value, in this case it is 3, but it could be anything else.
POLAR keps its value of 2 because of how the enum is defined.
Try redefining the RECTANGULAR variable in main to see different outputs.

I am getting garbage values for this prob in c [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
I am trying to execute the code below, but along with my answer I am getting some garbage values. Please help me find where I made an error.
int main()
{
int n,i,j,k=0;
int a[100];
printf("Enter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
a[++k]=i;
}
}
for(j=0;a[j]!='\0';j++)
{
printf("\t%d",a[j]);
}
}
int a[100];
C does not initialize the array elements by default. So all the elements that are not assigned in your first loop will have garbage.
What you can do is:
int a[100] = {0};
This will initialize all elements to 0
In C array indexing starts from 0. Preincrement ++k will cause to start array indexing from 1. Change it to k++. Also change
for(j=0;a[j]!='\0';j++)
to
for(j=0;j < k;j++)
to print the only values you have entered.
your code should be
for(i=1;i<=n;i++)
{
if(n%i==0)
{
a[k++]=i;
}
}
for(j=0; j < k;j++)
{
printf("\t%d",a[j]);
}
Here how you can fix this. Next time please describe what you were trying to achive

Resources