|
INTERVIEW QUESTIONS
PROGRAMMING LANGUAGES
C
DETAILS
Question: How to write a 'C' program to read an array of 10 numbers and print the prime nums?
this is what i wrote could someone tell me where is my mistake? can i use here flag at all?
#include <stdio.h>
void main () { int arr[10], i, j, flag;
for (i=0;i<10;i++)
scanf ("%d", &arr[i]);
for (i=0;i<10;i++) { flag=1;
for (j=2;j<=(arr[i]/2); j++) { if (arr[i]%j==0) { flag=0; break;
} } }
for (i=0;i<10;i++) { if (flag=1)
printf ("%d", arr[i]); }
Answer: Hi, U r using the same flag for all the 10 numbers and after coming out of the two loops u r printing the numbers based on the value of the flag but the flag will contain the value thats been updated by the last number in the array..thats absolutely wrong..here is the correct program...
#include <stdio.h>
void main () { int arr[10], i, j, flag;
for (i=0;i<10;i++)
scanf ("%d", &arr[i]);
for (i=0;i<10;i++) { flag=1;
for (j=2;j<=(arr[i]/2); j++) { if (arr[i]%j==0) { flag=0; break;
} } if (flag==1)
printf ("%d", arr[i]); }
|
|
|
Category |
C Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.2) By 9436 users |
Added on |
4/8/2015 |
Views |
68595 |
Rate it! |
|
|
Question:
How to write a 'C' program to read an array of 10 numbers and print the prime nums?
this is what i wrote could someone tell me where is my mistake? can i use here flag at all?
#include <stdio.h>
void main () { int arr[10], i, j, flag;
for (i=0;i<10;i++)
scanf ("%d", &arr[i]);
for (i=0;i<10;i++) { flag=1;
for (j=2;j<=(arr[i]/2); j++) { if (arr[i]%j==0) { flag=0; break;
} } }
for (i=0;i<10;i++) { if (flag=1)
printf ("%d", arr[i]); }
Answer:
Hi, U r using the same flag for all the 10 numbers and after coming out of the two loops u r printing the numbers based on the value of the flag but the flag will contain the value thats been updated by the last number in the array..thats absolutely wrong..here is the correct program...
#include <stdio.h>
void main () { int arr[10], i, j, flag;
for (i=0;i<10;i++)
scanf ("%d", &arr[i]);
for (i=0;i<10;i++) { flag=1;
for (j=2;j<=(arr[i]/2); j++) { if (arr[i]%j==0) { flag=0; break;
} } if (flag==1)
printf ("%d", arr[i]); }
Source: CoolInterview.com
Answered by: Karthik | Date: 6/7/2010
| Contact Karthik
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
Which language is not high-level language?
|
View Answer
|
|
What is data independence? what is transaction process application? Discuss the main charecteristics of data approach how it differs from traditional file system!
|
View Answer
|
|
How to find size of an datatype without using sizeof operator?
|
View Answer
|
|
How to find GCD of four numbers?
|
View Answer
|
|
Write a program to accept any number up to six digit and print in words. for ex:1234=one two three four
|
View Answer
|
|
What is type integration? And what is integer type integration?
|
View Answer
|
|
What is :- Token , identifier , block , parameter , argument , scope of variable , Macro ?
|
View Answer
|
|
What is the code to find our age,for example if my date of birth is 4/12/1986 and today date is 16/11/2008 answer should give our year,month,no of days
|
View Answer
|
|
What is the difference between unix and linux.
|
View Answer
|
|
How to convert decimal to octal and hexadecimal?
|
View Answer
|
|
If we want that any wildcard characters in the command line argument should be approxiemately expanded , are we required to make any special provision?if yes, which?
|
View Answer
|
|
How would you obtain the current time and difference between two times?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All C Interview Questions & Answers - Exam Mode /
Learning Mode
|