Code 1:
Count the number of digits in c programming language
#include<stdio.h>
int main(){
int num,count=0;
printf("Enter a number: ");
scanf("%d",&num);
while(num){
num=num/10;
count++;
}
printf("Total digits is: %d",count);
return 0;
}
Sample output:
Enter a number: 23
Total digits is: 2
Code 2:
C code to count the total number of digit using for loop
#include<stdio.h>
int main(){
int num,count=0;
printf("Enter a number: ");
scanf("%d",&num);
for(;num!=0;num=num/10)
count++;
printf("Total digits is: %d",count);
return 0;
}
Sample output:
Enter a number: 456
Total digits is: 3
Code 3:
Count the digits of a given number in c language using recursion
#include<stdio.h>
int countDigits(num);
int main(){
int num,count;
printf("Enter a number: ");
scanf("%d",&num);
count = countDigits(num);
printf("Total digits is: %d",count);
return 0;
}
int countDigits(int num){
static int count=0;
if(num!=0){
count++;
countDigits(num/10);
}
return count;
}
Sample output:
Enter a number: 1234567
Total digits is: 7
Extract digits from integer in c language
#include<stdio.h>
int main(){
int num,temp,factor=1;
printf("Enter a number: ");
scanf("%d",&num);
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;
}
Sample output:
Enter a number: 123
Each digits of given number are: 1 2 3
Simple program of c find the largest number
#include<stdio.h>
int main(){
int n,num,i;
int big;
printf("Enter the values of n: ");
scanf("%d",&n);
printf("Number %d",1);
scanf("%d",&big);
for(i=2;i<=n;i++){
printf("Number %d: ",i);
scanf("%d",&num);
if(big<num)
big=num;
}
printf("Largest number is: %d",big);
return 0;
}
Sample Output:
Enter the values of n:
Number 1: 12
Number 2: 32
Number 3: 35
Largest number is: 35
#include<stdio.h>
int main(){
int num = 1;
print(num);
return 0;
}
int print(num){
if(num<=100){
printf("%d ",num);
print(num+1);
}
}
Output:
Sample output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Alogrithm:
**
How to convert string to int without using library functions in c programming language
#include<stdio.h>
int stringToInt(char[] );
int main(){
char str[10];
int intValue;
printf("Enter any integer as a string: ");
scanf("%s",str);
intValue = stringToInt(str);
printf("Equivalent integer value: %d",intValue);
return 0;
}
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;
}
Sample output:
Enter any integer as a string: 123
Equivalent integer value: 123
Write a c program to find out NCR factor of given number Or
C program to find the ncr value by using recursive function
Algorithm:
In the mathematics nCr has defined as
C program to find the ncr value by using recursive function
#include<stdio.h>
int main(){
int n,r,ncr;
printf("Enter any two numbers->");
scanf("%d %d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("The NCR factor of %d and %d is %d",n,r,ncr);
return 0;
}
int fact(int n){
int i=1;
while(n!=0){
i=i*n;
n--;
}
return i;
}
Algorithm:
In the mathematics nCr has defined as
nCr = n! /((n-r)!r!)
Prime factor of a number in c
#include<stdio.h>
int main(){
int num,i=1,j,k;
printf("\nEnter a number:");
scanf("%d",&num);
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++;
}
return 0;
}
FIND OUT GENERIC ROOT OF A NUMBER By C PROGRAM
C program for generic root
#include<stdio.h>
int main(){
long int num,sum,r;
printf("\nEnter a number:-");
scanf("%ld",&num);
while(num>10){
sum=0;
while(num){
r=num%10;
num=num/10;
sum+=r;
}
if(sum>10)
num=sum;
else
break;
}
printf("\nSum of the digits in single digit is: %ld",sum);
return 0;
}
C code for calculation of generic root in one line
#include <stdio.h>
int main(){
int num,x;
printf("Enter any number: ");
scanf("%d",&num);
printf("Generic root: %d",(x=num%9)?x:9);
return 0;
}
Sample output:
Enter any number: 731
Generic root: 2
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
Write a c program to find largest among three numbers using conditional operator
#include<stdio.h>
int main(){
int a,b,c,big;
printf("\nEnter 3 numbers:");
scanf("%d %d %d",&a,&b,&c);
big=(a>b&&a>c?a:b>c?b:c);
printf("\nThe biggest number is: %d",big);
return 0;
}
If you have any suggestions on above c program to find largest or biggest of 3 numbers, please share us.
Write a c program to find largest among three numbers using binary minus operator
#include<stdio.h>
int main(){
int a,b,c;
printf("\nEnter 3 numbers: ");
scanf("%d %d %d",&a,&b,&c);
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);
return 0;
}
Alogrithm:
**
Write a c program to subtract two numbers without using subtraction operator
Write a c program or code to subtract two numbers without using subtraction operator
#include<stdio.h>
int main(){
int a,b;
int sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
sum = a + ~b + 1;
printf("Difference of two integers: %d",sum);
return 0;
}
Sample Output:
Enter any two integers: 5 4
Difference of two integers: 1
Add two numbers in c without using operator
How to add two numbers without using the plus operator in c
#include<stdio.h>
int main(){
int a,b;
int sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
//sum = a - (-b);
sum = a - ~b -1;
printf("Sum of two integers: %d",sum);
return 0;
}
Sample output:
Enter any two integers: 5 10
Sum of two integers: 15
Algorithm:
In c ~ is 1's complement operator. This is equivalent to:
~a = -b + 1
So, a - ~b -1
= a-(-b + 1) + 1
= a + b – 1 + 1
= a + b
How to calculate power of a number in c
How to write power in c
#include<stdio.h>
int main(){
int pow,num,i=1;
long int sum=1;
printf("\nEnter a number: ");
scanf("%d",&num);
printf("\nEnter power: ");
scanf("%d",&pow);
while(i<=pow){
sum=sum*num;
i++;
}
printf("\n%d to the power %d is: %ld",num,pow,sum);
return 0;
}
Code 1:
1. C program to add digits of a number
2. C program for sum of digits of a number
3. C program to calculate sum of digits
#include<stdio.h>
int main(){
int num,sum=0,r;
printf("Enter a number: ");
scanf("%d",&num);
while(num){
r=num%10;
num=num/10;
sum=sum+r;
}
printf("Sum of digits of number: %d",sum);
return 0;
}
Sample output:
Enter a number: 123
Sum of digits of number: 6
Code 2:
1. Sum of digits of a number in c using for loop
#include<stdio.h>
int main(){
int num,sum=0,r;
printf("Enter a number: ");
scanf("%d",&num);
for(;num!=0;num=num/10){
r=num%10;
sum=sum+r;
}
printf("Sum of digits of number: %d",sum);
return 0;
}
Sample output:
Enter a number: 567
Sum of digits of number: 18
Code 3:
1. Sum of digits in c using recursion
#include<stdio.h>
int getSum(int);
int main(){
int num,sum;
printf("Enter a number: ");
scanf("%d",&num);
sum = getSum(num);
printf("Sum of digits of number: %d",sum);
return 0;
}
int getSum(int num){
static int sum =0,r;
if(num!=0){
r=num%10;
sum=sum+r;
getSum(num/10);
}
return sum;
}
Sample output:
Enter a number: 45
Sum of digits of number: 9
1. Write a c program to reverse a given number
2. C program to find reverse of a number
3. C program to reverse the digits of a number
4. Reverse of a number in c using while loop
#include<stdio.h>
int main(){
int num,r,reverse=0;
printf("Enter any number: ");
scanf("%d",&num);
while(num){
r=num%10;
reverse=reverse*10+r;
num=num/10;
}
printf("Reversed of number: %d",reverse);
return 0;
}
Sample output:
Enter any number: 12
Reversed of number: 21
Code 2:
1. Reverse very large or big numbers beyond the range of long int
2. Reverse five digit number c program
Logic is we accept the number as string
#include<stdio.h>
#define MAX 1000
int main(){
char num[MAX];
int i=0,j,flag=0;
printf("Enter any positive integer: ");
scanf("%s",num);
while(num[i]){
if(num[i] < 48 || num[i] > 57){
printf("Invalid integer number");
return 0;
}
i++;
}
printf("Reverse: ");
for(j=i-1;j>=0;j--)
if(flag==0 && num[j] ==48){
}
else{
printf("%c",num[j]);
flag =1;
}
return 0;
Sample output:
Enter any positive integer: 234561000045645679001237800000000000
Reverse: 8732100976546540000165432
Code 3:
1. C program to reverse a number using for loop
2. How to find reverse of a number in c
3. Wap to reverse a number in c
#include<stdio.h>
int main(){
int num,r,reverse=0;
printf("Enter any number: ");
scanf("%d",&num);
for(;num!=0;num=num/10){
r=num%10;
reverse=reverse*10+r;
}
printf("Reversed of number: %d",reverse);
return 0;
}
Sample output:
Enter any number: 123
Reversed of number: 321
Code 4:
1. C program to reverse a number using recursion
#include<stdio.h>
int main(){
int num,reverse;
printf("Enter any number: ");
scanf("%d",&num);
reverse=rev(num);
printf("Reverse of number: %d",reverse);
return 0;
}
int rev(int num){
static sum,r;
if(num){
r=num%10;
sum=sum*10+r;
rev(num/10);
}
else
return 0;
return sum;
}
Sample output:
Enter any number: 456
Reverse of number: 654