Write a Python program to accept two integers and check whether they are equal or not

C programming, exercises, solution: Write a C program to accept two integers and check whether they are equal or not.

C# Sharp programming, exercises, solution: Write a C# Sharp program to accept two integers and check whether they are equal or not.

1. Take the two integers as input and store it in the variables m and n respectively. 2. Using if,else statements check if m is equal to n. 3. If they are equal, then print the output as “M and N are equal”. 4. Otherwise print it as “M and N are not equal”.

write a program to input two unequal positive number and check whether they are perfect square or not. if the user in the negative numbers in the program display message 'square root of negative number cannot be determined'.

In this, we’ll write a program that will test if the given number which is greater than 1 is prime or not. A prime number is a positive integer greater than 1 and which has only two factors 1 & the number itself for example number: 2, 3, 5, 7… etc are prime numbers as they have only two factors .i.e. 1 & the number itself.

Write a C program to enter elements in two matrices and check whether both matrices are equal or not. C program to check whether elements of two matrices are equal or not. Logic to check if two matrices are equal or not in C programming.

Take two inputs from user and check whether they are equal or not. print "Enter first number" first = input () print "Enter second number" second = input () print first == second 2.

Input : num1 = 1233, num2 - 1233 Output : Same Input : num1 = 223, num2 = 233 Output : Not Same Recommended: Please try your approach on {IDE} first, before moving on to the solution. Method 1: The idea is to use XOR operator.

You Might Like:

Last update on August 09 2022 03:31:20 (UTC/GMT +8 hours)

Write a Python program to input two integers in a single line.

Sample Solution-1:

Python Code:

print("Input the value of x & y") x, y = map(int, input().split()) print("The value of x & y are: ",x,y)

Sample Output:

Input the value of x & y 2 4 The value of x & y are: 2 4

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Sample Solution-2:

Python Code:

a, b = [int(a) for a in input("Input the value of a & b: ").split()] print("The value of a & b are:",a,b)

Sample Output:

Input the value of a & b: 2 4 The value of a & b are: 2 4

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to calculate the time runs (difference between start and current time)of a program.
Next: Write a Python program to print a variable without spaces between values.

What is the difficulty level of this exercise?

Split a String:

>>> sentence = 'this is, a python, tutorial, about, idioms.' >>> sentence.split(', ') ['this is', 'a python', 'tutorial', 'about', 'idioms.'] >>> sentence.split(', ', 2) ['this is', 'a python', 'tutorial, about, idioms.'] >>> sentence.rsplit(', ') ['this is', 'a python', 'tutorial', 'about', 'idioms.'] >>> sentence.rsplit(', ', 2) ['this is, a python, tutorial', 'about', 'idioms.']

Last update on August 09 2022 03:31:14 (UTC/GMT +8 hours)

Write a C program to accept two integers and check whether they are equal or not.

Pictorial Presentation:

Write a Python program to accept two integers and check whether they are equal or not

Sample Solution:

C Code:

#include <stdio.h> void main() { int int1, int2; printf("Input the values for Number1 and Number2 : "); scanf("%d %d", &int1, &int2); if (int1 == int2) printf("Number1 and Number2 are equal\n"); else printf("Number1 and Number2 are not equal\n"); }

Sample Output:

Input the values for Number1 and Number2 : 15 15 Number1 and Number2 are equal

Flowchart:

Write a Python program to accept two integers and check whether they are equal or not

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: C Conditional Statement Exercises Home
Next: Write a C program to check whether a given number is even or odd.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

How to format strings using printf() to get equal length in the output?

You can specify a width on string fields, e.g.

printf("%-20s", "initialization...");

And then whatever's printed with that field will be blank-padded to the width you indicate.

The - left-justifies your text in that field.

Ref : https://bit.ly/34DMOc3

  • Exercises: Weekly Top 12 Most Popular Topics
  • Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • C Programming Exercises, Practice, Solution : For Loop
  • Python Exercises, Practice, Solution
  • Python Data Type: List - Exercises, Practice, Solution
  • C++ Basic: Exercises, Practice, Solution
  • SQL Exercises, Practice, Solution - exercises on Employee Database
  • SQL Exercises, Practice, Solution - exercises on Movie Database
  • SQL Exercises, Practice, Solution - exercises on Soccer Database
  • C Programming Exercises, Practice, Solution : Recursion

