Thursday, 7 September 2017

Write a recursive function that displays a positive integer in words.



Write a recursive function that displays a positive integer in words. 
Ex: if the integer is 465 then it is displayed as: FOUR SIX FIVE

Program:
#include <stdio.h>
void rec(int n)
{
               if((n/10)>0)
                              rec(n/10);
               int r=n%10;
               switch(r)
               {
                              case 0:
                                                            printf("zero");
                                                            break;
                              case 1:
                                                            printf("one ");
                                                            break;
                              case 2:
                                                            printf("two ");
                                                            break;
                              case 3:
                                                            printf("three ");
                                                            break;
                              case 4:
                                                            printf("four ");
                                                            break;
                              case 5:
                                                            printf("five ");
                                                            break;
                              case 6:
                                                            printf("six ");
                                                            break;
                              case 7:
                                                            printf("seven ");
                                                            break;
                              case 8:
                                                            printf("eight ");
                                                            break;
                              case 9:
                                                            printf("nine ");
                                                            break;
               }
}
main()
{
               int n;
               printf("enter a number");
               scanf("%d",&n);
               rec(n);
}

Output

 

Wednesday, 6 September 2017

Write a C program to find kth digit from right of a^b.


Given two numbers a and b, Write a C program to find kth digit from right of a^b. 

Program:



#include <stdio.h>
#include <math.h>
void kdigit(int);
main()
{
               int a,b;
               printf("enter two numbers");
               scanf("%d%d",&a,&b);
               kdigit(pow(a,b));
              
}
void kdigit(int n)
{
               int k,r;
               printf("enter k value");
               scanf("%d",&k);
               while(n>0 && k != 0)
               {
                              r=n%10;
                              n=n/10;
                              k--;
               }
              
               if((n<=0 && k != 0))
                              printf("kthdigit is not there");
               else
                              printf("kthdigit is %d",r);
              
}

Thursday, 31 August 2017

C PROGRAM TO CHECK WHETHER THE ENTERED NUMBER IS AUTOMORPHIC OR NOT.

An automorphic number is a number whose square contains the same number as last digits or digit. Example :, 52 = 25,62 = 36

Program:


#include <stdio.h>
main()
{
               int i,n,y,flag=0;
               printf("enter a number");
               scanf("%d",&n);
               y=n*n;
               while(n>0)
               {
                              if(n%10 == y%10)//if units place digits are same
                              {
                                             n=n/10;
                                             y=y/10;
                              }
                              else
                              {
                                             flag=1;
                                             break;
                              }
               }
               if(flag==0)
                              printf("given number is automorphic");
               else
                              printf("given number is not automorphic");
}

Tuesday, 29 August 2017

C program to print the numbers between 1 to n that do not appear in Fibonacci series


Problem:
Write a C program to print the numbers between 1 to n that do not appear in Fibonacci series.

Program:
#include<stdio.h>
void main()
{
int n,i,f0=0,f1=1,f2=f0+f1;
printf("enter the number of terms");
scanf("%d",&n);
while(f2<=n)
                {
                                for(i=f1+1;i<f2;i++) /*print all numbers between f1 and f2 */
                                 {
                                                printf("%d\t",i);
                                 }
                                 f0=f1;
                                 f1=f2;
                                 f2=f0+f1;
 }

}

Output:




(or)

#include <stdio.h>
main()
{
               int i,n,f0=0,f1=1,f2=f0+f1;
               printf("enter the number of terms");
               scanf("%d",&n);
               for(i=1;i<=n;i++)
               {
                              if(i != f2)
                                             printf("%d\t",i);
                              if(i==f2) /* we generate next fibonacci term if all the numbers till  f2 are printed */
                              {
                                             f0=f1;
                                             f1=f2;
                                             f2=f0+f1;
                              }
               }
}

Friday, 25 August 2017

Sherlock and Divisors hacker Rank solution in c



#include <stdio.h>
#include <math.h>
int main()
{
               int n,i,t,c;
               scanf("%d",&t);
               while(t--)
               {
                              c=0;
                              scanf("%d",&n);
                              for(i=1;i<=sqrt(n);i++)
                              {
                                             if(n%i == 0)    //i is divisor of n
                                             {
                                                            if(i%2 == 0)   //even divisor
                                                                           c++;
                                                            if((n/i)%2 == 0 && (n/i)!=i)   //if i is divisor of n then n/i is also divisor of n
                                                                           c++;
                                             }
                              }
                              printf("%d\n",c);
               }             
              
}