If there are 1 to 100 Numbers in array of 101 elements.
Which is the easy way to find repeated number?
Answer Posted / vadivelt
1.Get i/p of 101 elements and add all the nos.And say the
result of that Addition is 'sum'.
2.We all know that n(n+1)/2 is formula to calculate the
addition of 'n' numbers.
if n = 100 then n(n+1)/2 give addtion of 1....100.
so say total = n(n+1)/2;
3.Now 'sum' holds the addition of 1....101 nos
And 'total' holds the addition of 1....100 nos
So the repeated no would be.,
Result = sum - total;
Equalent Code is.,
#include<stdio.h>
main()
{
int i, n, sum = 0, a[150], Total;
printf("ENTER SIZE OF ARRAY:\n");
scanf("%d", &n);
printf("\nENTER ELEMENTS OF ARRAY:\n");
for(i = 0; i<n; i++)
{
scanf("%d", &a[i]);
sum = sum + a[i];
}
n = n-1;
Total = (n *(n+1) /2);
printf("\nREPEATED NO: %d",sum - Total);
getch();
}
| Is This Answer Correct ? | 38 Yes | 6 No |
Post New Answer View All Answers
What is c++ virtual inheritance?
How a new operator differs from the operator new?
What is difference between c++ 11 and c++ 14?
What happens when the extern "c" char func (char*,waste) executes?
What does it mean to declare a destructor as static?
What is purpose of abstract class?
Write about all the implicit member functions of a class?
Explain the differences between list x; & list x();.
What is the difference between an array and a list?
What are manipulators used for?
Which programming language's unsatisfactory performance led to the discovery of c++?
what is oops and list its features in c++?
How do you master coding?
What does the following code do: int c=0; cout< a) Undefined *Updated* b) 01 c) 00
Write a program to encrypt the data in a way that inputs a four digit number and replace each digit by (the sum of that digit plus 7) modulus 10. Then sweep the first digit with the third, second digit with the fourth and print the encrypted number.