Here is a series of a few source codes on Series and Numbers we getting by separating the digits of a number
Input a number, count the numbers of digits present and find the sum of digits.
//Digit separation
#include <stdio.h>
#include <conio.h>
void main()
{
int n, c=0,s=0,m,r;
clrscr();
printf("Enter a Number : ");
scanf("%d",&n);
m=n;
printf("\n Number : %d \n Digits ",m);
do
{
r=n%10; //getting the last digit
s=s+r; //adding the digits
n=n/10;
c++; //count
printf(" %d " ,r);
}while(n>0);
printf(" Sum of the digits = %d Total Nos. Digits %d ",s,c);
getch();
}
Armstrong number. Sum of cube of each digit equal to the number. e.g. 153 = 13 + 53 + 33.
//Armstrong Number
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int r,m,n,s=0;
clrscr();
printf("Enter a no : ");
scanf("%d",&n);
m=n;
while(n>0)
{
r=n%10;
n=n/10;
s=s+pow(r,3);
}
if(s==m)
printf("%d is a Armstrong No",m);
else
printf("%d is not a Armstrong No",m);
getch();
}
Palindrome : Number that reads the same forward and backward. e.g. 121.
//Palindrome
#include <stdio.h>
#include <conio.h>
void main()
{
int n, s=0,m,r;
clrscr();
printf("Enter a Number : ");
scanf("%d",&n);
m=n;
do
{
r=n%10;
s=s*10+r;
n=n/10;
}while(n>0);
if(s==m)
printf("%d is Palindrome",m);
else
printf("%d is not a Palindrome",m);
getch();
}
Automorphic Numbers : After removing the leftmost digit from the square of a number return the number itself. e.g. 25 = 625 or 6 = 36.
//Automorphic Number
#include <stdio.h>
#include <conio.h>
#include <math.h>
int DigiCount(int n)
{
int c=0;
while(n>0)
{
n/=10;
c++;
}
return(c);
}
void main()
{
int n,c,s;
clrscr();
printf("Enter a Number : ");
scanf("%d",&n);
c=DigiCount(n);
s=(n*n)%(int)pow(10,c);
if(s==n)
printf("%d is a Automorphic Number",n);
else
printf(" %d is not a Automorphic Number",n);
getch();
}
Kraprekar No – First count the digits and then separate same numbers of digits from its square and then add it to the left most remainder. The Kaprekar numbers are named after D. R. Kaprekar. e.g. 2972 = square of it = 88209. separate 88 and 209 and add. 88 + 209 = 297. 452 = 2025 = 20 + 25= 45.
//Kaprecker Number
#include <stdio.h>
#include <conio.h>
#include <math.h>
long cal(long x)
{
long num,s,c=0;
num=x*x;
while(x>0)
{
x=x/10;
c++;
}
s=num/(pow(10,c))+num%(int)pow(10,c);
return s;
}
void main()
{
long n,s;
clrscr();
printf("Enter the number : ");
scanf("^d",&n);
s=cal(n);
if(y==n)
printf("The given number is a Kuprecker number ");
else
printf("The given number is not a Kuprecker number ");
getch();
}
Decimal to Binary : Convert decimal numbers to binary.
//Decimal to Binary Number
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int n,m,s=0,c=0;
clrscr();
printf("\nEnter a No : ");
scanf("%d",&n);
m=n;
while(m>0)
{
s=s*10+m%2;
m=m/2;
if(s==0) //incase MSB is Zero
c++;
}
s=s*pow(10,c);
printf("\nBinary Equivalent %d = %d ",n,s);
getch();
}
Binary to Decimal : Converting Binary numbers to decimal
//Binary to Decimal Number
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
long n,m,r,s=0,i=0;
clrscr();
printf("\nEnter a a Binary No : ");
scanf("%ld",&n);
m=n;
while(m>0)
{
r=m%10;
if(r>1 || r<0)
{
printf("\nInvalid input!! ");
exit(0);
getch();
}
s=s+r*pow(2,i);
i++;
m=m/10;
}
printf("\nDecimal Equivallent %ld = %ld ",n,s);
getch();
}
No comments:
Post a Comment