Monday, August 31, 2015

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

2015 & 2016 Batch Freshers Registration Link

JOIN MY FACEBOOK GROUP FOR UPDATES 

C Program to Find GCD of two Numbers

Note: GCD of any two numbers( say a and b)  lies between 1 and minimum of two numbers

LCM(a,b) * GCD(a,b)=a*b

main()
{
          int num1, num2, min,i;
          printf("Enter two integers: ");
          scanf("%d %d", &num1, &num2);
 
min=(num1>num2)?num2:num1; /* minimum value is stored in variable min */
          for(i=min;i>=1;--i)
          {
                    if(num1%i==0 && num2%i==0)
                    {
                                       printf("HCF of %d and %d is %d", num1, num2,i);
                             break;
                    }
          }
}



C Program to Find LCM of two Numbers

Note: LCM of any two numbers( say a and b)  lies between maximum of two given numbers and product of two numbers

LCM(a,b) * GCD(a,b)=a*b

main()
{
          int num1, num2, max;
          printf("Enter two positive integers: ");
          scanf("%d %d", &num1, &num2);
          max=(num1>num2) ? num1 : num2; /* maximum value is stored in variable max */
          while(1)                       /* Always true. */
          {
                    if(max%num1==0 && max%num2==0)
                    {
                                       printf("LCM of %d and %d is %d", num1, num2,max);
                                       break;          /* while loop terminates. */
                    }
                    ++max;
          }
}





C Program to Display Fibonacci Series

The Fibonacci Sequence is the series of numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
The next number is found by adding up the two numbers before it.
    The 2 is found by adding the two numbers before it (1+1)
    Similarly, the 3 is found by adding the two numbers before it (1+2),
    And the 5 is (2+3),
    and so on!
Example: the next number in the sequence above is 21+34 = 55
It is that simple!
Here is a longer list:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811
/* Displaying Fibonacci sequence up to nth term where n is entered by user. */
#include <stdio.h>
int main()
{
          int count, n, t1=0, t2=1, display=0;
          printf("Enter number of terms: ");
          scanf("%d",&n);
          printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */
          count=2;    /* count=2 because first two terms are already displayed. */
          while (count<n) 
          {
                    display=t1+t2;
                    t1=t2;
                    t2=display;
                    ++count;
                    printf("%d+",display);
          }
}

Contact Form

Name

Email *

Message *