Friday, January 1, 2016

C program to check whether a number is an Armstrong number

No comments:
/* Also known as narcissistic numbers,
 * Armstrong numbers are the sum of their own digits
 * to the power of the number of digits.
 * Example: 407 = 4^3 + 0^3 + 7^3 = 407
 */
#include <stdio.h>
#include <math.h>
int main(void) {
  int number_to_check = 153;
  int copy_number = number_to_check;
  int digits[10];
  int i = 0, sum = 0, number_of_digits;
  while(number_to_check!=0){
    digits[i] = number_to_check % 10;
    number_to_check = number_to_check/10;
    i++;
  }
  number_of_digits = i;
  for(i = number_of_digits-1; i >= 0; i--){
    sum = sum + pow(digits[i], number_of_digits);
  }
  if(copy_number == sum)
    printf("The supplied number is an armstrong number.");
  else
    printf("The supplied number is NOT an armstrong number.");
  return 0;
}

C programs related to Fibonacci series

No comments:
C program to find Fibonacci series without recursion
#include <stdio.h>
int main(void) {
  int n = 10; // print n fibonacci series
  int a = 0, b = 1, s;
  printf("%d %d ", a,b);
  n = n - 2; // already 2 numbers printed
  while(n--){
  s = a + b;
  printf("%d ",a+b);
  a = b;
  b = s;
  }
  return 0;
}

Thursday, December 31, 2015

C program to add 2 numbers without using arithmetic operators

No comments:
#include <stdio.h>
int main(void) {
  int a = 3, b = 5, c = 0;
  while(b != 0){
    c = a & b; // carry
    a = a ^ b;
    b = c << 1;
  }
  printf("%d",a);
  return 0;
}

Ref: http://www.geeksforgeeks.org/add-two-numbers-without-using-arithmetic-operators/

C program to print various patterns

No comments:
C program to print the below pattern
 ABCDEDCBA
  ABCDCBA
   ABCBA
    ABA
     A

Wednesday, December 30, 2015

C program to round numbers and decimals without using in-built functions

No comments:
Suppose we want to round off 838.274. Depending on which place value we're rounding to, the final result can vary. For example,
  • Round to the nearest hundred (838.274) is 800
  • Round to the nearest ten (838.274) is 840
  • Round to the nearest one (838.274) is 838
  • Round to the nearest tenth (838.274) is 838.3
  • Round to the nearest hundredth (838.274) is 838.27

Reference : http://www.calculatorsoup.com/calculators/math/roundingnumbers.php

C Program to round a number to the nearest thousand
#include <stdio.h>
int main(void) {
  double a=3250;
  int x = (a+500)/1000;
  x = x*1000;
  printf("%d",x); //3000
  return 0;
}

C Program to swap two numbers without using a temporary variable

No comments:
C Program to swap two numbers without using a temporary variable
First Method
#include <stdio.h>
int main(void) {
    int a=3,b=6;
    a = a + b; // a = 9
    b = a - b; // b = 3
    a = a - b; // a = 6
    printf("a=%d,b=%d",a,b);
    return 0;
}

C program to add two numbers using ++ and -- operators

No comments:
First method using ++ and -- operators
#include <stdio.h>
int main(void) {
  int a=3,b=6;
  while(b--){
     a++;
  }
  printf("%d",a);
  return 0;
}