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);
               }             
              
}

Thursday 24 August 2017

C program to find the smallest number divisible by 1 to 20.

Question: 
2520 is the smallest number divisible by 1 to 10. Write a C program to find the smallest number divisible by 1 to 20.

Program:

#include <stdio.h>
int main()
{
                int n,c,j;
                for(n=2520; ;n++)
                {
                                c=0;
                                for(j=1;j<=20;j++)
                                {
                                                if(n%j != 0)
                                                {
                                                                c=1;
                                                                break;
                                                }
                                }
                                if(c==0)
                                {
                                                printf("the smallest number divisible by 1 to 20 = %d",n);
                                                break;
                                }
                               
                }
               

}

Output:


Tuesday 15 August 2017

Birthday Chocolate hacker rank solution in C

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int solve(int n, int s_size, int* s, int d, int m){
    int i,j,sum,c=0;
    for(i=0;i<=n-m;i++)
                {
                                sum=0;
                                for(j=i;j<m+i;j++)
                                {
                                                sum=sum+*(s+j);
                                }
                                if(sum == d)
                                                c++;
                }
    return c;
}

int main() {
    int n;
    scanf("%d", &n);
    int *s = malloc(sizeof(int) * n);
    for(int s_i = 0; s_i < n; s_i++){
       scanf("%d",&s[s_i]);
    }
    int d;
    int m;
    scanf("%d %d", &d, &m);
    int result = solve(n, n, s, d, m);
    printf("%d\n", result);
    return 0;
}

Thursday 10 August 2017

Write a C program to print prime pairs with distance <=3

Write  a program to print all possible pairs of prime numbers which are having distance 3 or less than 3  between them in a natural number line starting from 1 to given number N.
Example: prime pairs with distance <=3 – (2,3),  (2,5),  (3,5),  (5,7)
  (3,7), (5,11) are prime pairs with distance >3

Program:
#include<stdio.h>
main()
{
               int i,j,n,x=2,x1=3,x2=0,flag;
               printf("enter n value");
               scanf("%d",&n);
               printf("(%d,%d)\n",x,x1);
               for(i=4;i<=n;i++)
               {
                              flag=0;
                              for(j=2;j<i;j++)
                              {
                                             if(i%j == 0)
                                             {
                                                            flag=1;
                                                            break;
                                             }
                              }
                              if(flag == 0)
                              {
                                            
                                             x2=i;
                                             if(x2-x<=3)
                                                            printf("(%d,%d)\n",x,x2);
                                             if(x2-x1<=3)
                                                            printf("(%d,%d)\n",x1,x2);
                                             x=x1;
                                             x1=x2;
                              }
               }
}
              
Output: