Practice Program
Solution: through while loop
void main()
{
int a,n,count=0;
printf("Enter the Number: ");
scanf("%d",&n);
while(n!=0)
{
a = n%10;//as modulo operator gives
count++; //last digit of number
//when we modulo number with 10.
n = n/10;
//when we divide a number with 10
//then it removes the last digit from
//the number
}
printf("There are %d digit.",count);
}
void main() { int a,n,count=0; printf("Enter the Number: "); scanf("%d",&n); while(n!=0) { a = n%10;//as modulo operator gives count++; //last digit of number //when we modulo number with 10. n = n/10; //when we divide a number with 10 //then it removes the last digit from //the number } printf("There are %d digit.",count); }
Output1
Enter the Number:321
There are 2 digit.
There are 2 digit.
Output2
Enter the Number:3421
There are 4 digit.
There are 4 digit.
Output3
Enter the Number:3214561
There are 7 digit.
There are 7 digit.
Solution: from do-while loop
void main()
{
int a,n,count=0;
printf("Enter the Power: ");
scanf("%d",&n);
do
{
a = n%10;//as modulo operator gives
count++; //last digit of number
//when we modulo number with 10.
n = n/10;
//when we divide a number with 10
//then it removes the last digit from
//the number
} while(n!=0);
printf("There are %d digit.",count);
}
void main() { int a,n,count=0; printf("Enter the Power: "); scanf("%d",&n); do { a = n%10;//as modulo operator gives count++; //last digit of number //when we modulo number with 10. n = n/10; //when we divide a number with 10 //then it removes the last digit from //the number } while(n!=0); printf("There are %d digit.",count); }
defining a function 'count_digit' which will return number of digit.
int count_digit(int n)
{
int a,count = 0;
while(n!=0)
{
a = n%10;//as modulo operator gives
count++; //last digit of number
//when we modulo number with 10.
n = n/10;
//when we divide a number with 10
//then it removes the last digit from
//the number
}
return count;
}
int count_digit(int n) { int a,count = 0; while(n!=0) { a = n%10;//as modulo operator gives count++; //last digit of number //when we modulo number with 10. n = n/10; //when we divide a number with 10 //then it removes the last digit from //the number } return count; }
Using above define function 'count_digit':
void main()
{
int n,count;
printf("Enter the Power: ");
scanf("%d",&n);
count = count_digit(n);
printf("There are %d digit.",count);
}
void main() { int n,count; printf("Enter the Power: "); scanf("%d",&n); count = count_digit(n); printf("There are %d digit.",count); }
Output1
Enter the Number:321
There are 2 digit.
There are 2 digit.
Using above define function 'count_digit':
void main()
{
int n,count;
printf("Enter the Power: ");
scanf("%d",&n);
count = count_digit(n);
printf("There are %d digit.",count);
}
void main() { int n,count; printf("Enter the Power: "); scanf("%d",&n); count = count_digit(n); printf("There are %d digit.",count); }
Output2
Enter the Number:3421
There are 4 digit.
There are 4 digit.
Using above define function 'count_digit':
void main()
{
int n,count;
printf("Enter the Power: ");
scanf("%d",&n);
count = count_digit(n);
printf("There are %d digit.",count);
}
void main() { int n,count; printf("Enter the Power: "); scanf("%d",&n); count = count_digit(n); printf("There are %d digit.",count); }
Output3
Enter the Number:3214561
There are 7 digit.
There are 7 digit.
0 Comments
Post a Comment