Practice Program
Solution: through while loop
void main()
{
int a,b=0,n;
printf("Enter the number");
scanf("%d",&n);
while(n!=0)
{ //when we number is modulo with 10
// then give the last digit of that number
a = n%10;
//at every iteration we are assigning the current_value of b
//multiply by 10 and then adding the value of a and
b = (b*10)+a; //assigning to the new value of b.
n = n/10; //after setting up with new value of b then
//we are assigning the current value to n divide by 10 to
} //the the new value of n.
printf("Reverse number is %d",b);
}
void main() { int a,b=0,n; printf("Enter the number"); scanf("%d",&n); while(n!=0) { //when we number is modulo with 10 // then give the last digit of that number a = n%10; //at every iteration we are assigning the current_value of b //multiply by 10 and then adding the value of a and b = (b*10)+a; //assigning to the new value of b. n = n/10; //after setting up with new value of b then //we are assigning the current value to n divide by 10 to } //the the new value of n. printf("Reverse number is %d",b); }
Output1
Enter the Number:321
Reverse number is 123.
Reverse number is 123.
Output2
Enter the Number:3421
Reverse number is 1243.
Reverse number is 1243.
Output3
Enter the Number:456
Reverse number is 654.
Reverse number is 654.
Solution:from do-while loop
void main()
{
int a,b=0,n;
printf("Enter the number");
scanf("%d",&n);
do
{ //when we number is modulo with 10
// then give the last digit of that number
a = n%10;
//at every iteration we are assigning the current_value of b
//multiply by 10 and then adding the value of a and
b = (b*10)+a; //assigning to the new value of b.
n = n/10; //after setting up with new value of b then
//we are assigning the current value to n divide by 10 to
}while(n!=0); //the the new value of n.
printf("Reverse number is %d",b);
}
void main() { int a,b=0,n; printf("Enter the number"); scanf("%d",&n); do { //when we number is modulo with 10 // then give the last digit of that number a = n%10; //at every iteration we are assigning the current_value of b //multiply by 10 and then adding the value of a and b = (b*10)+a; //assigning to the new value of b. n = n/10; //after setting up with new value of b then //we are assigning the current value to n divide by 10 to }while(n!=0); //the the new value of n. printf("Reverse number is %d",b); }
defining a function 'reverse_number' which will return the reverse number.
int reverse_number(int n)
{
int a,b=0;
while(n!=0)
{ //when we number is modulo with 10
// then give the last digit of that number
a = n%10;
//at every iteration we are assigning the current_value of b
//multiply by 10 and then adding the value of a and
b = (b*10)+a; //assigning to the new value of b.
n = n/10; //after setting up with new value of b then
//we are assigning the current value to n divide by 10 to
} // the new value of n.
return b;
}
int reverse_number(int n) { int a,b=0; while(n!=0) { //when we number is modulo with 10 // then give the last digit of that number a = n%10; //at every iteration we are assigning the current_value of b //multiply by 10 and then adding the value of a and b = (b*10)+a; //assigning to the new value of b. n = n/10; //after setting up with new value of b then //we are assigning the current value to n divide by 10 to } // the new value of n. return b; }
Using above define function 'reverse_number':
void main()
{
int n,result;
printf("Enter the Number: ");
scanf("%d",&n);
//calling function
result = reverse_number(n);
printf("Rverse number is %d",result);
}
void main() { int n,result; printf("Enter the Number: "); scanf("%d",&n); //calling function result = reverse_number(n); printf("Rverse number is %d",result); }
Output1
Enter the Number:321
Reverse number is 123.
Reverse number is 123.
Using above define function 'reverse_number':
void main()
{
int n,result;
printf("Enter the Number: ");
scanf("%d",&n);
//calling function
result = reverse_number(n);
printf("Sum of digits is %d",result);
}
void main() { int n,result; printf("Enter the Number: "); scanf("%d",&n); //calling function result = reverse_number(n); printf("Sum of digits is %d",result); }
Output2
Enter the Number:3421
Reverse number is 1243.
Reverse number is 1243.
Using above define function 'reverse_number':
void main()
{
int n,result;
printf("Enter the Number: ");
scanf("%d",&n);
//calling function
result = reverse_number(n);
printf("Sum of digits is %d",result);
}
void main() { int n,result; printf("Enter the Number: "); scanf("%d",&n); //calling function result = reverse_number(n); printf("Sum of digits is %d",result); }
Output3
Enter the Number:456
Reverse number is 654.
Reverse number is 654.
0 Comments
Post a Comment