Posts

Showing posts from September, 2020

DECIMAL TO BINARY CONVERSION IN C

Image
In this program , you will learn to write a program to convert Decimal numbers into Binary numbers.  To understand this Decimal number to Binary number conversion program, you must have basic knowledge about Decimal and Binary numbers. Some binary numbers for the decimal numbers: Program to convert Decimal number into binary number: //convert decimal number to binary number #include <stdio.h>  int main( ) { int a[i], num, i; printf ("Enter the number:");                 scanf( " %d ", &num ); for (i=0; n>0;i++) { a[i]=num%2; num=num/2; } printf ( "\nBinary number for given number:"); for (i=i-1;i>=0;i--) { printf ("%d",a[i]): } return 0; } Output: Enter the number: 5 Binary number for given number : 0101 Try this code by your own! If you have any doubts , then feel free to drop a comment. I'll be happy to answer quest

ARMSTRONG NUMBER PROGRAM IN C

 In this example, you will learn a program to check whether given number is Armstrong  number or not. Armstrong Number: If sum of cubes of digits of a number is equal to the original number itself, is called as Armstrong Number. Example: consider the number 153, Let's calculate the sum of cubes of its all digits. i.e. 1 + 125 + 27 = 153, and this is equal to the original n umber(153) itself. Thus. 153 is Armstrong number . Program to check whet her a given number is Armstrong number or not: //check whether given number is prime or not #include <stdio.h>   int main( ) { int num, r, sum=0, temp=0;  printf ("Enter the number :"); scanf(" %d ", &num ); temp=num; while(num!=0 ) {  r=num%10;                           // to get last digit of number sum=sum + r*r*r     num= num/10;   } if (temp==sum) printf ("%d is Armstrong number",temp); else printf ("%d is not Armstrong number",temp); retu

PRIME NUMBER PROGRAM IN C

In this example , you will learn a program to check whether a given number is prime number or not. Prime number:   A number that is greater than 1 and can be only divided by 1 and itself, is called a prime number. In short, Prime numbers can't be divided by other numbers than itself and 1. Example:  2,3,5,7,11,13...... are the examples of prime numbers. Program to check for prime number: //check whether given number is prime or not #include <stdio.h>   int main( ) { int num, i, n=0, flag=0; printf ("Enter the number :"); scanf(" %d ", &num ); n=num/2; for (i=2;i<=n;i++) { if (num%i==0) { flag=1; break; } if (flag==0) printf ("%d is prime number",num); else printf ("%d is not prime number",num); return 0; } Output: Enter the number:5 5 is prime number Enter the number:34 34 is not prime number Note: > 0 and 1 are not considered as prime n

PALINDROME NUMBER PROGRAM IN C

 In this program you will learn to check whether entered number is palindrome or not. A number is palindrome if and only if the reverse of that number is equal to original number . Example: > 121 is palindrome. > 1001 is palindrome. > 19090 is not palindrome. Program to check palindrome: //check whether entered number is palindrome or not #include <stdio.h>   int main( ) { int num, reverse_num=0, rem, temp;   printf ("Enter the number :");                 scanf(" %d ", &num ); temp=num; while (num!=0) { rem=num%10; reverse_num=reverse_num*10+ rem; num= num/10; } if (temp== reverse_num) { prinf ("%d is palindrome", temp); } else { printf ("%d is not palindrome",temp); } return 0; } Output: Enter the number:121 121 is palindrome. Try this code by your own.  If you have any doubts , then feel free to drop a comment. I&#

FIBONACCI SERIES PROGRAM IN C

In this example , you will learn to print the Fibonacci series upto n terms (entered by user).  The Fibonacci series is sequence in which next term is the sum of previous two terms. The first two terms of the Fibonacci series are 0 and 1. The Fibonacci series of first 10 terms is :                 0     1      1       2      3     5    8     13    21      34 The next number is found by adding previous two numbers :          > The third term , which is 1 , is the sum of first two terms 0 and 1.          > Then 1 +1 =2 , and 2  is the fourth term i.e sum of 2nd and 3rd terms.          > The 3 is found by adding two numbers before it (1+2).          >  Then 5 is 2+3 .          > and so on............ The Fibonacci series program can be written in C in two ways:           1. Without recursion           2. With recursion Fibonacci series program in C without using recursion:  //fibonacci series without recursion #include <stdio.h>   int main( ) {