Tricky C Program questions

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

2) Bacteria are known to multiply very rapidly. If a certain container contains just one bacterium on the first day and there are twice as many on the next day. In this manner the number of bacteria in the container doubles itself everyday. Assuming that the container would be full on the 10th day with 13,312 bacteria, find the number of bacteria that was initially in the container on the first day.

3) Calculate the factorial of a number recursively. From that calculate the value of COS(X) = 1 - X2/2! + X4 /4! - X6/ 6! +…….

4) A number of “Cats” got together and decided to kill between them 999919 mice. Every cat killed equal number of “mice”. Write a program to find number of cats.

5) Consider the following number 45*45=2025; 20+25=45.Write a program to generate number between 32 and 99 that satisfies the above property.

6) Rita has a money pouch containing Rs.700. There are equal number of 25 paise coins, 50 paise and one rupee coins. Write a C program to find how many of each are there?

7) Calculate the factorial of a number recursively and from that calculate the value of ex=1+ (X1/1!) + (X2 /2!) + (X3/3!) + ……….

8) There are some goats and ducks in a farm. There are 60 eyes and 86 foot in total. Write a program to find number of goats and ducks in the farm.

9) Write a C program to find a three digit number which is greater than the aggregate of its third, tenth and the twelfth parts by 58.

10) Write a C program to find a two digit number, the second digit of which is smaller than its first digit by 4, and if the number was divided by the digit’s sum, the quotient would be 7.

Questions by ganesan81

Showing Answers 1 - 75 of 115 Answers

ravikth

  • Oct 31st, 2008
 

Step1: We have to check the condition xy==3(x+y) -> xy/3 = x+y


The above statements unveil two essential points to write efficient program
1. x and y must be int value, not a real (float) value. so the xy will be
divisible by 3.
2. Maximum value of 3(x+y) is 54 if xy equals to 99 (max value of two digit).
so the xy will be less than 54.


Conclusion: we can get the result by checking the numbers which are divisible
by 3 and less than 54 instead of checking all the numbers.
Ans: 27

1  #include<stdio.h>
    #include<math.h>
    main()
{
   int x,y,r,m;
   printf("enter a numbern");
   scanf("%d",&x);
   y=x%10;
    x=x/10;
   r=x+y;
   m=3*r;
   printf("%d",m);
   getch();
}

RamLaw

  • Jun 16th, 2009
 

Answer for Q:5

#include<stdio.h>
int main(){
 int i = 32, n = 99, sq, val1, val2, val3;
 
 while(i <= n){
  sq = i * i;
  val1 = sq/100;
  val2 = sq%100;
  
  val3 = val1 + val2;
  
  if(val3 == i){
  printf("%d satisfy propertyn",i);
  }
 
  i++;
 }
 return 0;
}

#include <stdio.h>
#include <conio.h>
void main()
{
 int i,x,y;
 clrscr();
 for(i=10;i<100;i++)
  {
    x=i/10;
    y=i%10;
    if(((x+y)*3)==i)
         printf("Peculiar Num is : %d",i);
  }
  getch();
}

jothejo2

  • Jun 26th, 2009
 

Q1.
This is what i've done but dere are still some bugs will fix them later
But this is the whole idea.

#include<stdio.h>

int main()
{
      for (i=10;i<=99;i++)
      { 
            j=i;
   //to do the splitting of the two digit value and find the sum of its digits
             while(j != 0)
               {    x = j%10;
                    y = y +x;
                   j = j/10;
                }
 // Because of the relation x +y = xy/3
         c = i/3;
         if (c == y)
           printf("Perculia vlue:  %d",i);
           //break;
          }
}

ranjini.np

  • Aug 6th, 2009
 

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


#include <stdio.h>
int main()
{
    char num;
    char second_pos, first_pos, total;
    for(num = 10; num < 100; num++)
    {
            second_pos = num / 10;
            first_pos = num % 10 ;
            total = second_pos + first_pos;

            printf("total = %d n", total);
            printf(" %d n", (total * 3 ));

            if(num == (3 * total))
            {
                printf("%d is the answern", num);
                return 0;
            }

    }
    return 0;
}

  Was this answer useful?  Yes

