- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a function that returns 2 for input 1 and returns 1 for 2 in C programming
A function that returns 2 for input 1 and 1 for input 2 is to be made. This function can be made in many ways based on the logic you use. The easiest way to do this is to use a conditional statement that if the number is 1 then return 2 otherwise return 1 and ways include using mathematical operations (any will do) and XOR operation.
Example
#include <stdio.h> // Method 1 using the if statement int reverseif(int x) { if (x == 1) return 2; else return 1; } // Method 2 using the subtarction form sum of the two numbers (3 in this case) int reversesub(int x){ return (3-x); } int main() { printf("%d
", reverseif(1)); printf("%d
", reversesub(2)); return 0; }
Output
2 1
- Related Articles
- How to write a JavaScript function that returns true if a portion of string 1 can be rearranged to string 2?
- How do I write a function that takes an array of values and returns an object JavaScript?
- Prove that ( frac{a^{-1}}{a^{-1}+b^{-1}}+frac{a^{-1}}{a^{-1}-b^{-1}}=frac{2 b^{2}}{b^{2}-a^{2}} )
- Prove that:( frac{tan ^{2} A}{1+tan ^{2} A}+frac{cot ^{2} A}{1+cot ^{2} A}=1 )
- Simplify and verify for $p=1$ and $q=1$$( iv)$. $frac{-2}{3} pq^{2} times( frac{-3}{2}qp^{2})$
- When MySQL IN() function returns NULL?
- Simplify: $-frac{1}{2}a^{2}b^{2}c+frac{1}{3}ab^{2}c-frac{1}{4}abc^{2}-frac{1}{5}cb^{2}a^{2}+frac{1}{6}cb^{2}a-frac{1}{7}c^{2}ab+frac{1}{8}ca^{2}b$.
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1
- If the equation $(1+m^2)x^2+2mcx+(c^2-a^2)=0$ has equal roots, prove that $c^2=a^2(1+m^2)$.
- Solve for $x$ if $2^{x+1}+2^{x+2}=192$.
- Function that returns the minimum and maximum value of an array in JavaScript
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + … + upto n terms in C++
- Prove that the points $A (2, 3), B (-2, 2), C (-1, -2)$ and $D (3, -1)$ are the vertices of a square $ABCD$.
- Solve for x:$frac{1}{x+1} +frac{2}{x+2} =frac{4}{x+4} ; xneq -1, -2, -4$
- When MySQL MAKE_SET() function returns NULL?

Advertisements