Thursday, September 1, 2011

Simple Numbers and Series with Nested Loop in C (Part IV)


Simple Numbers and Series with Nested Loop in C (Part IV)


Magic Numbers : When we added up the digits of a number and
e.g. 19 = 1 + 9 = 10 = 1+ 0 = 1

//C – Magic Number

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

  void main()
      {
           int s=0,m,n,f=0;
           clrscr();
           printf("Enter the number ");
           scanf("%d",&n);
           m=n;
           while(m>9)
              {
                   s=0;
                   while(m!=0)
                        {
                            s=s+m%10;
                            m=m/10;
                        }
                   if(s==1)
                        {
                            f=1;
                            break;
                        }
                   m=s;
              }
           if(f==1)
                   printf("%d  is magic number",n);
           else
                   printf("%d is not magic number",n);
           getch();
      }

Happy Numbers : 28 = 22 + 82 = 4 + 64 = 68 = 62 + 82 =100 =1


//C – Happy Number

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

  void main()
      {
           int s=0,m,n,f=0;
           clrscr();
           printf("Enter the number ");
           scanf("%d",&n);
           m=n;
           while(m>9)
              {
                   s=0;
                   while(m!=0)
                        {
                            s=s+pow(m%10,2);
                            m=m/10;
                        }
                   if(s==1)
                        {
                            f=1;
                            break;
                        }
                   m=s;
              }
           if(f==1)
                   printf("%d  is happy number",n);
           else
                   printf("%d is not happy number",n);
           getch();
      }


Special Number / Krishnamurthy Number : 145 = 1! + 4! + 5! = 145.


//C – Special Number

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

 void main()
  {
    int n,r,s=0,i,f,m;
    clrscr();
    printf("Enter a No : ");
    scanf("%d",&n);
    m=n;
    do
       {
             r=m%10;
             f=1;
             for(i=1;i<=r;i++)
               {
                     f=f*i;
               }
             s=s+f;
            m=m/10;
        }while(m>0);
    if(s==n)
           printf(" %d is a Special No ",n);
    else
           printf("%d is not a Special No ",n);
    getch();
   }

Sorting the digits of a given number without using array


//C – Digit Sorting

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

void main()
      {
           long int s=0,m,n,i;
           clrscr();
           printf("Enter the number ");
           scanf("%ld",&n);
           m=n;
           printf("\nNumber %ld and in Sorted order ",n);
           for(i=0;i<=9;i++)
              {
                   while(m!=0)
                        {
                            s=m%10;
                            if(i==s)
                                      printf("%ld",s);
                            m=m/10;
                        }
                   m=n;
              }
           getch();
      }

Frequency of each digits in a given number


//C – Digit Frequency

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

void main()
      {
           long int s=0,m,n,i,c;
           clrscr();
           printf("Enter the number ");
           scanf("%ld",&n);
           m=n;
           printf("\nNumber %ld Digit Frequency :\n\n",n);
           for(i=0;i<=9;i++)
              {
                   c=0;
                   while(m!=0)
                        {
                            s=m%10;
                            if(i==s)
                                      c++;
                            m=m/10;
                        }
                   m=n;
                   if(c>0)
                            printf("%ld Repeated %ld Times \n",i,c);
              }
           getch();
      }



Prime Numbers between a given Ranges


//C – Prime Number

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

  int isPrime(int N)
      {
           int j;
           for(j=2;j<=N/2;j++)
              {
                   if(N%j==0)
                            return 0;
              }
           return 1;
      }

  void main()   //Start of main
      {
           int Lower, Upper,i,j,flag=0;   //variables
           clrscr();
           printf("\nEnter the Lower Range : ");
           scanf("%d",&Lower);
           printf("\nEnter the Upper Range : ");
           scanf("%d",&Upper);
           for(i=Lower;i<=Upper;i++) //start of outer loop
              {
                   if(isPrime(i)==1)
                            printf("%d  ",i);
              }
           printf("\nPress Any key to Cont...");
           getch();
      }


No comments: