Skip to main content

Posts

Harshad Number | C programming

  Q.10     WAP in C/ C++/ java to check whether a number is a  Harshad Number  or not.  In recreational mathematics, a Harshad number in a given number base is an integer that is divisible by the sum of its digits when written in that base.         Example: Number 200 is a Harshad Number because the sum of digits 2 and 0 and 0 is 2(2+0+0) and 200 is divisible by 2. Number 171 is a Harshad Number because the sum of digits 1 and 7 and 1 is 9(1+7+1) and 171 is divisible by 9.     Solution :  #include <stdio.h> int   sumofdigits ( int  x)  // function to compute sum of digits {      int   sum = 0 ;      while (x > 0 )     {          sum += x % 10 ;         x = x / 10 ;     }      return   sum ; } void   main () {      int   n ,  sod ;      printf ( " \n Enter any positive number : " );      scanf ( " %d " ,  & n );      if  ( n < 0 )     {          printf ( " \n Invalid input! Enter the value again -->" );          main ();  // calls the m
Recent posts

Special Number | C programming

 Q.9    WAP in C/ C++/ java to check whether the number is a Special Number or not. Definition: If the sum of the factorial of digits of a number (N) is equal to the number itself, the number (N) is called a  special  number. INPUT : 145     The digits of the number are: 1, 4, 5 Factorial of digits: 1! = 1 4! = 4*3*2*1 = 24 5! = 5*4*3*2*1 = 120 Sum of factorial of digits = 1 + 24 + 120 = 145             Hence, the given number 145 is a special number. Solution :  #include   <stdio.h> int   factorial ( int  x)  // function to return factorial of the number {      int   f   =   1 ;      for  ( int   i   =   1 ;  i   <=  x;  i ++ )     {          f   =   f   *   i ;     }      return   f ; } void   main () {      int   n , sum , x ;      printf ( " \n Enter any positive number : " );      scanf ( " %d " ,  & n );      if  ( n   <   0 )     {          printf ( " \n Invalid input! Enter the value again -->" );          main (

Neon Number | C programming

 Q.8     WAP in C/ C++/ java to check whether the number is a Neon number or not. Input: 9 Output: Neon Number Explanation: the square is 9*9 = 81 and the sum of the digits of the square ( 8+1) is 9. Solution :  #include <stdio.h> int   sumofdigits ( int  x)  // function to compute sum of digits {      int   sum = 0 ;      while (x > 0 )     {          sum += x % 10 ;         x = x / 10 ;     }      return   sum ; } int   main () {      int   n ;         printf ( " \n Enter any positive number : " );      scanf ( " %d " ,  & n );      if  ( n < 0 )     {          printf ( " \n Invalid input! Enter the value again -->" );          main ();  // calls the main() function again     }      else     {          if ( n ==   sumofdigits ( n * n ))          printf ( " %d  is a Neon Number \n " ,  n );          else          printf ( " %d  Not a Neon Number. \n " ,  n );     }      return   0 ; }  // end of main()

Calculating Sum of digits & total number of digits | C programming

  Q.7       WAP in C/ C++/ java to find the sum of the digits of the number and also count   the number of digits present.          Example,                      Input : 123,                       Output : Sum of digits = 6             [ expl. 1+2+3]                                          Total digits = 3 Solutions : #include <stdio.h> int   digits ( int  x)  // function to count total no. of digits {      int   c = 0 ;      while  (x > 0 )     {         x = x / 10 ;          c ++ ;     }      return   c ; } int   sumofdigits ( int  x)  // function to compute sum of digits {      int   sum = 0 ;      while (x > 0 )     {          sum += x % 10 ;         x = x / 10 ;     }      return   sum ; } void   main () {      int   n ;      printf ( " \n Enter any positive number : " );      scanf ( " %d " ,  & n );      if  ( n < 0 )     {          printf ( " \n Invalid input! Enter the value again -->" );          main ();  // calls the ma

Finding Factorial of a number | C programming

  Q.6      WAP in C/ C++/ java to find the factorial of the number. Solution :  #include <stdio.h> void   main () {      int   n ;      printf ( " \n Enter any positive number : " );      scanf ( " %d " ,  & n );      if  ( n < 0 )     {          printf ( " \n Invalid input! Enter the value again -->" );          main ();  // calls the main() function again     }      else     {          int   fact = 1 ;          for ( int   i = 1 ;  i <= n ;  i ++ )         {              fact = fact * i ;         }          printf ( "Factorial is  %d\n " ,  fact );     } }  // end of main()

Generate Prime Numbers within a given range | C programming

Q.5    WAP in C/ C++/ java to generate prime numbers between M and N (0> M,N <1000) Solution :  #include   <stdio.h> int   isprime ( int  n) {      int   f = 0 ;      for  ( int   j   =   1 ;  j   <=  n;  j ++ )     {          if  (n  %   j   ==   0 )              f ++ ;     }      if  ( f   ==   2 )      return   1 ;      else      return   0 ; } void   main () {      int   M ,  N ;      printf ( " \n Enter the lower limit : " );      scanf ( " %d " ,  & M );      printf ( "Enter the upper limit : " );      scanf ( " %d " ,  & N );      if  ( M   >   N   ||   M   ==   N   ||   M   <   1   ||   M   >=   1000   ||   N   <   1   ||   N   >=   1000 )     {          printf ( " \n Invalid input. Enter the inputs again -->" );          main ();     }      else     {          printf ( "The prime numbers are : " );          for  ( int   i   =   M ;  i   <=   N ;  i ++ )         {            

Division, Divisor, Result | C programming

 Q.4     WAP in C/ C++/ java to divide two integers (dividend and divisor)              Example, Dividend = 7 Divisor = 2 Result = 3 Dividend = -17 Divisor = 5 Result = -3 Solution : #include <stdio.h> void   main () {      int   a , b ;      printf ( " \n Dividend = " );      scanf ( " %d " ,  & a );           printf ( "Divisor = " );      scanf ( " %d " ,  & b );           printf ( "Result :  %d\n " ,  a / b ); }  // end of main()