View Discussion

Improve Article

Save Article

Like Article

The following are not allowed to use 

  1. Comparison Operators 
  2. String function

Examples:  

Input : num1 = 1233, num2 – 1233
Output : Same

Input : num1 = 223, num2 = 233
Output : Not Same

Method 1: The idea is to use the XOR operator. XOR of two numbers is 0 if the numbers are the same, otherwise non-zero. 

#include <iostream>

using namespace std;

void areSame(int a, int b)

{

   if (a^b)

       cout << "Not Same";

   else

       cout << "Same";

}

int main()

   areSame(10, 20);

}

class GFG

{

    static void areSame(int a,int b)

    {

        if( (a ^ b) != 0 )

            System.out.println("Not Same");

        else

            System.out.println("Same");

    }

    public static void main(String args[])

    {

        areSame(10,20);

    }

}

def areSame(a, b):

   if (a ^ b):

       print("Not Same")

   else:

       print("Same")

areSame(10, 20)

using System;

class GFG

{

    static void areSame(int a, int b)

    {

        if( (a ^ b) != 0 )

          Console.Write("Not Same");

        else

          Console.Write("Same");

    }

    public static void Main()

    {

        areSame(10, 20);

    }

}

<?php

function areSame($a, $b)

{

    if ($a ^ $b)

        echo "Not Same";

    else

        echo "Same";

}

areSame(10, 20);

?>

<script>

function areSame(a, b)

{

if (a^b)

    document.write("Not Same");

else

    document.write("Same");

}

areSame(10, 20);

</script>

Time Complexity: O(1)
Auxiliary Space: O(1)

Method 2: We can subtract the numbers. The same numbers yield 0. If the answer is not 0, the numbers are not the same. 

#include <bits/stdc++.h>

using namespace std;

void areSame(int a, int b)

{

    if (!(a - b))

        cout << "Same";

    else

        cout << "Not Same";

}

int main()

{   

     areSame(10, 20);   

    return 0;

}

class GFG{

    static void areSame(int a, int b)

    {

        if ((a - b) == 0)

            System.out.println("Same");

        else

            System.out.println("Not Same");

    }

    public static void main(String args[])

    {   

        areSame(10, 20);   

    }

}

def areSame(a, b):

    if (not(a - b)):

        print ("Same")

    else:

        print ("Not Same")

areSame(10, 20)

using System;

class GFG

{

    static void areSame(int a, int b)

    {

        if ((a - b) == 0)

            Console.Write("Same");

        else

            Console.Write("Not Same");

    }

    public static void Main()

    {

        areSame(10, 20);

    }

}

<?php

function areSame($a, $b)

{

    if (!($a - $b))

        echo "Same";

    else

        echo "Not Same";

}

areSame(10, 20);

?>

<script>

function areSame(a , b)

{

    if ((a - b) == 0)

        document.write("Same");

    else

        document.write("Not Same");

    }

areSame(10, 20);

</script>

Time Complexity: O(1)
Auxiliary Space:  O(1)

Method 3: The bitwise AND of one number and the complement of the other number is zero if they are the same, and non-zero if they are not the same.

Below is the implementation of the above approach:

#include <iostream>

using namespace std;

void areSame(int a, int b)

{

    if (a & (~b))

        cout << "Not Same" << endl;

    else

        cout << "Same" << endl;

}

int main()

{

    areSame(10, 20);

}

def areSame(a, b):

    if (a & ~b):

        print ("Not Same")

    else:

        print ("Same")

areSame(10, 10)

areSame(10, 20)

using System;

class GFG

{

  static void areSame(int a, int b)

  {

    if (Convert.ToBoolean(a & (~b)))

      Console.WriteLine("Not Same");

    else

      Console.WriteLine("Same");

  }

  public static void Main(string[] args)

  {

    areSame(10, 20);

  }

}

function areSame(a, b)

{

    if (a & (~b))

        console.log("Not Same");

    else

        console.log("Same");

}

areSame(10, 20);

Time Complexity: O(1)
Auxiliary Space:  O(1)

This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.