Tricky C Program

Write a C program to find a peculiar two digit number which is three times the sum of its digits.

Questions by dineshtom

Showing Answers 1 - 12 of 12 Answers

#include using namespace std; int main()
{
int n,a,sum=0,dig=0; cout << "Enter a number" << endl;
cin>>n; if(n<=0||n>99)
{
cout<<"Enter 2 digit pecular number";
return 0;
}
a=n; while(n!=0)
{
dig=n%10; sum=sum+dig; n=n/10;
}
if(a==(3*sum))
{
cout<<"Pecular number";
}
else cout<<"Not a pecular number";
return 0;
}

  Was this answer useful?  Yes

shravanthy

  • Sep 3rd, 2010
 

#include<stdio.h>
int main()
{
          int iTemp,iRemainder,iCount;
          for(iCount=10;iCount<=99;iCount++){
                     iRemainder=iCount%10;
                     iTemp=iCount/10;
                      if(iCount==3*(iRemainder+iTemp)){
                                   printf("%d",iCount);
                      }
          return 0;
}

  Was this answer useful?  Yes

sharadat

  • Sep 5th, 2010
 

/*********************************************************************************************************************/
/* The formula to be used is 10x+y 3(x+y) */
/* 10x-3x 3y-y */
/* 7x 2y */
/********************************************************************************************************************/

#include StdAfx.h
#include stdio.h  
int main(int argc char* argv[])
{
int x y;
for(x 1; x < 9 ; x++)
{
for (y 1; y < 9; y++)
{
if (7*x 2*y){ printf(
number d d n x y);
}
}
}
getchar();
return 0;
}

  Was this answer useful?  Yes

olumon

  • Sep 6th, 2010
 


bool TrickyCProgram (int num)
{

    int tens, units, digitSum;
    bool condition = FALSE;
   
    tens = num/10;

     if (  (tens >= 10)  || ( tens == 0) )
     {
          /* if there is ten or more tens or less than one ten
              this is not a two digit number */
          printf("ERROR:: %d is not a 2 digit number", num);
     }
     else
     {
           units  = num%10;
           digitSum =  tens + units;

           if ( num == 3*digitSum)
          {
               condition = TRUE;
          }

      }

     return condition;
}

  Was this answer useful?  Yes

Cmkraj

  • Sep 15th, 2010
 

Here is the simple 2 line program

    for(int i=10;i<100;i++)
        printf("Peculier number %d %sn",i,(i==(((i/10)+(i%10))*3)?"Yes":"No") );

  Was this answer useful?  Yes

Shortest Working Code
Tested for C++ (microsoft visual C++ compiler).

#include <iostream>

int main()
{
using namespace std;

for (int i=10;i<100; i++)
{
int a =  3*((i%10) + ((i-(i%10))/10))  ;
if (a==i)
cout<<i;
}
return 0;
}


Regards
Rahil Choudhary

  Was this answer useful?  Yes

/**
* Print all 2-digit integers where the value is
* N times the sum of the digits (wnere N < 10).
*
* Start with the formula
* 10 * d0 + d1 == N * (d0 + d1)
*
* where d0 represents the tens digit and d1 represents the units digit, we find that
*
* (10 - N) * d0 == (N - 1) * d1, or
* * d0 == (N - 1) * d1 / (10 - N)
*
* So, for example, if N is 3, then
* (10 - 3) * d0 == (3 - 1) * d1, or
* d0 == 2 * d1 / 7
*
* Then it's just a matter of checking that 27 == 3 * (2 + 7)
* The following code cycles through the unit digits 0 through 9, computes the tens digit
according to the formula above, and if the result is valid, prints it to stdout.
*/
void digisum(int N)
{
??int d0, d1;
??int c0 = 10 - N; // it's assumed that N < 10
??int c1 = N - 1;

??printf("All 2-digit numbers where the value is %d times the sum of the digitsn", N);

??for (d1 = 0; d1 < 10; d1++)
??{
????d0 = c1 * d1 / c0;
????if (d0 < 10)
????{
??????int val = d0 * 10 + d1;
??????if (val > 0 && val == N * (d0 + d1))
????????printf("%d * (%d + %d) == %d", N, d0, d1, val);
????}
??}
}

To find the number that's 3 times the sum of its digits, just call the above as

??digisum(3);

  Was this answer useful?  Yes

#include<stdio.h>
#include<conio.h>
void main()
{
int n,temp,rem,sum=0;
printf("enter the two disit number");
scanf("n%d",&n);
for(temp=n;temp>0;temp/=10)
{
rem=temp%10;
sum+=rem;
}
if(n==(sum*3))
printf("the number which is equal to the three times the sum of its disits is %d",n);
else
printf("the number is not three times the sum of its disits");
getch();
}

prithriya

  • Oct 14th, 2010
 

#include<stdio.h>

int main()
{
int x,y,z;

for(z=10;z<=99;z++)
{
  x = z/10;
  y = z%10;

if(z==(3*(x+y)))
   printf("npec no: %dn",z);
}

return 0;
}

  Was this answer useful?  Yes

main()
{
 int num,a[2];
 printf("enter the two digit number:n");
 scanf("%d",&num);
 int i=0;
 while(num!=0)
 {
  a[i]=num%10;
  num/=10;
  i++;
 }
 if((7*a[1])==(3*a[0]))
   printf("%d%dn"a[1],a[0]);
 else
  printf("No such number exists...!!!n");
}
  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions