Ca some one please help me with aC code to allow user enter
numbers from 1 to 20 without repeating and prnt the sum of
those numbers
thnx
Answer Posted / sanjay bhosale
/ Duplicate.cpp : main project file.
// Program : To find duplicate no in array by traversing it only once.
#include "stdafx.h"
#include<stdio.h>
#include<malloc.h>
using namespace System;
int main(array<System::String ^> ^args)
{
int *arr;
int val = 0;
int size = 0;
printf(" \n Enter the no of elements in array :");
scanf_s("%d",&size);
arr = (int *)malloc(sizeof(int)*size);
printf("\n Enter elements in the range 1 to %d:",size);
for(int i=0;i<size;i++)
{
scanf_s("%d",&val);
if(val<1 && val>=size)
{
printf("\n enter element again :\t");
i--;
}
else
*(arr+i) = val;
}
int sum = 0;
getchar();
printf("Array is :\n");
for(int i=0;i<size;i++)
{
printf("\t %d",arr[i]);
sum+= arr[i];
}
getchar();
printf("\n sum : %d",sum);
int diff = sum-(size*(size-1)/2.0);
printf(" Duplicate element is :%d\n",diff);
getchar();
return 0;
}
Might this help you........
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
What does %d do in c?
The file stdio.h, what does it contain?
What is the difference between printf and scanf )?
what is the different bitween abap and abap-hr?
What are compound statements?
What is a static function in c?
What does %p mean c?
Why do we use static in c?
What is quick sort in c?
How can I sort a linked list?
What are the differences between new and malloc in C?
How can I implement sets or arrays of bits?
c language supports bitwise operations, why a) 'c' language is system oriented b) 'c' language is problem oriented c) 'c' language is middle level language d) all the above
What is the equivalent code of the following statement in WHILE LOOP format?
If a variable is a pointer to a structure, then which operator is used to access data members of the structure through the pointer variable?