Skip to main content

Featured

C++ Program to Perform Linear Search on a Vector

  C++ Program to Perform Linear Search on a Vector Introduction In this C++ program, we will learn how to perform a Linear Search on a vector. The program first takes the size of the vector and its elements as input. Then it asks the user for the element to search. If the element is found, it displays the index where it is located. Otherwise, it displays a message indicating that the element is not found. C++ Program #include<bits/stdc++.h> using namespace std; int main() { int num, search, found = 0; cout << "Enter the size of the vector:" << endl; cin >> num; vector<int> v(num); cout << "Enter " << num << " elements in vector:" << endl; for(int i = 0; i < num; i++) { cin >> v[i]; } cout << "Enter element that you want to search:" << endl; cin >> search; for(int i = 0; i < num; i++) { ...

C Program to Reverse a Number Using While Loop

✅ C Program to Reverse a Number Using While Loop

#include <stdio.h>

int main() {
    int num, reversed = 0, remainder;

    printf("Enter a number: ");
    scanf("%d", &num);

    while(num != 0) {
        remainder = num % 10;          
        reversed = reversed * 10 + remainder; 
        num = num / 10;                
    }

    printf("Reversed number = %d", reversed);

    return 0;
}
  

๐Ÿ“˜ Explanation:

This program reverses a given number using a while loop. It extracts digits one by one from the end and rebuilds the number in reverse order.

  • Take user input using scanf().
  • Use num % 10 to extract the last digit.
  • Multiply reversed number by 10 and add the extracted digit.
  • Remove last digit using num = num / 10.
  • Repeat until the number becomes 0.

This program is commonly asked in beginner coding interviews and programming exams.

๐Ÿงพ Sample Output:

Enter a number: 1234
Reversed number = 4321
  

๐Ÿ”‘ Keywords:

C program to reverse a number, reverse number in C using while loop, C programming examples, beginner C programs, number manipulation in C, C logic building program

๐Ÿ“Œ Hashtags:

#CProgramming #ReverseNumber #LearnC #CodingForBeginners #WhileLoop #1printf

๐Ÿ” Search Description:

Learn how to reverse a number in C using while loop. Beginner-friendly program with step-by-step explanation and sample output.

Comments

Popular Posts

๐ŸŒ™