Read a 2 Digit Int From a File C

Write a program to opposite digits of a number

Write a program to reverse the digits of an integer.

Examples :

            Input :            num = 12345            Output:            54321            Input :            num = 876            Output:            678

Flowchart:

ITERATIVE Mode

Algorithm:

Input:  num (1) Initialize rev_num = 0 (two) Loop while num > 0      (a) Multiply rev_num by ten and add remainder of num             divide past 10 to rev_num                rev_num = rev_num*x + num%10;      (b) Split up num by 10 (3) Return rev_num

Example:

num = 4562
rev_num = 0
rev_num = rev_num *ten + num%x = 2
num = num/10 = 456
rev_num = rev_num *x + num%10 = xx + 6 = 26
num = num/10 = 45
rev_num = rev_num *10 + num%10 = 260 + v = 265
num = num/10 = 4
rev_num = rev_num *x + num%ten = 2650 + four = 2654
num = num/10 = 0

Programme:

C++

#include <bits/stdc++.h>

using namespace std;

int reverseDigits( int num)

{

int rev_num = 0;

while (num > 0) {

rev_num = rev_num * 10 + num % 10;

num = num / 10;

}

return rev_num;

}

int master()

{

int num = 4562;

cout << "Reverse of no. is " << reverseDigits(num);

getchar ();

return 0;

}

C

#include <stdio.h>

int reverseDigits( int num)

{

int rev_num = 0;

while (num > 0) {

rev_num = rev_num * x + num % 10;

num = num / 10;

}

return rev_num;

}

int main()

{

int num = 4562;

printf ( "Reverse of no. is %d" , reverseDigits(num));

getchar ();

return 0;

}

Java

class GFG {

static int reverseDigits( int num)

{

int rev_num = 0 ;

while (num > 0 ) {

rev_num = rev_num * 10 + num % x ;

num = num / 10 ;

}

return rev_num;

}

public static void main(Cord[] args)

{

int num = 4562 ;

Arrangement.out.println( "Reverse of no. is "

+ reverseDigits(num));

}

}

Python

n = 4562

rev = 0

while (n > 0 ):

a = n % ten

rev = rev * 10 + a

n = northward / / 10

print (rev)

C#

using System;

class GFG {

static int reverseDigits( int num)

{

int rev_num = 0;

while (num > 0) {

rev_num = rev_num * 10 + num % ten;

num = num / ten;

}

return rev_num;

}

public static void Main()

{

int num = 4562;

Console.Write( "Reverse of no. is "

+ reverseDigits(num));

}

}

PHP

<?php

role reverseDigits( $num )

{

$rev_num = 0;

while ( $num > one)

{

$rev_num = $rev_num * 10 +

$num % x;

$num = (int) $num / 10;

}

return $rev_num ;

}

$num = 4562;

echo "Contrary of no. is " ,

reverseDigits( $num );

?>

Javascript

<script>

permit num = 4562;

office reverseDigits(num) {

allow rev_num = 0;

while (num > 0)

{

rev_num = rev_num * ten + num % 10;

num = Math.floor(num / ten);

}

return rev_num;

}

document.write(reverseDigits(num));

</script>

Output

Contrary of no. is 2654

Time Complication: O(log(n)), where n is the input number.

Auxiliary Space: O(1)

RECURSIVE WAY

C++

#include <bits/stdc++.h>

using namespace std;

int reverseDigits( int num)

{

static int rev_num = 0;

static int base_pos = 1;

if (num > 0) {

reverseDigits(num / ten);

rev_num += (num % 10) * base_pos;

base_pos *= 10;

}

return rev_num;

}

int principal()

{

int num = 4562;

cout << "Reverse of no. is " << reverseDigits(num);

return 0;

}

C

#include <stdio.h>;

int reversDigits( int num)

{

static int rev_num = 0;

static int base_pos = ane;

if (num > 0) {

reversDigits(num / 10);

rev_num += (num % 10) * base_pos;

base_pos *= 10;

}

return rev_num;

}

int main()

{

int num = 4562;

printf ( "Reverse of no. is %d" , reversDigits(num));

getchar ();

return 0;

}

Java

grade GFG {

static int rev_num = 0 ;

static int base_pos = 1 ;

static int reversDigits( int num)

{

if (num > 0 ) {

reversDigits(num / x );

rev_num += (num % 10 ) * base_pos;

base_pos *= 10 ;

}

return rev_num;

}

public static void main(Cord[] args)

{

int num = 4562 ;

Organisation.out.println(reversDigits(num));

}

}

