Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views2 pages

Program 2

The document contains two C programs: the first program finds the largest of three numbers using a ternary operator, while the second program swaps two numbers without using temporary variables, employing the bitwise XOR method. Both programs include user input and output statements to display results. Additional methods for swapping using arithmetic and bitwise operators are also commented in the second program.

Uploaded by

gtshadow2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Program 2

The document contains two C programs: the first program finds the largest of three numbers using a ternary operator, while the second program swaps two numbers without using temporary variables, employing the bitwise XOR method. Both programs include user input and output statements to display results. Additional methods for swapping using arithmetic and bitwise operators are also commented in the second program.

Uploaded by

gtshadow2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// Program 2.

A: Design and develop C program to find the bigger number among three
numbers using ternary operator.

#include <stdio.h>

int main()
{
int number1, number2, number3, largest;

// Input three numbers


printf("Enter any three numbers:\n");
scanf("%d%d%d", &number1, &number2, &number3);

// Use ternary operator to find the largest number


largest = (number1 > number2) ? ((number1 > number3) ? number1 : number3)
: ((number2 > number3) ? number2 : number3);

// Output the largest number


printf("The largest number is: %d\n", largest);

return 0;
}

// Program 2.B: Develop a C program to find the swap to numbers without using
temporary variables.

/* This can be done in two ways one way is by using arithmetic operators(+,/,*,/)
and bitwise XOR(^) operator */

#include <stdio.h>

int main()
{
int number_1, number_2;

// Input two numbers


printf("Enter two numbers:\n");

printf("Number 1: ");
scanf("%d", &number_1);

printf("Number 2: ");
scanf("%d", &number_2);

// Swapping using bitwise XOR


number_1 = number_1 ^ number_2; // Step 1: number_1 becomes the XOR of
number_1 and number_2
number_2 = number_1 ^ number_2; // Step 2: number_2 becomes the original value
of number_1
number_1 = number_1 ^ number_2; // Step 3: number_1 becomes the original value
of number_1

/* using + and - operators


number_1 = number_1 + number_2;
number_2 = number_1 - number_2;
number_1 = number_1 - number_2;

using * and / operators


number_1 = number_1 * number_2;
number_2 = number_1 / number_2;
number_1 = number_1 / number_2;
*/

// Output the swapped values


printf("After swapping:\n");
printf("Number 1: %d\n", number_1);
printf("Number 2: %d\n", number_2);

return 0;
}

You might also like