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 8 years ago.
Improve this question
i am trying to make a palindrome program using simple logic but unfortunate I can't get the output being typed in Printf( "This is palindrome")
#include<stdio.h>
int main(void)
{
int a,b,c,d,e,f;
printf("enter the 5 digit of palindrome");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
if(a==f && b==e && c==d)
{
printf("Its a Palindrome");
}
else if( a!=f || b!=e || c!=d){
printf("its a palindrome");
}
getchar();
return 0;
}
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
You should have another %d there. Otherwise f contains a garbage value and it'll be never equals a.
I recommend you to reconsider your code and write something more general, like reading a string instead of individual ints.
There are a couple of things wrong with your code:
you call scanf() with 5 %d, but pass 6 parameters (you're missing a %d)
your condition in the else branch is superfluous; you already know that at least one of the pairs (a,f), (b,e), (c,d) doesn't contain two equal values
in your else branch, you have printf("its a palindrome"), whereas it clearly is not a palindrome
#include <stdio.h>
int main(void){
int a,b,c,d,e;
printf("enter the 5 digit of palindrome :");
scanf("%1d%1d%1d%1d%1d",&a,&b,&c,&d,&e);
if(a==e && b==d)
printf("it's a palindrome");
else
printf("it's not a palindrome");
getchar();
return 0;
}
else if( a!=f || b!=e || c!=d){
printf("its a palindrome");
}
If the first and the last digit do not equal then it is not a palindrome.
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 1 year ago.
Improve this question
Can someone please show or tell how to fix my code so that it can determine even numbers. Am new to for loops and C.
EDIT: please paste code here instead of image.
You could have just posted the code along with your question but man I could hardly understand your code so I'll make some silly assumptions about what you were trying to do.
1)
printf("even number is %d",);
Firstly I am doubly sure that this did not compile because of the hanging comma, number two removing the comma will either print rubbish or cause the entire program to crash because that %d is supposed to be accompanied by an integer argument for example:
printf("even number is %d", a);
It has to do with the way C handles variadic arguments.
2)
Instead of handling the input gotten from scanf you went on to check if i is an even number which makes no sense, of course the if statement there checks if it is an even number but then there's no need for asking for input.
3)
I suggest changing your code to this:
#include <stdio.h>
int main()
{
int a, i = 0; //remove one of the variables
printf("Enter 10 positive values\n");
while (i < 10)
{
printf("Enter a number:");
if(scanf("%d", &a) != 1) //ask for input first and check if scanf failed
{
printf("An error occured\n"); /*now scanf will push back the string or whatever it read back in the stdin stream
if it encounters a non integer value ('d' for example) and return 0 if this happens ther's something you can do about that but I'm not going to bother going into that
now such an error can happen because of other reasons do this research on your own*/
return 1;
}
if (a < 0) //check if a is positive
{
printf("%d is not positive please enter an even number\n", a); //print an error message if a is not positive
continue; //go back to the beginning of the loop
}
else
{
if ((a % 2) == 0)
{
printf("%d is an even number\n", a);
}
else
{
printf("%d is not an even number\n", a);
}
i++;
}
}
printf("Done!\n");
return 0;
}
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
I am new to C can anyone help me how to fix this code.
I am getting a error:
'else' without a previous if
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20];
char str2[20];
int value;
printf("Enter the string: ");
scanf("%s",str1);
printf("Enter the string: ");
scanf("%s",str2);
value=strcmp(str1,str2);
if (value==0);
printf("The strings are same");
else
{
printf("The strings are not same");
}
return 0;
}
The problem here is that the semicolon after the if statement
if (value==0);
evaluates to: if value is equal to 0, do nothing.
The following line
printf("The strings are same");
not only would get executed every time, but also breaks the if-else apart so that the following else statement does not find any related if statement.
You could use braces here instead to prevent such problems in the future.
if (value == 0) {
printf("The strings are same");
} else {
printf("The strings are not same");
}
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
Following code returns correctly if I enter ZERO or NEGATIVE value. But, if I enter any positive value it does nothing.
Can someone explain why? My expectation is, it should return factorial of positive number.
#include<stdio.h>
int functionfact(int);
void main()
{
int x,fact;
printf("Input an integer value:\n");
scanf("%d",&x);
if (x<0)
printf("Please enter positive value!!");
else if (x==0)
printf ("The factorial of 0 is 1");
else
{
fact=functionfact(x);
printf("The factorial of %d is %d",x,fact);
}
}
int functionfact(int n)
{
return(n*functionfact(n-1));
}
I've upvoted Kalbi's proposal, but I would write it as:
int functionfact(int n)
{
if (n > 0) {
return n * functionfact(n-1);
} else {
return 1;
}
}
Let's face it: this question is asked by a beginner who is learning about the basics of recursion. Best is to first give a complete understanding of how to deal with such kind of programming (as we all struggled with it before, recursion is not easy), and only afterwards, let's make some typical C oneliners.
As Eric pointed out, there is no way for the function to now when to stop.
It should work if you just change the function to
int functionfact(int n)
{
return n > 0 ? (n*functionfact(n-1)) : 1;
}
Hello Dear!
There is not any termination condition for function functionfact. You can try this...
int functionfact(int n)
{
if(n>1)
return(n*functionfact(n-1));
}
I hope u got your answer.
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 3 years ago.
Improve this question
I was solving a problem on strings
Given a string S, write a program to title case every first letter of words in string.
Input:
The first line consists of an integer T i.e number of test cases. T testcases follow. The first and only line of each test case consists of a string S.
Output:
For each testcase, in a new line, print the required output.
Constraints:
1 <= T <= 100
1 <= |S| <= 1000
Example:
Input:
1
I love programming
Output:
I Love Programming
and for that I came up with this solution.
#include <stdio.h>
#include <string.h>
int main() {
//code
int t,flag;
scanf("%d", &t);
while(t--){
int n,i=0;
scanf("%d", &n);
char str[100];
scanf("%s", str);
while(i<n){
if (str[i]!= str[n-1-i]){
flag = 1;
printf("%d", flag);
break;
}
else{
flag = 0;
printf("%d", flag);
continue;
}
i++;
}
if(flag ==1 )
printf("No\n");
else
printf("Yes\n");
}
return 0;
}
This code works fine when continue is removed, but when the above code is run, it prints 0 infinitly.
Can you help me where I'm going wrong?
Thanks in advance.
The "continue" here means "immediately perform the next iteration in the current loop, in this case the while loop".
In your code the line "i++" is therefor not executed and variable i never changes, thus causing an infinite loop.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
#include<stdio.h>
#include<string.h>
int main()
{
char string1[100],string2[100];
int count,i=0,j=0;
gets(string1);
gets(string2);
for(i=0;i<strlen(string1)-1;i++)
{
for(j=strlen(string2)-1;j<0;j--)
{
if(string1[i]==string2[j])
{
printf("They are reverse of each other");
}
else
printf("They are not");
}
}
}
/* I am trying to check if ABC and CBA are equal by checking the first index of string1 and last index of string2 and incrementing and decrementing resp. */
Try this code, it is used check both stings are reverse to each other or not.
#include<stdio.h>
#include<string.h>
int main()
{
char s1[100],s2[100];
int count,i=0,j=0, flag=1;
gets(s1);
gets(s2);
int l1=strlen(s1), l2=strlen(s2);
if(l1==l2)
{
l2--;
for(i=0;i<l1;i++)
{
if(s1[i]!=s2[l2-i])
{
flag=0;
break;
}
}
if(flag)
printf("Both are reverse to each other\n");
else
printf("Not revrese to each other\n");
}
else
printf("Not revrese to each other\n");
}