Friday, December 11, 2015

C Programs on flow control statements (if, switch, loop, break and continue)-8




c program to find out prime factors of given number

Algorithm
1. Find factor of a given number
2. Apply prime number program to check whether factor is prime or not

main()
{
          int num,i=1,j,k;
          printf("\nEnter a number:");
          scanf("%d",&num);
          while(i<=num)
{
                    k=0;
                    if(num%i==0)   /* Finding factor */
          {
                             j=1;
                                      while(j<=i)  /* Check prime or not */
                     {
                                       if(i%j==0)
                                       k++;
                                      j++;
                                      }
                                       if(k==2)
                                      printf("\n%d is a prime factor",i);
                    }
                    i++;
          }
 }


C Program to Display Prime Numbers between Two Intervals

#include <stdio.h>
int main()
{
          int n1, n2, i, j, flag;
          printf("Enter two numbers(intevals): ");
          scanf("%d %d", &n1, &n2);
          printf("Prime numbers between %d and %d are: ", n1, n2);
          for(i=n1+1; i<n2; ++i)
          {
                   flag=0;
                   for(j=2; j<=i/2; ++j)
                   {
                             if(i%j==0)
                             {
                                                flag=1;
                                                break;
                             }
                   }
                   if(flag==0)
                             printf("%d ",i);
          }
          return 0;
}

C Program to Display Armstrong Number Between Two Intervals



#include <stdio.h>
int main()
{
          int n1, n2, i, temp, num, rem;
          printf("Enter two numbers(intervals): ");
          scanf("%d %d", &n1, &n2);
          printf("Armstrong numbers between %d an %d are: ", n1, n2);
          for(i=n1+1; i<n2; ++i)
          {
                   temp=i;
                   num=0;
                   while(temp!=0)
                   {
                                      rem=(temp%10);
                                      num+=rem*rem*rem;
                                      temp/=10;
                   }
                   if(i==num)
                   {
                                      printf("%d ",i);
                   }
          }
          return 0;
}

Contact Geek Placement Preparation

Name

Email *

Message *