npoojasjce

  • Oct 13th, 2009
 

#include<stdio.h>
void main()
{
   int i,a,b,sum;
   for(i=10;i<100;i++)
    {
        a=i/10;
        b=i%10;
       sum=a+b;
       if(b==(a-4) && (i/sum)==7)
          printf("%dn",i);
     }
}

  Was this answer useful?  Yes

meenu1992

  • Oct 23rd, 2009
 

5) Consider the following number 45*45=2025; 20+25=45.Write a program to generate number between 32 and 99 that satisfies the above property.

#include<stdio.h>
#include<conio.h>
void main()
{int i,n,a,b;
for(i=32;i<=99;i++)
{n=i*i;
a=n%100;
n=n100;
b=a+n;
{if(b==i)
printf("%dt",b);
}
}
getch();
}

Ankur1989

  • Jan 22nd, 2010
 

#include<stdio.h>
#include<conio.h>
void main()
{ clrscr();
  int n, x,y,z;
  printf("nEnter the number:");
  scanf("%d",&n);
  x=n%10;
  y=n/10;
  z=x+y;
  if(n==3*z)
{
  printf("nnThis is required number ....");
 }
   else
printf(""nnEnter Some other number");

}

  Was this answer useful?  Yes

Ankur1989

  • Jan 22nd, 2010
 


//Program for numbert being three times its digits

#include<stdio.h>
#include<conio.h>

 void main()
{clrscr();
 
 int n, x,y,z;

 printf("nEnter the number you want to check for");
 
 scanf("%d",&n);

 y=n%10;
 x=n/10;
 z=x+y;

 if(n==3*z)
{
 printf("nThe number is three times some of its digits....");
}
 else

 printf("nnEnter some other number");
 
}

  Was this answer useful?  Yes

Ankur1989

  • Jan 22nd, 2010
 

//Program to calculate bacteria on first day when bacteria on 10th day is 13, 312


 #include<stdio.h>
#include<conio.h>
 
 void main()
{
  clrscr();

 int n, i,x,
printf("nEnter the number of days:");
 scanf("%d",&n);
 n=10;

 printf("nEnter the bacteria's at end of 10th day...:");
 scanf("%d",&total);
 total=13,312;

 printf("nSo total bacteria's initially are.....:");
 x=total/pow(2,n-1);

 }

 

  Was this answer useful?  Yes

Ankur1989

  • Jan 22nd, 2010
 

#include<stdio.h>
#include<conio.h>
#include<math.h>
 
 void main()
{clrscr();
 int n, sum,x;
 float result; 
 
  void main()
 printf("nEnter the number upto which prog is to be calculated :");
 scanf("%d",&n);

 int rec(int n)
{
 if(n==1)
 
 return 1;
 
 else
 

 return (n)*(n-1);

}

 printf("nEnter the value of X:");
 scanf("%d",&x);

 sum= 0;
 sum+=pow(x,2*(n-1))*pow(-1,n);

 result= sum/rec(n);

 printf("nnThe result is...:%f" result);
 }

  Was this answer useful?  Yes

vkscool

  • Sep 19th, 2010
 

programe to fine no where secound no is less then first by 4 and the no is divided by the sum of digit of the no then the quotient is 7

#include<stdio.h>
#include<conio.h>
void main()
{
 int num=20,i,j,k,l,m=0,arr[20];
 while(num<100)
 {
   j=num%10;
   k=num/10;
   if(k-j==4)
     {
       l=j+k;
       printf("n%d",num);
       if((num/l)==7)
         {
           arr[m]=num;
           m++;
         }
     }
     num++;
 }

 for(i=0;i<m;i++)
 {
  printf("n:- %d",arr[i]);
 }

 getch();

 }

  Was this answer useful?  Yes

Q.2.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,day,bact;
clrscr();
printf("Enter the no. of day and the total no. of bacterian");
scanf("%d %d",&day,&bact);
for(i=day;i>1;i--)
b=b/2;
printf("Number of bacteria present in the 1st day is=%dn",bact);
getch();
return 0;
}

