mercredi 6 février 2019

Palindrome Number Program in C

I'm trying to make a C program that determines whether a five-digit integer is a palindrome or not. The part I'm struggling with is not using a loop, as I know how to do it with a loop. However, I'm supposed to do it without one, and instead use nested if else statements of some sorts. I've figured out how to reverse a 3 digit number but not 5, and then going on from there. Here is what I have so far. I'm just learning so it's a bit harder as well.

#include "pch.h"
#include <iostream>
using namespace stdl
int main()
{ int a, b, c, d, e , num, reversed;
cout << "\n Enter a five digit number";
cin >> num;
a=num /100;
b=(num % 100)/10;
c = num % 10;
d = num % 10;
e = num % 10;
reversed = 100 * c + 10 * b + a;
cout << " " << reversed;
return 0;
}

Again, I can't get it to work for 5 digits but I assume after that using if else statements I can relate reversed and the original number with % to see if it's a palindrome.

Aucun commentaire:

Enregistrer un commentaire