Practice Program
Solution:through while loop
void main()
{
int a,b=0,n,m;
printf("Enter the number");
scanf("%d",&m);
n = m;
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.
if(b == m) //if we value of b and m are same
{ //then we will print number is palindrome,
printf("Number is palindrome"); //otherwise we will print not palindrome
}
else
printf("Number is Not Palindrome");
}
void main() { int a,b=0,n,m; printf("Enter the number"); scanf("%d",&m); n = m; 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. if(b == m) //if we value of b and m are same { //then we will print number is palindrome, printf("Number is palindrome"); //otherwise we will print not palindrome } else printf("Number is Not Palindrome"); }
Output1
Enter the Number:321
Number is Not Palindrome
Number is Not Palindrome
Output2
Enter the Number:101
Number is Palindrome
Number is Palindrome
Output3
Enter the Number:456
Number is Not Palindrome
Number is Not Palindrome
Solution:from do-while loop
void main()
{
int a,b=0,n,m;
printf("Enter the number");
scanf("%d",&m);
n = m;
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.
if(b == m) //if we value of b and m are same
{ //then we will print number is palindrome,
printf("Number is palindrome"); //otherwise we will print not palindrome
}
else
printf("Number is Not Palindrome");
}
void main() { int a,b=0,n,m; printf("Enter the number"); scanf("%d",&m); n = m; 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. if(b == m) //if we value of b and m are same { //then we will print number is palindrome, printf("Number is palindrome"); //otherwise we will print not palindrome } else printf("Number is Not Palindrome"); }
defining a function 'is_palindrome' which will check a number is palindrome or not.
int is_palindrome(int m) //formal parameters
{
int a,b=0,n;
n = m;//assigning the value of m
//to variable n.
//because further we will need m.
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.
if(m == b)//if value in variable m and b are same
return 1; //then we will return 1,
else // it means the given numbers is palindrome number
return 0;
// we will return 0,if number is not palindrome.
}
int is_palindrome(int m) //formal parameters { int a,b=0,n; n = m;//assigning the value of m //to variable n. //because further we will need m. 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. if(m == b)//if value in variable m and b are same return 1; //then we will return 1, else // it means the given numbers is palindrome number return 0; // we will return 0,if number is not palindrome. }
Using above define function 'is_palindrome':
void main()
{
int n,result;
printf("Enter the Number");
scanf("%d",&n);
//calling function.
result = is_palindrome(n);//actual parameter.
if(result == 1)//if 'is_palindrome()' will return 1,
{ // then number is palindrome,otherwise not
printf("Number is Palindrome");
}
else
printf("Number is Not Palindrome");
}
void main() { int n,result; printf("Enter the Number"); scanf("%d",&n); //calling function. result = is_palindrome(n);//actual parameter. if(result == 1)//if 'is_palindrome()' will return 1, { // then number is palindrome,otherwise not printf("Number is Palindrome"); } else printf("Number is Not Palindrome"); }
Output1
Enter the Number:101
Number is Palindrome
Number is Palindrome
Using above define function 'is_palindrome':
void main()
{
int n,result;
printf("Enter the Number");
scanf("%d",&n);
//calling function.
result = is_palindrome(n);//actual parameter.
if(result == 1)//if 'is_palindrome()' will return 1,
{ // then number is palindrome,otherwise not
printf("Number is Palindrome");
}
else
printf("Number is Not Palindrome");
}
void main() { int n,result; printf("Enter the Number"); scanf("%d",&n); //calling function. result = is_palindrome(n);//actual parameter. if(result == 1)//if 'is_palindrome()' will return 1, { // then number is palindrome,otherwise not printf("Number is Palindrome"); } else printf("Number is Not Palindrome"); }
Output2
Enter the Number:3421
Number is Not Palindrome.
Number is Not Palindrome.
Using above define function 'is_palindrome':
void main()
{
int n,result;
printf("Enter the Number");
scanf("%d",&n);
//calling function.
result = is_palindrome(n);//actual parameter.
if(result == 1)//if 'is_palindrome()' will return 1,
{ // then number is palindrome,otherwise not
printf("Number is Palindrome");
}
else
printf("Number is Not Palindrome");
}
void main() { int n,result; printf("Enter the Number"); scanf("%d",&n); //calling function. result = is_palindrome(n);//actual parameter. if(result == 1)//if 'is_palindrome()' will return 1, { // then number is palindrome,otherwise not printf("Number is Palindrome"); } else printf("Number is Not Palindrome"); }
Output3
Enter the Number:456
Number is Not Palindrome
Number is Not Palindrome
0 Comments
Post a Comment