Q.6.
#include<stdio.h>
#include<conio.h>
int main()
{
float r;
int x;
clrscr();
printf("Enter the Rs.n");
scanf("%f",&r);
x=r*4/7;
printf("If there r equal no. of 25 paise coins, 50 paise coins and one rupee coins, then the no of each coins is %d n",x);
getch();
return 0;
}

  Was this answer useful?  Yes

Q.8.
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y,p,q;
clrscr();
printf("Enter the no of eyes and foot in totaln");
scanf("%d %d",&p,&q);
x=(q-p)/2;
y=(p-2*x)/2;
printf("The no of goats in the farm is = %dn",x);
printf("The no of ducks in the farm is = %dn",y);
getch();
return 0;
}

//The equations are
//1. 2*(x+y)=p;
//2. 4*x + 2*y=q;

  Was this answer useful?  Yes

Q.10.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,a,b;
clrscr();
for(i=10;i<100;i++)
{
b=i%10;
a=i/10;
if(((a-b)==4)&&(i/(a+b)==7))
printf("%dt",i);
}
getch();
return 0;
}

  Was this answer useful?  Yes

Suyash Patil

  • Jul 22nd, 2011
 

Code
  1. #include<stdio.h>

  2. #include<conio.h>

  3.  

  4.  void main()

  5. {clrscr();

  6.  

  7.  int n, x,y,z;

  8.  

  9.  printf("nEnter the number you want to check for");

  10.  

  11.  scanf("%d",&n);

  12.  

  13.  y=n%10;

  14.  x=n/10;

  15.  z=x+y;

  16.  

  17.  if(n==3*z)

  18. {

  19.  printf("nThe number is three times some of its digits....");

  20. }

  21.  else

  22.  

  23.  printf("nnEnter some other number");

  24.  

  25. }

  Was this answer useful?  Yes

Akul Swamy

  • Jun 29th, 2012
 

Code
  1.  

  2. #include<stdio.h>

  3. main()

  4. {

  5.   int g,d;

  6.   for(g=30,d=0;g>0,d<30;g--,d++)

  7.     {

  8.        if((((g+d)*2)==60)&&(((g*4)+(d*2))==86)&&(g+d==30))

  9.        { printf("The number of goats and ducks in the farm are %d and %d",g,d);

  10.          break;

  11.        }

  12.     }                        

  13.   system("pause");

  14. }

  Was this answer useful?  Yes

anusha

  • Aug 6th, 2012
 

10th ques answer is
62
73

84

Code
  1. #include<stdio.h>

  2. #include<conio.h>

  3. main()

  4. {

  5. int a,b,i,l;

  6. printf("the numbers are:");

  7. for(i=40;i<100;i++)

  8. {

  9. a=i/10;

  10. b=i%10;

  11. if(a>b)

  12. {

  13. if(a-b==4)

  14. {

  15. l=a+b;

  16. if(i/l==7)

  17. {


  18. %d",i);

  19. }

  20. }

  21. }

  22. }

  23. getch();

  24. }

  Was this answer useful?  Yes

Ranjini

  • Mar 12th, 2013
 

Given below the code for question 1