Python3

rev_num = 0

base_pos = one

def reversDigits(num):

global rev_num

global base_pos

if (num > 0 ):

reversDigits(( int )(num / 10 ))

rev_num + = (num % x ) * base_pos

base_pos * = x

return rev_num

num = 4562

print ( "Reverse of no. is " ,

reversDigits(num))

C#

using System;

class GFG {

static int rev_num = 0;

static int base_pos = 1;

static int reversDigits( int num)

{

if (num > 0) {

reversDigits(num / 10);

rev_num += (num % 10) * base_pos;

base_pos *= 10;

}

render rev_num;

}

public static void Main()

{

int num = 4562;

Console.WriteLine(reversDigits(num));

}

}

PHP

<?php

$rev_num = 0;

$base_pos = 1;

office reversDigits( $num )

{

global $rev_num ;

global $base_pos ;

if ( $num > 0)

{

reversDigits((int)( $num / x));

$rev_num += ( $num % ten) *

$base_pos ;

$base_pos *= 10;

}

return $rev_num ;

}

$num = 4562;

echo "Reverse of no. is " ,

reversDigits( $num );

?>

Javascript

<script>

var rev_num = 0;

var base_pos = ane;

function reversDigits(num)

{

if (num > 0)

{

reversDigits(Math.flooring(num/10));

rev_num += (num%10)*base_pos;

base_pos *= ten;

}

render rev_num;

}

let num = 4562;

certificate.write( "Reverse of no. is "

+ reversDigits(num));

</script>

Output

Reverse of no. is 2654

Time Complexity: O(log(north)) where northward is the input number.

Using String in java

Nosotros volition catechumen the number to a string using StringBuffer later this, we will reverse that string using the reverse() method

corner instance

Input: 32100

And then for the higher up input if nosotros try to solve this by reversing the string, and so the output will exist 00123.

So to deal with this state of affairs nosotros again demand to convert the string to integer and then that our output will be 123

C++

#include <bits/stdc++.h>

using namespace std;

int reverseDigits( int num)

{

cord strin = to_string(num);

opposite(strin.begin(), strin.stop());

num = stoi(strin);

render num;

}

int main()

{

int num = 4562;

cout << "Reverse of no. is " << reverseDigits(num);

return 0;

}

Coffee

public course GFG {

static int reversDigits( int num)

{

StringBuffer string

= new StringBuffer(String.valueOf(num));

cord.reverse();

num = Integer.parseInt(String.valueOf(string));

return num;

}

public static void chief(Cord[] args)

{

int num = 4562 ;

Organisation.out.println( "Reverse of no. is "

+ reversDigits(num));

}

}

Python3

def reversDigits(num):

string = str (num)

string = listing (string)

cord.reverse()

cord = ''.join(string)

num = int (string)

return num

if __name__ = = "__main__" :

num = 4562

print ( "Reverse of no. is " , reversDigits(num))

C#

using Organization;

public grade GFG{

public static cord ReverseString( cord s)

{

char [] array = s.ToCharArray();

Array.Reverse(array);

return new string (array);

}

static int reversDigits( int num)

{

string strin = num.ToString();

strin = ReverseString(strin);

num = int .Parse(strin);

render num;

}

static public void Main ()

{

int num = 4562;

Console.Write( "Contrary of no. is "

+ reversDigits(num));

}

}

Javascript

<script>

function reversDigits(num)

{

permit str

= num.toString().split( "" ).reverse().join( "" );

num = parseInt(str);

return str;

}

let num = 4562;

document.write( "Opposite of no. is "

+ reversDigits(num));

</script>

Output

Opposite of no. is 2654

Time Complexity: O(logxnorthward)

Auxiliary Infinite: O(1)

Opposite digits of an integer with overflow handled

Note that the above program doesn't consider leading zeroes. For case, for 100 programs volition print ane. If you want to print 001 then encounter this comment from Maheshwar.

Try extensions of above functions that should also piece of work for floating-point numbers.


ferrellovelinterst.blogspot.com

Source: https://www.geeksforgeeks.org/write-a-program-to-reverse-digits-of-a-number/

0 Response to "Read a 2 Digit Int From a File C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel