logo
  • Home
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C Program that Takes Input as 2323 and Gives Output as 2332

Posted on August 30, 2013 by staff10

This is a C program to find a greater number of entered number using same digits.

Problem Description

This program takes a number as input and finds a greater number of the entered number using same digits.

Problem Solution

1. Take a number as input.
2. Reverse the number and store it in the array.
3. Using for loop check if the array[i] is greater than array[i+1]. If it is, then interchange them.
4. The number in the array after swapping is the required number.

Program/Source Code

Here is a source code of the C program to find a greater number of entered number using same digits. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program that takes input as 2323 and gives output as 2332. 
  3.  * ie.the new number should be greater than the previous number
  4.  * but should have the same digits
  5.  */
  6. #include <stdio.h>
  7. #include <math.h>
  8.  
  9. int evaluate(int [], int);
  10. int find(int);
  11.  
  12. int main()
  13. {
  14.     int num, result;
  15.  
  16.     printf("Enter a number: ");
  17.     scanf("%d", &num);
  18.     result = find(num);
  19.     if (result)
  20.     {
  21.         printf("The number greater than %d and made of same digits is %d.\n", num, result);
  22.     }
  23.     else
  24.     {
  25.         printf("No higher value possible. Either all numbers are same or the digits of the numbers entered are in decreasing order.\n");
  26.     }
  27.  
  28.     return 0;
  29. }
  30.  
  31. int find(int num)
  32. {
  33.     int digit[20];
  34.     int i = 0, len = 0, n, temp;
  35.  
  36.     n = num;
  37.     while (n != 0)
  38.     {
  39.         digit[i] = n % 10;
  40.         n = n / 10;
  41.         i++;
  42.     }
  43.     len = i;
  44.     for (i = 0; i < len - 1; i++)
  45.     {
  46.         if (digit[i] > digit[i + 1])
  47.         {
  48.             temp = digit[i];
  49.             digit[i] = digit[i + 1];
  50.             digit[i + 1] = temp;
  51.  
  52.             return (evaluate(digit, len));
  53.         }
  54.     }
  55.  
  56.     return 0;
  57. }
  58.  
  59. int evaluate(int digit[], int len)
  60. {
  61.     int i, num = 0;
  62.  
  63.     for (i = 0; i < len; i++)
  64.     {
  65.         num += digit[i] * pow(10, i);
  66.     }
  67.  
  68.     return num;
  69. }
Program Explanation

1. Take a number as input and store it in the variable num.
2. Use the functions evaluate() and find() to find the number.
3. In the function find() reverse the input number and store it in the array digit[]. Use while loop to do this.
4. Using for loop check if digit[i] is greater than digit[i+1]. If it is, then swap their values and call the function evaluate().
5. In this function multiply the array elements with the power of 10 and add all the multiplied values to get the required number.

Runtime Test Cases
Enter a number: 56732   
The number greater than 56732 and made of same digits is 57632.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.
« Prev Page - C Program to Print Nth Node from the last of a Linked List
» Next Page - C Program to Implement Doubly Linked List using Singly Linked List
« C Program to Find Intersection & Union of 2 Linked Lists
C# Program to Check whether the Entered Number is Even or Odd »

Deep Dive @ Sanfoundry:

  1. C# Programming Examples on Mathematics
  2. Python Programming Examples
  3. Java Programming Examples on Data-Structures
  4. C++ Programming Examples on Data-Structures
  5. C Programming Examples using Recursion
  6. C Programming Examples on Strings
  7. C# Programming Examples on Arrays
  8. C Programming Examples on Arrays
  9. Simple C Programs
  10. C# Basic Programming Examples
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
TOS & Privacy
Jobs
Bangalore Training
Online Training
SAN Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry