Write a program to Print First n Palindrome Numbers using For loop in C
Write a program to Print First n Palindrome Numbers using For loop in C++
Write a program to Print First n Palindrome Numbers using For loop in Python
Write a program to Print First n Palindrome Numbers using For loop in PHP
Write a program to Print First n Palindrome Numbers using For loop in Java
Write a program to Print First n Palindrome Numbers using For loop in Java Script
Write a program to Print First n Palindrome Numbers using For loop in C#
Explanation:
Steps
Define a palindrome check function:
A number is a palindrome if it reads the same forwards and backwards.
Reverse the number and compare it with the original.
Iterate through numbers:
Start from 1 and check each number to see if it is a palindrome.
Use a counter to keep track of how many palindrome numbers have been printed.
Stop when n palindromes have been found.
Output: Print the palindrome numbers.
Program to Print First n Palindrome Numbers using For loop
C
C++
Python
PHP
JAVA
Java Script
C#
#include <stdio.h>
int is_palindrome(int num) {
int original = num, reversed = 0, remainder;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
return original == reversed; // Return true if palindrome
}
int main() {
int n, count = 0, num = 1;
printf("Enter the number of palindrome numbers to print: ");
scanf("%d", &n);
printf("First %d palindrome numbers are:\n", n);
while (count < n) {
if (is_palindrome(num)) {
printf("%d ", num);
count++;
}
num++;
}
return 0;
}
#include <iostream>
using namespace std;
bool is_palindrome(int num) {
int original = num, reversed = 0, remainder;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
return original == reversed; // Return true if palindrome
}
int main() {
int n, count = 0, num = 1;
cout << "Enter the number of palindrome numbers to print: ";
cin >> n;
cout << "First " << n << " palindrome numbers are:" << endl;
while (count < n) {
if (is_palindrome(num)) {
cout << num << " ";
count++;
}
num++;
}
return 0;
}
def is_palindrome(num):
original = num
reversed_num = 0
while num != 0:
remainder = num % 10
reversed_num = reversed_num * 10 + remainder
num //= 10
return original == reversed_num # Return True if palindrome
n = int(input("Enter the number of palindrome numbers to print: "))
count = 0
num = 1
print(f"First {n} palindrome numbers are:")
while count < n:
if is_palindrome(num):
print(num, end=" ")
count += 1
num += 1
<?php
function is_palindrome($num) {
$original = $num;
$reversed = 0;
while ($num != 0) {
$remainder = $num % 10;
$reversed = $reversed * 10 + $remainder;
$num = (int)($num / 10);
}
return $original == $reversed; // Return true if palindrome
}
$n = (int)readline("Enter the number of palindrome numbers to print: ");
$count = 0;
$num = 1;
echo "First $n palindrome numbers are:\n";
while ($count < $n) {
if (is_palindrome($num)) {
echo "$num ";
$count++;
}
$num++;
}
?>
import java.util.Scanner;
public class PalindromeNumbers {
public static boolean isPalindrome(int num) {
int original = num, reversed = 0, remainder;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
return original == reversed; // Return true if palindrome
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of palindrome numbers to print: ");
int n = scanner.nextInt();
int count = 0, num = 1;
System.out.println("First " + n + " palindrome numbers are:");
while (count < n) {
if (isPalindrome(num)) {
System.out.print(num + " ");
count++;
}
num++;
}
scanner.close();
}
}
function isPalindrome(num) {
let original = num;
let reversed = 0;
while (num !== 0) {
let remainder = num % 10;
reversed = reversed * 10 + remainder;
num = Math.floor(num / 10);
}
return original === reversed; // Return true if palindrome
}
let n = parseInt(prompt("Enter the number of palindrome numbers to print: "));
let count = 0;
let num = 1;
console.log(`First ${n} palindrome numbers are:`);
while (count < n) {
if (isPalindrome(num)) {
console.log(num);
count++;
}
num++;
}
using System;
class Program {
static bool IsPalindrome(int num) {
int original = num, reversed = 0, remainder;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
return original == reversed; // Return true if palindrome
}
static void Main() {
Console.Write("Enter the number of palindrome numbers to print: ");
int n = int.Parse(Console.ReadLine());
int count = 0, num = 1;
Console.WriteLine($"First {n} palindrome numbers are:");
while (count < n) {
if (IsPalindrome(num)) {
Console.Write(num + " ");
count++;
}
num++;
}
}
}