Monday, 24 October 2016

Write a function sum_fibo(n) which sums all the even valued terms in the Fibonacci sequence till 'N'

/*
Write a function sum_fibo(n) which sums all the even valued terms in the Fibonacci sequence till 'N' where n<4*10^6
By Starting with 1 and 2, the first 10 terms will be : 1, 2, 3, 5, 8, 13, 21, 34, 55, 89
*/


#include<stdio.h>
int sum_fibo(int n) {
 int sum=0,i;
 int f1=0, f2=1, f3;
 for(i=0; i<n; i++) {
  f3=f1+f2;
  if(f3%2==0) {
   sum = sum + f3;
  }
  f1 = f2;
  f2 = f3;
 }
 return sum;
}
int main() {
 int n;
 printf("Enter N:");
 scanf("%d", &n);
 printf("\nResult: %d", sum_fibo(n));
 return 0;
}

List all the natural numbers below 'N' that are multiples of 3 or 5.

/*
List all the natural numbers below 'N' that are multiples of 3 or 5.
sum_multiples(10)
we get, 3 + 5 + 6 + 9 = 23
*/

#include<stdio.h>
int sum_multiples(int n) {
 int i,sum=0;
 // Numbers from 3 to 'N'
 for(i=3; i<n; i++) {
  // Check for divisibility by 3 or 5
  if(i%3==0 || i%5==0) {
   // Add the number which is multiple of 3 or 5
   sum = sum + i;
  }
 }
 // return the result
 return sum;
}

int main() {
 int n;
 printf("Enter N:");
 scanf("%d", &n);
 printf("\nResult: %d", sum_multiples(n));
 return 0;
}

Create a function sum_alt_primes(n) such that it sums every alternate prime number till 'n'

/*
Create a function sum_alt_primes(n) such that it sums every alternate prime number till 'n'.

Eg. sum_alt_primes(10) = 7
2 3 5 7
2 + 5 = 7 

Eg. sum_alt_primes(20) = 35
2 3 5 7 11 13 17 19
2 + 5 + 11 + 17 = 35
*/

#include<stdio.h>
int sum_alt_primes(int n) {

 // Variable Declaration
 int i,j,k, count=0,flag,sum=0;

 // Check all numbers from 2 to 'n' to find out prime number.
 for(i=2; i<=n; i++) {
 
  // Initialize a flag to zero
  flag = 0;
 
  // Check a number from above loop whether it is prime number or not.
  for(j=2; j<i; j++) {
  
   // Check whether a number is divisible or not by any other number.
   if(i%j == 0) {
    // Set flag value to ONE
    flag = 1;
    // Break the execution of current loop because the number is not prime
    break;
   }
  }
  // check flag value for prime check
  if(flag == 0) {
   // Increase the counter as we found one prime number
   count++;  
   // Check for alternate prime number
   if(count%2 != 0) {
    // Add the alternate prime number
    sum = sum + i;
   }
  }
 }
 // return the sum of the alternate prime number
 return sum;
}

int main() {
 int n;
 printf("Enter N:");
 scanf("%d", &n);
 printf("\nResult: %d", sum_alt_primes(n));
 return 0;
}

Thursday, 20 October 2016

Circumference and Area of Circle Using C

/*
Circumference and Area of Circle
*/


#include<stdio.h>
#include<math.h> // To find the power of a number, for calculating area.

int main() {

 // Declaration of variables
 float PI = 3.14;
 float area, radius, circumference;

 // Take radius value as input from user
 printf("Enter the radius : ");
 scanf("%f",&radius);

 // Calculate area and circumference of circle
 area = PI * pow(radius, 2);
 circumference = 2 * PI * radius;

 // Print the result
 printf("Circumference : %f unit, Area : %f sq. unit", circumference, area);

 return 0;

}

Bubble Sort Using Pointers In C Programming

/*
It is one of the best example to handle pointers.

Here in this program, I am assuming the pointer as array and operating it as like array.

The array variable are nothing but the pointers which are holding the initial address of the array of specific size.
*/

#include<stdio.h>

int * arraySorting(int *arr, int size)                          // Bubble sort function
{
int i,j,temp;
for(i=0; i<size-1; i++) {
for(j=i+1; j<size; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}

int main()
{
int arr[10] = {5,2,3,0,1,6,5,2,4,7};         // Instead of static initialization, you can initialize dynamically.
int size = 10;
int i = 0;
arraySorting(arr, size);
for(i = 0; i<size; i++) {
printf("%d\t", arr[i]);
}
return 0;
}