Tuesday, 17 March 2015

Conversion Decimal to binary in C program

#include <stdio.h>
 
int main()
{
  int n, c, k;
 
  printf("Enter an integer in decimal number system\n");
  scanf("%d", &n);
 
  printf("%d in binary number system is:\n", n);
 
  for (c = 31; c >= 0; c--)
  {
    k = n >> c;
 
    if (k & 1)
      printf("1");
    else
      printf("0");
  }
 
  printf("\n");
 
  return 0;
}
 
Output of program:


Enter an integer in decimal number system
100
100 
in binary number system is: 
000000000000000000001100100 
 
 

convert the decimal to binary numbers using bitwise operator in C programming

// bitwise operators
#include <stdio.h>

// function prototype
void DisplayBits(unsigned);

int main(void)
{
unsigned p;

// prompt user for input
printf("Enter an unsigned integer: ");

// read and store the data
// scanf("%u", &p);
scanf_s("%u", &p, 1);

// function call
DisplayBits(p);

return 0;
}

// function definition
void DisplayBits(unsigned number)
{
unsigned q;

// 2 byte, 16 bits position operated bit by bit and hide/mask other bits
// using left shift operator, start with 10000000 00000000
unsigned DisplayMask = 1<<15;
printf("%7u = ", number);

for(q = 1; q < 16; q++)
{
// combining variable number with variable DisplayMask
putchar(number & DisplayMask ? '1':'0');
// number variable is left shifted one bit
number<<= 1;
// separate by 8 bits position (1 byte)
if(q % 8 == 0)
putchar(' ');
}
putchar('\n');
}


Output example:

Enter an unsigned integer: 200
200 = 00000000 1100100
Press any key to continue . . .

Program to Print All ASCII Values in C Programming

//Program to Print All ASCII Values in C Programming

#include<stdio.h>
void main() {
   int i = 0;
   char ch;
   for (i = 0; i < 256; i++) {
      printf("%c ", ch);
      ch = ch + 1;
   }
}

Output :

Å É æ Æ ô ö ò û ù ÿ Ö Ü ¢ £ ¥ ₧ ƒ á í ó ú ñ Ñ ª º ¿ ⌐ ¬ ½ ¼ ¡ « » ░ ▒ ▓ │ ┤ ╡ ╢
╖ ╕ ╣ ║ ╗ ╝ ╜ ╛ ┐ └ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬ ╧ ╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐
▀ α ß Γ π Σ σ µ τ Φ Θ Ω δ ∞ φ ε ∩ ≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■     ☺ ☻ ♥ ♦ ♣ ♠
♫ ☼ ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ ← ∟ ↔ ▲ ▼ ! " # $ % &amp; ' ( ) * + , - . / 0 1 2 3 4 5 6
7 8 9 : ; &lt; = &gt; ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ ] ^
_ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ ⌂ Ç ü é â ä à å
ç ê ë è ï î ì Ä

C Program to find greatest in 3 numbers

C Program to find greatest in 3 numbers

#include<stdio.h>
int main() {
   int a, b, c;
   printf("\nEnter value of a, b & c : ");
   scanf("%d %d %d", &a, &b, &c);
   if ((a > b) && (a > c))
      printf("\na is greatest");
   if ((b > c) && (b > a))
      printf("\nb is greatest");
   if ((c > a) && (c > b))
      printf("\nc is greatest");
   return(0);
}

Output :
Enter value for a,b & c : 15 17 21
c is greatest

C Program to find exponent Power Series

//C Program to find exponent Power Series

#include<stdio.h>
#define ACCURACY 0.0001
 int main() {
   int n, count;
   float x, term, sum;
   printf("\nEnter value of x :");
   scanf("%f", &x);
    n = term = sum = count = 1;
    while (n <= 100) {
      term = term * x / n;
      sum = sum + term;
      count = count + 1;
       if (term < ACCURACY)
         n = 999;
      else
         n = n + 1;
   }
    printf("\nTerms = %d Sum = %f", count, sum);
   return 0;
}
 
Output :
Enter value of x:0
Terms = 2 Sum = 1.000000
Enter value of x:0.1
Terms = 5 Sum = 1.105171
Enter value of x:0.5
Terms = 7 Sum = 1.648720
Enter value of x:0.75
Terms = 8 Sum = 2.116997
Enter value of x:0.99
Terms = 9 Sum = 2.691232
Enter value of x:1
Terms = 9 Sum = 2.718279
 
Explanation
A program to evaluate the power series
           x2      x3            xn
ex  =  1 + x + ---  +  --- + ..... + ---- , 0 &lt; x &lt; 1
                2!      3!            n!
It uses if……else to test the accuracy.
The power series contains the recurrence relationship of the type
        Tn   =  Tn-1  (---)   for n &gt; 1
         T1   =  x             for n = 1
         T0   =  1
If Tn-1 (usually known as previous term) is known, then Tn (known as present term) can be easily found by
multiplying the previous term by x/n. Then
  ex   =  T0 +  T1  +  T2 + ...... +  Tn  =  sum

C Program to calculate sum of 5 subjects and find percentage

#include<stdio.h>
int main()
 {
   int s1, s2, s3, s4, s5, sum, total = 500;
   float per;
   printf("\nEnter marks of 5 subjects : ");
   scanf("%d %d %d %d %d", &s1, &s2, &s3, &s4, &s5);
   sum = s1 + s2 + s3 + s4 + s5;
   printf("\nSum : %d", sum);
   per = (sum * 100) / total;
   printf("\nPercentage : %f", per);
   return (0);
}

Output :
Enter marks of 5 subjects : 80 70 90 80 80
Sum : 400
Percentage : 80.00

C Program to calculate gross salary of a person.

#include<stdio.h>
 
int main() {
   int gross_salary, basic, da, ta;
 
   printf("Enter basic salary : ");
   scanf("%d", &basic);
 
   da = (10 * basic) / 100;
   ta = (12 * basic) / 100;
 
   gross_salary = basic + da + ta;
 
   printf("\nGross salary : %d", gross_salary);
   return (0);
}
Output :
 Enter basic Salary : 1000
Gross Salart : 1220