0

Hii I have developed a code in which i have two check those 2 digit numbers whose 3 times sum is equal to the number
eg:-27=3(2+7)

#include<conio.h>
#include<stdio.h>
int main()
{
clrscr();
int a,b,n;
long sum=0;
loop:for(n=24;n<=28;n++)
{
 a=n/10;
 b=n%10;
 sum=sum+(a+b);

 }
 if(n==(3*sum))
 {
 printf("%d \n",n);
 }
 else
    {
    goto loop;

 }

    getch();
    return 0;
    }

However i am only getting a blank screen with a cursor
can anyone explain

4
Contributors
4
Replies
7
Views
5 Years
Discussion Span
Last Post by scrappy57
0

You've got the logic wrong.

In your code, your for loop does not do any checking of the numbers. It just creates a big value for sum. Then your if statement checks that one big sum value against 28, and then goes back to loop: because whatever that big sum value is, it is not 3*(2+8)

Your if statement should be inside the for loop so that you check each value of n. Also, you need to reset the value of sum to zero every time you go round that for loop.

Edited by Moschops: n/a

0

And you don't need the goto loop. Always try to avoid using goto. You can use do- while in you code, but i think you don't need it anyway.

0

You've got the logic wrong.

In your code, your for loop does not do any checking of the numbers. It just creates a big value for sum. Then your if statement checks that one big sum value against 28, and then goes back to loop: because whatever that big sum value is, it is not 3*(2+8)

Your if statement should be inside the for loop so that you check each value of n. Also, you need to reset the value of sum to zero every time you go round that for loop.

Thank you it worked ...Ya i should place the if code inside the for loop....

0
#include<stdio.h>

void main(){

int num, sum, temp1, temp2, i;

for(num=10;num<100;num++)
{

temp1 = num/10;
temp2 = num%10;


if( num == 3 * (temp1 + temp2))
{

printf("hurry !!! found it %d \n", num);
}
}

return 0;
}

try this mate :P

Edited by scrappy57: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.