If there are 1 to 100 Numbers in array of 101 elements.
Which is the easy way to find repeated number?
Answers were Sorted based on User's Feedback
Answer / 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 |
Write a short code using c++ to print out all odd number from 1 to 100 using a for loop
write a programme to get a character and thier ASCII value
What is a catch statement?
How do you print for example the integers 3,2,1,5,4 in a binary tree within the console in format where it looks like an actual binary tree?
What are disadvantages of pointers?
What is the difference between the functions rand(), random(), srand() and randomize()?
simple c++ program for "abcde123ba" convert "ab321edcba" with out using string
What is an ABC: an "Abstract Base Class"?
When does a name clash occur?
How delete [] is different from delete?
There is a magic square matrix in such a way that sum of a column or a row are same like 3 5 2 4 3 3 3 2 5 sum of each column and row is 10. you have to check that matrix is magic matrix or not?
Will a C compiler always compile C++ code a) Yes b) No c) Only optimized compilers