4. Write a c program to add two numbers without using addition operator.
10. Write a c program to find out NCR factor of given number.
11. How to convert string to int without using library functions in c
12. Program in c to print 1 to 100 without using loop
13. Split number into digits in c programming
10. Write a c program to find out NCR factor of given number.
11. How to convert string to int without using library functions in c
12. Program in c to print 1 to 100 without using loop
13. Split number into digits in c programming
Perfect number is a positive number which sum of all positive divisors excluding that number is equal to that number. For example 6 is perfect number since divisor of 6 are 1, 2 and 3. Sum of its divisor is
ReplyDelete1 + 2+ 3 =6
Note: 6 is the smallest perfect number.
Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28
Some more perfect numbers: 496, 8128
C program to check perfect number:
void Perfect(int n)
{int i=0,sum=0;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
}
Perfect Numbers Between a range---
int n,i,sum;
int min,max;
for(n=min;n<=max;n++){
i=1;
sum = 0;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d ",n);
}
Sum of Digits:
ReplyDelete--------------
Iterative:
while(num){
r=num%10;
num=num/10;
sum=sum+r;
}
for(;num!=0;num=num/10){
r=num%10;
sum=sum+r;
}
Recursive:
int getSum(int num){
static int sum =0,r;
if(num!=0){
r=num%10;
sum=sum+r;
getSum(num/10);
}
return sum;
}
Power of a number:
------------------
sum=1;
while(i < =pow){
sum=sum*num;
i++;
}
Add two numbers:
----------------
sum = a - ~b -1;
Note:a - ~b -1
= a-(-b + 1) + 1
= a + b – 1 + 1
= a + b
Subtract 2 numbers:
-------------------
sum = a + ~b + 1;
Largest among three:
--------------------
Using minus--------
if(a-b > 0 && a-c > 0)
printf("\nGreatest is a :%d",a);
else
if(b-c > 0)
printf("\nGreatest is b :%d",b);
else
printf("\nGreatest is c :%d",c);
Using Conditional-------
big=(a > b&&a > c?a:b > c?b:c);
Generic Root:
-------------
Meaning of generic root:
It sum of digits of a number unit we don't get a single digit. For example:
Generic root of 456: 4 + 5 + 6 = 15 since 15 is two digit numbers so 1 + 5 = 6
So, generic root of 456 = 6
Solution 1-----
while(num > 10){
sum=0;
while(num){
r=num%10;
num=num/10;
sum+=r;
}
if(sum > 10)
num=sum;
else
break;
}
Solution 2-----
printf("Generic root: %d",(x=num%9)?x:9);
Prime factors of a number:
---------------------------
while(i <= num){
k=0;
if(num%i==0){
j=1;
while(j < =i){
if(i%j==0)
k++;
j++;
}
if(k==2)
printf("\n%d is a prime factor",i);
}
i++;
}
ncr
--------
ncr=fact(n)/(fact(r)*fact(n-r));
int fact(int n){
int i=1;
while(n!=0){
i=i*n;
n--;
}
return i;
}
String to INT:
----------------
int stringToInt(char str[]){
int i=0,sum=0;
while(str[i]!='\0'){
if(str[i] < 48 || str[i] > 57){
printf("Unable to convert it into integer.\n");
return 0;
}
else{
sum = sum*10 + (str[i] - 48);
i++;
}
}
return sum;
}
Print 1 to 100 without using loop:
---------------------------------
int print(num){
if(num <= 100){
printf("%d ",num);
print(num+1);
}
}
Extract Digits:
---------------
int num,temp,factor=1;
temp=num;
while(temp){
temp=temp/10;
factor = factor*10;
}
printf("Each digits of given number are: ");
while(factor > 1){
factor = factor/10;
printf("%d ",num/factor);
num = num % factor;
}
return 0;
}
Armstrong Number
ReplyDelete----------------
Those numbers which sum of the cube of its digits is equal to that number are known as Armstrong numbers. For example 153 since 1^3 + 5^3 + 3^3 = 1+ 125 + 9 =153
Other Armstrong numbers: 370,371,407 etc.
while(temp!=0){
r=temp%10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);
}
Prime Number:
-------------
for(i=2;i < =num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
Strong Number:
---------------
A number is called strong number if sum of the factorial of its digit is equal to number itself. For example: 145 since
1! + 4! + 5! = 1 + 24 + 120 = 145
int num,i,f,r,sum=0,temp;
temp=num;
while(num){
i=1,f=1;
r=num%10;
while(i < =r){
f=f*i;
i++;
}
sum=sum+f;
num=num/10;
}
if(sum==temp)
printf("%d is a strong number",temp);
else
printf("%d is not a strong number",temp);
Palindrome Number or NOT:
-------------------------
int checkPalindrome(int num){
static int sum=0,r;
if(num!=0){
r=num%10;
sum=sum*10+r;
checkPalindrome(num/10);
}
return sum;
}
Armstrong Number
ReplyDelete----------------
Those numbers which sum of the cube of its digits is equal to that number are known as Armstrong numbers. For example 153 since 1^3 + 5^3 + 3^3 = 1+ 125 + 9 =153
Other Armstrong numbers: 370,371,407 etc.
while(temp!=0){
r=temp%10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);
}
Prime Number:
-------------
for(i=2;i < =num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
Strong Number:
---------------
A number is called strong number if sum of the factorial of its digit is equal to number itself. For example: 145 since
1! + 4! + 5! = 1 + 24 + 120 = 145
int num,i,f,r,sum=0,temp;
temp=num;
while(num){
i=1,f=1;
r=num%10;
while(i < =r){
f=f*i;
i++;
}
sum=sum+f;
num=num/10;
}
if(sum==temp)
printf("%d is a strong number",temp);
else
printf("%d is not a strong number",temp);
Palindrome Number or NOT:
-------------------------
int checkPalindrome(int num){
static int sum=0,r;
if(num!=0){
r=num%10;
sum=sum*10+r;
checkPalindrome(num/10);
}
return sum;
}