Code
  1. /* C program to find a peculiar two digit number which is three times the sum

  2.  * of its digits.

  3.  * Solution :-

  4.  *  let say number is 10x+y

  5.  *  According to the question 10x+y = 3 ( x + y)

  6.  *  10x + y = 3x + 3y

  7.  *  7x = 2y -------> (1)

  8.  *  If any of the two one digit number satisfies the equation (1) , then

  9.  *  the two digit number is (10x+y)

  10.  *

  11.  */

  12.  

  13. #include<stdio.h>

  14.  

  15. int main(void)

  16. {

  17.     int x = 0;

  18.     int y = 0;

  19.  

  20.     for ( x = 1; x < 10; x++)

  21.     {

  22.         for (y = 1; y < 10; y++)

  23.         {

  24.             /*Check the condition 7x == 2y */

  25.             if( (7 * x ) == (2 * y))

  26.             {

  27.                     printf("The two digit number is %d

  28. ", ((10 * x) + y));

  29.             }

  30.         }

  31.     }

  32.  

  33.     return 0;

  34. }

  35.  

  Was this answer useful?  Yes

Ranjini

  • Mar 12th, 2013
 

Answer for Q:2

Code
  1. /* Bacteria are known to multiply very rapidly. If a certain container contains

  2.  * just one bacterium on the first day and there are twice as many on the next

  3.  * day. In this manner the number of bacteria in the container doubles itself

  4.  * everyday. Assuming that the container would be full on the 10th day with

  5.  * 13,312 bacteria, find the number of bacteria that was initially in the

  6.  * container on the first day.

  7.  * Solution :-

  8.  *  It is geometrical progression

  9.  *  eg :- a, a*r, a*r*r, a*r*r*r, ...

  10.  *  The n-th term of a geometric sequence with initial value a and

  11.  *  common ratio r is given by

  12.  *  an = a * (r to the power (n-1))

  13.  *  here r = 2

  14.  *  an = 13312

  15.  *  n = 10

  16.  *  13312 = a * (2 to the power (10 - 1))

  17.  *  13312 = a * (2 to the power 9)

  18.  *  a = 13312 / (2 to the power 9)

  19.  */

  20.  

  21.  

  22. #include<stdio.h>

  23. #include<math.h>

  24.  

  25. int main (void)

  26. {

  27.     int n = 10;

  28.     int an = 13312;     //nth term of gp , here n is 10 .

  29.     int r = 2;          //common ratio

  30.     int power_value;    //this variable is used for storing (r to the power (n-1))

  31.     float a;              //scale factor

  32.  

  33.     power_value = pow(2, 9);

  34.     // a = an/(2 to the power (n - 1))

  35.     a = an / power_value;

  36.     printf(" The number of bacteria initially present in the container on the first day is %f

  37. ", a);

  38.     return 0;

  39. }

  40.  

  Was this answer useful?  Yes

Ranjini

  • Mar 15th, 2013
 

Answer to the question 4

Code
  1. /*

  2.  * A number of Cats got together and decided to kill between them 999919 mice.

  3.  * Every cat killed equal number of mice. Write a program to find number of cats

  4.  * soln:-

  5.  *  total number of cats = x

  6.  *  total number of mice = 999919

  7.  *  number of mice killed by cat = y

  8.  *  xy = 999919

  9.  */

  10.  

  11.  

  12. #include<stdio.h>

  13.  

  14. int main(void)

  15. {

  16.     int n_of_cats = 0;

  17.     int total_mice = 999919;

  18.     int n_of_mice_killed_by_each_cat = 0;

  19.     int exit_flag = 0;

  20.  

  21.     for( n_of_mice_killed_by_each_cat = 2; n_of_mice_killed_by_each_cat <= 999919; n_of_mice_killed_by_each_cat++)

  22.     {

  23.         for(n_of_cats = 2; n_of_cats <= 999919; n_of_cats++)

  24.         {

  25.             if((n_of_cats * n_of_mice_killed_by_each_cat) == 999919)

  26.             {

  27.                 printf("Each cat killed %d mice and total number of cats present are %d

  28. ", n_of_mice_killed_by_each_cat, n_of_cats);

  29.                 exit_flag = 1;

  30.             }

  31.         }

  32.         if(exit_flag == 1)

  33.         {

  34.             break;

  35.         }

  36.     }

  37.  

  38.     return 0;

  39. }

  40.  

  Was this answer useful?  Yes

Ranjini

  • Mar 15th, 2013
 

Answer to the question - 5

Code
  1. /*

  2.  * Consider the following number 45*45=2025; 20+25=45.Write a program to

  3.  * generate number between 32 and 99 that satisfies the above property.

  4.  * Soln:-

  5.  *  x * x = 100y + z

  6.  *  y + z = x

  7.  *  (y + z) * (y + z) = 100y + z

  8.  *

  9.  */

  10.  

  11. #include<stdio.h>

  12.  

  13. int main(void)

  14. {

  15.     int y = 0;

  16.     int z = 0;

  17.     int x = 0;

  18.     int i = 0;

  19.     for(y = 1; y < 98; y++)

  20.     {

  21.         if( y == 20 )

  22.         {

  23.             continue;

  24.         }

  25.         for( z = 1; (z + y) < 98 ; z++)

  26.         {

  27.             x = y + z;

  28.             if ( x < 33 || x > 99)

  29.             {

  30.                 continue;

  31.             }

  32.             if(x * x == ((100 * y) + z))

  33.             {

  34.                 printf(" The number which follows the propery ( 45 * 45 = 2025 ; 20 + 25 = 45 ) is %d  

  35.  And the property is ( %d * %d = %d ; %d + %d = %d)

  36. ", x, x, x, (x * x), (x *x)/100, (x *x )%100, x);

  37.             }

  38.         }

  39.     }

  40.  

  41.     return 0;

  42. }

  43.  

  Was this answer useful?  Yes

Ranjini

  • Mar 15th, 2013
 

Answer to the question 6

Code
  1. /*

  2.  * Rita has a money pouch containing Rs.700. There are equal number of

  3.  * 25 paise coins, 50 paise and one rupee coins. Write a C program to find

  4.  * how many of each are there?

  5.  * Soln:-

  6.  * total amount = 700

  7.  * coins present are 25 paise, 50 paise and 1 rupee

  8.  * Let x be the total count of each coins

  9.  * then

  10.  * x * (25 + 50 + 100) = 700 * 100

  11.  * x * 175 = 700 * 100

  12.  * x = 700 * 100 /175 = 400

  13.  *

  14.  */

  15.  

  16. #include<stdio.h>

  17.  

  18. int main(void)

  19. {

  20.     int total_amt_in_paise = 700 * 100 ;

  21.     int sum_of_three_coins = 25 + 50 + 100 ;

  22.     int count_of_each_coin = 0;

  23.  

  24.     count_of_each_coin = total_amt_in_paise / sum_of_three_coins ;

  25.  

  26.     printf("The total count of each coins(25paise, 50paise, 1rs) is %d

  27. ", count_of_each_coin);

  28.  

  29.     return 0;

  30. }

  31.  

  Was this answer useful?  Yes

manasa

  • Mar 15th, 2013
 

Code
  1.  

  2. #include<stdio.h>

  3. main()

  4. { int a=27,b,p,sum;

  5.         b=a;

  6.         while(b>0)

  7.         {

  8.                 p=b%10;

  9.                 sum=sum+p;

  10.                 b=b/10;

  11.                 }

  12.                 if(a==(3*sum))

  13.                

  14.                 printf("

  15. %d is the REQUIRED number:");

  16.                         else

  17.                         printf("

  18. not THE REQUIRED number:");

  19.                        

  Was this answer useful?  Yes

ranjini

  • Mar 15th, 2013
 

Answer to the Question 9

Code
  1. /*

  2.  *  Write a C program to find a three digit number which is greater than the

  3.  *  aggregate of its third, tenth and the twelfth parts by 58.

  4.  *  Soln:-

  5.  *  Let x be the 3 digit number .

  6.  *  x = (x / 3) + (x /10 ) + (x /12) + 58

  7.  *  x = ( 120 x + 36 x + 30 x )/ 360 + 58

  8.  *  360 x - 186 x = 58 * 360

  9.  *  174 x = 58 * 360

  10.  *  x = (58 * 360 ) / 174

  11.  */

  12.  

  13.  

  14. #include<stdio.h>

  15.  

  16. int main(void)

  17. {

  18.     float x ;

  19.  

  20.     for(x = 100; x < 1000; x++)

  21.     {

  22.         if(x == ((x / 3) + (x / 10) + (x / 12)) + 58)

  23.         {

  24.             printf("%f

  25. ", x);

  26.         }

  27.     }

  28.  

  29.     return 0;

  30. }

  31.  

  Was this answer useful?  Yes

ranjini

  • Mar 15th, 2013
 

Answer to the question 1

Code
  1. /* C program to find a peculiar two digit number which is three times the sum

  2.  * of its digits.

  3.  * Solution :-

  4.  *  let say number is 10x+y

  5.  *  According to the question 10x+y = 3 ( x + y)

  6.  *

  7.  */

  8.  

  9. #include<stdio.h>

  10.  

  11. int main(void)

  12. {

  13.     int x = 0;

  14.     int y = 0;

  15.     int lhs = 0;

  16.     int rhs = 0;

  17.     for ( x = 1; x < 10; x++)

  18.     {

  19.         for (y = 1; y < 10; y++)

  20.         {

  21.             /*Check the condition (10* x )+ y == 3 * ( x + y) */

  22.             lhs = (10 * x) + y;

  23.             rhs = 3 * (x + y);

  24.  

  25.             if( lhs == rhs)

  26.             {

  27.                     printf("The two digit number is %d

  28. ", lhs);

  29.             }

  30.         }

  31.     }

  32.  

  33.     return 0;

  34. }

  35.  

  Was this answer useful?  Yes

Ranjini

  • Mar 15th, 2013
 

Answer to the question 10

Code
  1. /*

  2.  * Write a C program to find a two digit number, the second digit of which is

  3.  * smaller than its first digit by 4, and if the number was divided by the

  4.  * digits sum, the quotient would be 7.

  5.  * Soln:-

  6.  * Let the two digits are x and y and two digit number is (10x+y)

  7.  * y = x - 4

  8.  * (10x+y)/x+y = 7

  9.  */

  10.  

  11. #include<stdio.h>

  12.  

  13. int main(void)

  14. {

  15.     float x = 0;

  16.     float y = 0;

  17.  

  18.     for(x = 1; x < 10; x++)

  19.     {

  20.         for(y = 0; y < 10; y++)

  21.         {

  22.             if(((((10 * x) + y ) / (x + y)) == 7) && (y == (x - 4)))

  23.             {

  24.                 printf("The two digit number which satisfy the condition ( (10x+y)/x+y = 7 ) and ( y = x - 4) is %f

  25. ", (10 * x) + y);

  26.             }

  27.         }

  28.     }

  29.    return 0;

  30. }

  31.  

  Was this answer useful?  Yes

muskan

  • Jul 29th, 2013
 

Code
  1. #include<stdio.h>

  2. #include<conio.h>

  3.  

  4. int main()

  5. {

  6.     int n;

  7.     printf("enter the number");

  8.     scanf("%d",&n);

  9.     int a = n/10;

  10.     int b= n%10;

  11.     int c = a+b;

  12.     int x= n/c;

  13.     if(x==3)

  14.     printf("the number which is 3 times of sum of its digit is:%d",n);

  15.     getch();

  16.     return 0;

  17. }

  18.  

  Was this answer useful?  Yes

muskan

  • Jul 29th, 2013
 

Q:2;

Code
  1. #include<stdio.h>

  2. #include<conio.h>

  3.  

  4. int main()

  5. {

  6.     int n;

  7.     n=13312;

  8.   int i;

  9.     for(i=0;i<10;i++)

  10.     n=n/2;

  11.    

  12.     printf("no. of bacteria in container : %d",n);

  13.  

  14.    

  15.     getch();

  16.     return 0;

  17. }

  18.  




  Was this answer useful?  Yes

muskan

  • Aug 1st, 2013
 

Q3:Calculate the factorial of a number recursively. From that calculate the value of COS(X) = 1 - X2/2! + X4 /4! - X6/ 6! +. . .+Xn/n!.

Code
  1. #include<stdio.h>

  2. #include<conio.h>

  3.  

  4. long factorial1(int n);

  5.  

  6. int main()

  7. {

  8.     int n;

  9.   int i;

  10.   long fact=1;

  11.  

  12. printf("enter the number:");

  13. scanf("%d",&n);

  14. int x;    

  15. printf("enter X:");

  16. scanf("%d",&x);

  17.  

  18.  float sum=0;

  19.  

  20. for(i=0;i<=n;i++)

  21. {

  22.  int m= i%2;

  23.  if(m==0)

  24.  {

  25.     m=i/2;

  26.     if(m%2==0)

  27.      {

  28.        sum = sum+(float)(pow(x,i)/factorial1(i));

  29.        }  

  30.        else

  31.        {

  32.        sum = sum - (float)(pow(x,i)/factorial1(i));                

  33.        }

  34.  

  35.  

  36. }

  37.  

  38. }    

  39. printf("cos(x):%f",sum);

  40.     getch();

  41.     return 0;

  42. }

  43.  

  44.  

  45.  

  46. long factorial1(int n)

  47. {

  48.      

  49.      if(n==0)

  50.      return 1;

  51.      else

  52.    return (factorial1(n-1) * n);

  53.          

  54.      

  55.      

  56.  }

  57.  


  Was this answer useful?  Yes

Code
  1. //answer to 2025 question

  2.  

  3. #include<stdio.h>

  4.  

  5. main()

  6.  

  7. {

  8.  

  9. printf("only for two digit numbers...

  10. ");

  11.  

  12. int x,y,i;

  13.  

  14. int b=0;

  15.  

  16.  

  17. for(i=10;i<100;i++)

  18.  

  19. {

  20.  

  21. b=0;

  22.  

  23. x=i*i;

  24.  

  25.  

  26. while(x)

  27.  

  28. {

  29.  

  30. y=x;

  31.  

  32. b=b+(y%100);

  33.  

  34.  

  35. x=x/100;

  36.  

  37.  

  38. }

  39.  

  40. if(b==i)

  41.  

  42. printf("number=%d, num*num=%d

  43. ",i,i*i);

  44.  

  45. }

  46.  

  47. }

  48.  

  Was this answer useful?  Yes

Code
  1. //code for goat and duck

  2. #include<stdio.h>

  3.  

  4. //with command line input...

  5.  

  6. main(int argc,char **argv)

  7.  

  8. {

  9.  

  10.  

  11. int eyes,foot,gf,ge,de,df;

  12.  

  13. int r1,r2,cnt1=0,cnt2=0,r3,r4;

  14.  

  15. if(argc!=3)

  16.  

  17. {

  18.  

  19. printf("wrong entry...

  20. ");

  21.  

  22. }

  23.  

  24. else

  25. {

  26.  

  27. eyes=atoi(argv[1]);

  28.  

  29. foot=atoi(argv[2]);

  30.  

  31. ge=eyes/2;

  32.  

  33. printf("two possiblities....

  34. ");

  35.  

  36. printf("**************************************************************

  37. ");

  38.  

  39. printf("first possblity if goats assumed to be more than ducks

  40. ");

  41.  

  42. printf("no of goats: %d

  43. ",(foot/4));

  44.  

  45. printf("foot= %d

  46. ",foot/4);

  47.  

  48. r1=ge-(foot/4);

  49.  

  50. printf("remaing eyes= %d

  51. ",r1);

  52.  

  53. r2=foot-((foot/4)*4);

  54.  

  55. while(r2>=2)

  56.  

  57. {

  58.  

  59. if(cnt1<r1)

  60. cnt1++;

  61.  

  62. r2=r2/2;

  63.  

  64. }

  65.  

  66. printf("remaining foots= %d

  67. ",r2);

  68.  

  69. printf("no of ducks: %d

  70. ",cnt1);

  71.  

  72.  

  73. printf("*****************************************

  74. ");

  75.  

  76. printf("second possblity if ducks assumed to be more than goats

  77. ");

  78.  

  79. r3=foot/2;

  80.  

  81. r4=0;

  82.  

  83. while(r4<r3)

  84.  

  85. {

  86.  

  87. cnt2++;

  88.  

  89.  

  90. r4++;

  91.  

  92. if(cnt2>(eyes/2))

  93.  

  94. break;

  95.  

  96. }

  97.  

  98. printf("no of ducks: %d

  99. ",cnt2);

  100.  

  101. printf("foot= %d

  102. ",foot/2);

  103.  

  104. r1=ge-(foot/2);

  105.  

  106. printf("remaing eyes= %d

  107. ",r1);

  108.  

  109. r2=foot-((foot/2)*2);

  110.  

  111. while(r2>=2)

  112.  

  113. {

  114.  

  115. if(cnt1<r1)

  116.  

  117. cnt1++;

  118.  

  119. r2=r2/2;

  120.  

  121. }

  122.  

  123. printf("remaining foots= %d

  124. ",r2);

  125.  

  126. printf("no of goats: %d

  127. ",cnt1);

  128.  

  129.  

  130.  

  131. }

  132.  

  133.  

  134.  

  135.  

  136. }

  137.  

  138.  

  Was this answer useful?  Yes

paritosh

  • Aug 24th, 2014
 

#include
using namespace std;
int main()
{
int end=13312;
for(int i=0;i<10;i++)
{
end=end/2;
}
cout<<"The number is :"< return 0;
}

~
~

  Was this answer useful?  Yes

Rohan

  • Jun 18th, 2015
 

It says find the magic number, so we need to check all possible combinations.
Maximum number is 3*(9+9) = 54. So we can check for all numbers from 0 to 54.

Code
  1.     #define MAX_POSSIBLE_NUM 3*(9+9)

  2.     int num_to_check;

  3.     float a=0,b=0;

  4.  

  5.     for(a=0;a<=(MAX_POSSIBLE_NUM/10);a++){

  6.         for(b=0;b<=9;b++){

  7.             num_to_check = (int)((a/10 + b/100)*100);

  8.             if(num_to_check == 3*(a+b))

  9.                printf("Number to check = %d

  10. ", num_to_check);

  11.         }

  12.     }

  13.  

  Was this answer useful?  Yes

Rohan

  • Jun 18th, 2015
 

I dont know if its optimized but works

Code
  1. {/*There are some goats and ducks in a farm. There are 60 eyes and 86 foot in total.

  2.    Write a program to find number of goats and ducks in the farm */

  3.  

  4.    int num_goats=0, num_ducks=0, animals=0;

  5.    int eyes = 60, feet = 86;

  6.  

  7.    animals = eyes/2;

  8.  

  9.    for(num_goats=0;num_goats<animals;num_goats++){

  10.        for(num_ducks=0;num_ducks<animals;num_ducks++){

  11.            if((num_goats*4 + num_ducks*2 == feet ) && (num_goats*2 + num_ducks*2 == eyes) && (num_goats + num_ducks == animals))

  12.                printf("Goats :: %d & Ducks :: %d

  13. ", num_goats,num_ducks);

  14.  

  15.        }

  16.    }

  17. }  

  18.  

  Was this answer useful?  Yes

Aneesh

  • Jul 29th, 2016
 

27

Code
  1. #include<stdio.h>

  2. #include<conio.h>

  3.  void main()

  4.   {

  5.     int i,k,a,sum=0;

  6.     clrscr();

  7.      for(i=10;i<=99;i++)

  8.       {

  9.           k=i;

  10.           a=k%10;

  11.           k=k/10;

  12.           sum=(a+k)*3;

  13.           if(sum==i)

  14.            printf("%d",i);

  15.        }

  16.      getch();

  17.   }

  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