Why is my program not giving correct output? [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 7 years ago.
Improve this question
Where is it logically wrong? I don't find it incorrect but the output it
gives is just 1. It should give all the Armstrong number from 1 to 500.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c=0 ,d,i=1;
while(i<=500)
{
b=i;
while(b>0)
{
a=b%10;
c=(a*a*a)+c;
b=b/10;
}
if(c==i)
` printf("%d",i);
i++;
}
getch();
}

You need to initialize c before the inner loop:
while(i<=500)
{
b=i;
c=0; /* reset 'c' */
while(b>0)
{
a=b%10;
c=(a*a*a)+c;
b=b/10;
}
}
You are using non-standard signature for main(). See: What should main() return in C and C++?

if you run the following code
you will see why there is only one output.
Note: correct declaration of main()
Note: using common functions rather than the proprietary conio.h
Note: uses simple 'for' statement rather than 'while' and increment of 'i'
#include <stdio.h>
#include <stdlib.h>
//#include<conio.h>
int main()
{
//clrscr();
int a;
int b;
int c=0;
int i=1;
for( ; i<=500; i++ )
{
b=i;
while(b>0)
{
a=b%10;
c=(a*a*a)+c;
b=b/10;
}
printf( "a=%d. b=%d, c=%d\n", a,b,c);
if(c==i)
printf("%d\n",i);
} // end for
//getch();
getchar();
return(0);
} // end function main

Related

I tried wiritng a code to calculate the mean of sopfr(x), but it doesnt work [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 months ago.
Improve this question
sopfr
I tried making a code that calculates the mean from sopfr(c) up to sofr(c+i), but when I input the numbers nothing happens
This is the code ive done
#include <stdio.h>
int sopfr(int x);
int main()
{
int c ,i ,sum1=0, a=0;
scanf("%i",&c);
scanf("i",&i);
for(c;c<=c+i;a++)
{
sum1+=sopfr(c);
c+=a;
}
float sum2=sum1/i;
printf("%f",sum2);
return 727;
}
int sopfr(x)
{
int t, n=2, s=0;
scanf("%i",&t);
while(t!=1)
{
if(t%n==0)
{
t/=n;
s+=n;
continue;
}
n++;
}
return s;
}
Remove the scanf() call and make t the function parameters.
int sopfr(int t)
{
int n=2, s=0;
while(t!=1)
{
if(t%n==0)
{
t/=n;
s+=n;
continue;
}
n++;
}
return s;
}

Void function usage issue, I'm new to 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 8 months ago.
Improve this question
the task is to print ascending order numbers using only void function. Any insight on what's wrong with the code? Explanation too would be appreciated
#include<stdio.h>
void listNumbersAsc(int start, int end);
int main()
{
int x,y;
printf("Enter the range of numbers maintaining start at first and end at second:");
scanf("%d%d",&x,&y);
listNumbersAsc(x, y);
}
void listNumbersAsc(int start, int end){
int i,s,e;
if(start>=end){
s=start;
e=end;
}
else{
s=end;
e=start;
}
for(i=s; i<=e; i++){
printf("%d",i);
}
}
This should work:
#include<stdio.h>
void listNumbersAsc(int start, int end);
int main()
{
int x,y;
printf("Enter the range of numbers maintaining start at first and end at second:");
scanf("%d %d",&x,&y);
listNumbersAsc(x, y);
}
void listNumbersAsc(int start, int end){
int i,s,e;
if(start<=end){
s=start;
e=end;
}
else{
s=end;
e=start;
}
for(i=s; i<=e; i++){
printf("%d ",i);
}
}
You if statement in listNumbersAsc function wasn't correct.
Also added a space between two %d in scanf("%d %d",&x,&y) to separate two numbers.
The problem of the function is that this for loop
for(i=s; i<=e; i++){
printf("%d",i);
}
can be executed inly once if initially start is specified equal to end due to this if statement
if(start>=end){
s=start;
e=end;
}
else{
s=end;
e=start;
}
That is due to this if statement start can not be less than end.
Also there are redundant variables in the function
The function can be defined the following way
void listNumbersAsc( int start, int end )
{
if ( end < start )
{
int tmp = start;
start = end;
end = tmp;
}
do
{
printf( "%d ", start );
} while ( start++ != end );
putchar( '\n' );
}
Pay attention to that this loop
for(i=s; i<=e; i++){
shown in your question and in the other answer is in general wrong. If the user will specify the value INT_MAX for the parameter end then the loop can be infinite.

C - Cannot iterate through second string onward [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 5 years ago.
Improve this question
Can anyone tell what I am doing wrong here?
Problem statement:
https://practice.geeksforgeeks.org/problems/good-or-bad-string/0
My code:
#include <stdio.h>
#include<string.h>
int is_vowel(char a) {
if(a==97||a==101||a==105||a==111||a==117){
return(1);
}
return(0);
}
int main() {
//code
int t,i;
scanf("%d",&t);
for(i=0;i<t;i++){
char str[100];
scanf("%s",str);
printf("%s",str);
int c_cnsnt=0;
int c_vwl=0;
int g_b=1;//suppose good
for(int j=0;j<strlen(str);j++){
//("%c",str[j]);
int num=is_vowel(str[j]);
printf("Debug %c %d %d\n",str[j],num,strlen(str));
if(is_vowel(str[j])) {
c_vwl++;
}
else { c_cnsnt++;}
if(c_vwl==c_cnsnt){
c_cnsnt=0;
c_vwl=0;
}
else {
if(c_vwl>5||c_cnsnt>=3){
g_b=0;
break;
}
}
}
printf("%d\n",g_b);
}
return 0;
}
Sample
Input:
2
aeioup??
bcdaeiou??
Output:
1
0
My solution link:
https://code.hackerearth.com/9bca55K
Why does the for loop not work for the 2nd string?
Hint: You have to clear the the consonant and vowel counts after increment the other (e.g {c_vwl++;c_cnsnt=0;}), not when they are equal, and always tests your BAD condition.
I will not give you a sample code. Good luck

Spoj LastDigit Wrong answer [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
Link to the problem
Spoj LastDigit problem
I have tested my code on my machine but in SPOJ it is showing wrong answer.
Here is my code-
#include<stdio.h>
int main()
{
int num=0;
scanf("%d",&num);
while(num--)
{
int a=0;
unsigned long int b;
scanf("%d\t%lu",&a,&b);
a%=10;
b%=100;
if(a==0||a==1)
printf("%d\n",a);
else if(b==0)
printf("1\n");
else if(a==5)
printf("5\n");
else
{
int d=b%4,e=1;
while(d--)
e*=a;
printf("%d\n",e%10);
}
}
return 0;
}
Your program is buggy. You are assuming cycle of length 4 for all digits except 0,1,5. That's incorrect
For instance, consider the input1
2 4
Your program outputs 1 whereas the answer should be last digit of Power(2,4) = last digit of 16 = 6
In SPOJ you get a WA because your program is used to calculate the last digit of not so big number. In the problem, it is clearly mentioned that the value of 'b' lies between '0' and '2,147,483,000' inclusive.
The correct way to solve the problem is by using Modular Exponentiation.
# Wasim Thabraze I used the modular approach but my solution is not accepted because
my solution is taking more than than 700bytes
#include<iostream>
using namespace std;
int lastdigit(int a,long int b);
int main()
{
int t,a;
long int b;
cin>>t;
while(t--)
{
cin>>a>>b;
cout<<lastdigit(a,b)<<"\n";
}
return 0;
}
int lastdigit(int a,long int b)
{ if(b==0){return 1;}
if(a%10==0){return 0;}
if(a%10==1){return 1;}
if(a%10==5){return 5;}
if(a%10==6){return 6;}
int ret,mod=b%4;
if(a%10==2||a%10==8||a%10==4)
{
if(a%10==8){if(mod==1){mod=3;}else if(mod==3){mod=1;}}
if(a%10==4){if(mod==1||mod==3){mod=2;}else if(mod==2){mod=0;}}
if(mod==1){ret= 2;}
else if(mod==2){ret =4;}
else if(mod==3){ret =8;}
else if(mod==0){ret= 6;}
}
else if(a%10==3||a%10==7||a%10==9)
{
if(a%10==7){if(mod==1){mod=3;}else if(mod==3){mod=1;}}
if(a%10==9){if(mod==1||mod==3){mod=2;}else if(mod==2){mod=0;}}
if(mod==1){ret= 3;}
else if(mod==2){ret= 9;}
else if(mod==3){ret= 7;}
else if(mod==0){ret= 1;}
}
return ret;
}

Invalid conversion from int* to int error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm getting an error in this code - invalid conversion from int* to int how can i correct this?
#include <stdio.h>
#include <conio.h>
void walk(int,int,int,int,int,int,int);
int main()
{
int n,i;
scanf("%d",&n);
int a[10],b[10],c[30];
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]); b[i]=0;
}
int j=1;
walk(1,j,0,b,c,n,a);
for(i=1;i<=j;i++)
{
if(c[i]==0)
printf("%d");
else printf("%d",c[i]);
}
getch();
}
void walk(int i,int j,int s,int b[],int c[],int n,int a[])
{
c[j]=a[i];
j++;
if(b[a[i]]!=1)
{
b[a[i]]=1;
walk(a[i],j,s,b,c,n,a);
} else {
c[j]=0;
s++;
i=1;
while(b[i]==1&&i<=n)
i++;
if(i<=n)
walk(i,j,s,b,c,n,a);
else printf("%d",s);
}
}
You're declaring walk as void walk(int,int,int,int,int,int,int) but in the definition you're using some int[] arguments.

Resources