C language tricky good pointers questions answers and explanation operators data types arrays structures questions functions recursion preprocessors, looping, file handling, strings questions switch case if else printf advance c linux objective types mcq faq interview questions and answers with explanation and solution for freshers or beginners. Placement online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c c++ tutorials and pdf

lets work out this with example now suppose for n=1 1<1 does not satisfy for n=2 1<2 satisfies 2%1==0 satisfies sum=0+1=1 i++(so i is 2 now) 2<2 does not satisfy for n=3 while 2<3 satisfies 3%2==0 does not satisfy for n=4 while 2<4 satisfies 2%4==0 satisfies sum=1+2=3 i=3(i++ i.e i=2+1=3) for n=5 while 3<5 satisfies 5%3==0 does not satisfy for n=6 while 3<6 satisfies 6%3==0 satisfies sum=3+3=6 So here Sum==n satisfies so 6 is a perfect number... Rocked it everyone can easily understand now

/* reverse of the value in using the program*/ #include #include void main() { int a[15][15],n,i,j; printf("Entr the number of the n=>"); scanf("%d",&n); printf("Enter the number you want to insert=>"); for(i=0;i0;i--) { for(j=n;j>0;j--) { printf("%d",a[i][j]); } getch(); } } ____________________________________________________________________________________________________________________ output _______ inter the value of n=2 value= 42,43,44,46; output is=46,44,43,42 is reverse order;

In code2 when i declared i,sum values at the starting it is not showing the results do u know the reason or there is any rule for for loop to give values in that loop

You can reduce the number of iterations by initializing 'sum with 1 and i with 2'. And why to check 'n%i == 0' upto 'i < n'. Check only upto 'i <= n/2'.

Excuse me, 6 is perfect number because 1+2+3 = 1*2*3, BUT with number 28 when i calculate : 1+2+4+7+14 = 28 and 1*2*4*7*14 = 784 <=== the sum of left doesn't equal with the multiply of right like 6=1+2+3=1*2*3 ???? I need to find all numbers include >=2 digits which have the sum = the multiplication, i wonder is it perfect number ? and how to find it ? thanks !!

#include main() { int q[10]={0},i,front=-1,rear=-1,max=10,n,item; printf("\n\tMENU\n1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.EXIT\n"); do { printf("\nEnter your choice\n"); scanf("%d",&n); switch(n) { case 1: if(rear=0) { printf("The deleted item =%d",q[front]); if(front==rear) { front=-1; rear=-1; } else front++; } else printf("Underflow\n"); break;

case 3: if((front==-1)&&(rear==-1)) printf("The queue is empty\n"); else { printf("The elements of the queue are :"); for(i=front;i<=rear;i++) printf("%d\t",q[i]); } break;

Deleting a Node from the Linked List (assuming values in Data are unique)

Function Delete(Value)

Pointer : Found 'Pointer for finding required Node to Delete

Found = NULL 'Assuming required Node not found

IF there are no Nodes in the List then WRITE error message that can't delete a Node

ELSE Go to memory location pointed by Start pointer 'Begin at the Start (first Node) Start Loop and run until Next points to NULL 'Check each Node until Next pointer points to NULL

IF value in Data of current Node is equal to Value then 'If we find our required Node Found = current Node 'Mark the current Node with Found pointer

Exit Loop 'No need to finish the loop

ENDIF

Go to Node pointed by Next pointer of current Node

End Loop

IF the required Node is the first Node in the List then Point the Start pointer to NULL 'Node deleted

ELSE

Point Next of previous Node to the Node pointed by Next of Found 'Node deleted (Previous.Next = Found.Next) ENDIF ENDIF End Function

binary search Begin ACCEPT N Elements READ Element X to search

IF (range contains only one element): Look for desired value IF found WRITE Element X found

Else:

1. Get midpoint of range(N) 2. Determine which half of range contains the value 3. Search that half of the range using binary search Look for desired value IF found WRITE Element X found ENDIF ENDIF End

/*proram to calculate the pay of a worker*/ #include #include float pays(int hours,float rate); int main() { int hours,itrvar,num; float rate; float pay=0,totalpay=0;

printf("enter the number of workers"); scanf("%d",&num);

for(itrvar=0;itrvar0)/*condition to check hourly rate should be greater than 0*/ { pay=pays(hours,rate);/*call of pays function*/ printf("Pay at %f rupees/hr for %d is %f\n",rate,hours,pay); totalpay=totalpay+pay; }/*end of if*/ else { printf("Total pay is=%f",totalpay); exit(0); }/*end of else*/ }/*end of loop*/

printf("Total pay is=%f",totalpay); }/*end of main()*/ float pays(int hours,float rate) { float pay; if(hours<40)/*check for number of hours worked for calculating pay*/ pay=hours*rate; else if(hours>=40&&hours<60) pay=hours*1.5*rate; else if(hours>=60) pay=hours*2.0*rate; return pay; }/*end of pays()*/

/*proram to calculate the pay of a worker*/ #include #include float pays(int hours,float rate); int main() { int hours,itrvar,num; float rate; float pay=0,totalpay=0;

printf("enter the number of workers"); scanf("%d",&num);

for(itrvar=0;itrvar0)/*condition to check hourly rate should be greater than 0*/ { pay=pays(hours,rate);/*call of pays function*/ printf("Pay at %f rupees/hr for %d is %f\n",rate,hours,pay); totalpay=totalpay+pay; }/*end of if*/ else { printf("Total pay is=%f",totalpay); exit(0); }/*end of else*/ }/*end of loop*/

printf("Total pay is=%f",totalpay); }/*end of main()*/ float pays(int hours,float rate) { float pay; if(hours<40)/*check for number of hours worked for calculating pay*/ pay=hours*rate; else if(hours>=40&&hours<60) pay=hours*1.5*rate; else if(hours>=60) pay=hours*2.0*rate; return pay; }/*end of pays()*/

int SearchByName(char* Name, char* Value, int n) { int loop;

for(loop=0; loop4) printf("\n Illegal choice!!");

else {

switch(Choice) { case 1: printf("\n Enter name to search: "); /* Searching by name */ scanf("%s",Value); temp1= SearchByName(Name, Value, Num); if(temp1!=-1) printf("\n Name: %s\n ID: %d\n Marks: %d\n\n", Name[temp1], ID[temp1], Marks[temp1]); else printf("\n Name not found"); break;

case 2: printf("\n Enter ID to search: "); /* Searching by ID */ scanf("%d", &temp2); temp1= Search(ID, temp2, Num); if(temp1!=-1) printf("\n Name: %s\n ID: %d\n Marks: %d\n\n", Name[temp1], ID[temp1], Marks[temp1]); else printf("\n ID not found"); break;

case 3: printf("\n Students record with rank\n\n"); /* Printind data rankwise */ for(loop=0; loop<Num; loop++) printf("\n Rank: %d\n Name: %s\n ID: %d\n Marks: %d\n\n", loop+1, Name[loop], ID[loop], Marks[loop]); break;

case 4: printf("\n Enter ID to search: "); /* Searching name by ID */ scanf("%d", &temp2); temp1= Search(ID, temp2, Num); if(temp1!=-1) printf("\n Name: %s\n\n",Name[temp1]); else printf("\n ID not found"); break; } }

I tried code 2 using for loop instead of while loop, but it is not working. What could be the possible reason. I replaced i=1; sum = 0;

while(i<n){ if(n%i==0) sum=sum+i; i++; } by for(i=1;i<n;++i) { if(n%i==0) sum+=i; } The compiler shows no errors but blank space after the line "The perfect numbers are:". Please reply.

thank u for ur program.

ReplyDeleteThis site is really good I visit this site eneryday.Good questions.The best part is it provides explanation to every question.Best site.

ReplyDeleteThanks alot

this site is undoubtedly a super duper excellent site to let anyone quench his thirst for c programming logics and concepts! hats offff!

ReplyDeleteIT IS BETTER TO PUT OUTPUT OF PARTICULAR PROGRAM....

ReplyDeletethis is a great site had much information for beginners to experienced .....

ReplyDeletesuper,best site for all new learners

ReplyDeletelets work out this with example now suppose

ReplyDeletefor n=1

1<1 does not satisfy

for n=2

1<2 satisfies

2%1==0 satisfies

sum=0+1=1

i++(so i is 2 now)

2<2 does not satisfy

for n=3

while 2<3 satisfies

3%2==0 does not satisfy

for n=4

while 2<4 satisfies

2%4==0 satisfies

sum=1+2=3

i=3(i++ i.e i=2+1=3)

for n=5

while 3<5 satisfies

5%3==0 does not satisfy

for n=6

while 3<6 satisfies

6%3==0 satisfies

sum=3+3=6

So here Sum==n satisfies so 6 is a perfect number...

Rocked it everyone can easily understand now

Plz tell me how to use this site???????

DeleteNice one raj

DeleteIt would be better to initialize Sum with 1 and i with 2.

ReplyDeletebrilliant

ReplyDeletethanks

ReplyDeleteluv dis site...\m/\m/\m/

ReplyDeletenice blog for studentsthx agian

ReplyDeletegive the answer for question --------------10. Write a c program to add two numbers without using addition operator.???

ReplyDeleteThank you very much. This tutorial have helped me complete my university Assignments and Lab reports in time.

ReplyDeleteThank u.....

ReplyDeleteBest site for a computer engineering student like me. Thanks a lot.....

ReplyDeleteParveen Arora

really i am very much thankful to u...its very much helpful to me..thanks a lot.

ReplyDeletesir should we have to take the sum of values of i for the perfect no...

ReplyDeleteThis site is really good.Good questions.The best part is it provides explanation to every question. site had much information for beginners

ReplyDeletenice concept used

ReplyDelete/* reverse of the value in using the program*/

ReplyDelete#include

#include

void main()

{

int a[15][15],n,i,j;

printf("Entr the number of the n=>");

scanf("%d",&n);

printf("Enter the number you want to insert=>");

for(i=0;i0;i--)

{

for(j=n;j>0;j--)

{

printf("%d",a[i][j]);

}

getch();

}

}

____________________________________________________________________________________________________________________

output

_______

inter the value of n=2

value= 42,43,44,46;

output is=46,44,43,42 is reverse order;

these are the question in c asked by the interviewer's so friends utilize this site.

ReplyDeletethis is a great job

nice

ReplyDeleteIn code 2,how about is "time complexity" ?

ReplyDeleteThanks.

No time complexity

DeleteIn code2 when i declared i,sum values at the starting it is not showing the results do u know the reason or there is any rule for for loop to give values in that loop

ReplyDeletei wish allah will make yr dreams true

ReplyDeleteCode 1.

ReplyDeleteYou can reduce the number of iterations by initializing 'sum with 1 and i with 2'.

And why to check 'n%i == 0' upto 'i < n'. Check only upto 'i <= n/2'.

Excuse me, 6 is perfect number because 1+2+3 = 1*2*3, BUT with number 28 when i calculate : 1+2+4+7+14 = 28 and 1*2*4*7*14 = 784 <=== the sum of left doesn't equal with the multiply of right like 6=1+2+3=1*2*3 ????

ReplyDeleteI need to find all numbers include >=2 digits which have the sum = the multiplication, i wonder is it perfect number ? and how to find it ?

thanks !!

This site is realy very good

ReplyDeletei love your website thank you for your help

ReplyDeletethanks a lot. :)

ReplyDeleteREAD POL_TYPE,term,amount;

ReplyDeleteIF(pol_type is endownment)

Printf(“2. Money back”);

Printf(“3. Term assurance”)

If (term>=25){

Bonus=(amount/1000)*60;

WRITE BONUS

ENDIF

If (term>=20 && term<25)

Bonus=(amount/1000)*55;

WRITE BONUS

ENDIF

If (term>=10 && term<20)

Bonus=(amount/1000)*50;

WRITE BONUS

ENDIF

If (term<10){

Bonus=(amount/1000)*45;

WRITE BONUS

ENDIF

ENDIF

IF(pol_type is Money back)

If (term>=25)

Bonus=(amount/1000)*55;

WRITE BONUS

ENDIF

If (term>=20 && term<25)

Bonus=(amount/1000)*50;

WRITE BONUS

ENDIF

If (term>=10 && term<25)

Bonus=(amount/1000)*45;

WRITE BONUS

ENDIF

If (term<10){

Bonus=(amount/1000)*45;

IF(pol_type is Term assurance)

WRITE ““bonus not applicable”)

END

#include

ReplyDeletemain()

{

int q[10]={0},i,front=-1,rear=-1,max=10,n,item;

printf("\n\tMENU\n1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.EXIT\n");

do

{

printf("\nEnter your choice\n");

scanf("%d",&n);

switch(n)

{

case 1:

if(rear=0)

{

printf("The deleted item =%d",q[front]);

if(front==rear)

{

front=-1;

rear=-1;

}

else

front++;

}

else

printf("Underflow\n");

break;

case 3:

if((front==-1)&&(rear==-1))

printf("The queue is empty\n");

else

{

printf("The elements of the queue are :");

for(i=front;i<=rear;i++)

printf("%d\t",q[i]);

}

break;

case 4:

break;

default:

printf("Invalid choice\n");

break;

}

}

while(n!=4);

}

#include

ReplyDelete#include

int main(void)

{

int salesperson[10][2];

int index,totalsales=0;

float minsales,incentiveamount,averagesales=0;

for(index=0;index<10;index++)

{

printf("enter the salesperson id and his sales: \n");

scanf("%d%d",&salesperson[index][0],&salesperson[index][1]);

totalsales+=salesperson[index][1];

}

averagesales=(float)totalsales/(float)10;

minsales=(averagesales*2)/(float)3;

printf("\n Salesperson ID \t Incentive Amount \n");

printf("\n_____________________________________\n");

for(index=0;index<10;index++)

{

if(salesperson[index][1]>minsales)

{

incentiveamount=(float)(salesperson[index][1]-minsales)*(float)0.2;

printf("%d \t \t\t %f \n", salesperson[index][0],incentiveamount);

}

}

getch();

return 0;

}

pseudocode for Queue

ReplyDeleteBegin

Accept the N elements

INSERT Operation(insertion in stack)(to back)

Repeat for N-1

Insert the first element to Queue[0]

Insert the second element to Queue[1]

"

"

"

Insert the last element to Queue[N-1] "

REMOVE Operation(deletion from stack)(from front)

Repeat for N-1

REMOVE the first element from Queue[N-1]

REMOVE the second element from Queue[N-2]

"

"

"

REMOVE the last element from Queue[0] "

End

pseudocode for Queue(lab3_3.4)

ReplyDeleteBegin

Accept the N elements

INSERT Operation(insertion in stack)(to back)

Repeat for N-1

Insert the first element to Queue[0]

Insert the second element to Queue[1]

"

"

"

Insert the last element to Queue[N-1] "

REMOVE Operation(deletion from Queue)(from front)

Repeat for N-1

REMOVE the first element from Queue[N-1]

REMOVE the second element from Queue[N-2]

"

"

"

REMOVE the last element from Queue[0] "

DISPLAY Operation(Display Queue Elements

Repeat for N-1

WRITE the first element from Queue[0]

WRITE the second element from Queue[1]

"

"

"

WRITE the last element from Queue[N-1] "

No. of Element in queue

Repeat for NULL(iteration until the element stored in a queue reaches to null value)

Count++

END of Loop

WRITE Count

End

stack

ReplyDeletepseudocode for Stack

Begin

Accept the N elements

PUSH Operation(insertion in stack)

Repeat for N-1

PUSH the first element to Stack[0]

PUSH the second element to Stack[1]

"

"

"

PUSH the last element to Stack[N-1] "

POP Operation(deletion from stack)

Repeat for N-1 to 0

POP the last element from Stack[N-1]

POP the second_last element from Stack[N-2]

"

"

"

POP the first element from Stack[0] "

End

linked list

ReplyDeletepseudocode for LINKED LIST

Deleting a Node from the Linked List (assuming values in Data are unique)

Function Delete(Value)

Pointer : Found 'Pointer for finding required Node to Delete

Found = NULL 'Assuming required Node not found

IF there are no Nodes in the List then

WRITE error message that can't delete a Node

ELSE

Go to memory location pointed by Start pointer 'Begin at the Start (first Node)

Start Loop and run until Next points to NULL 'Check each Node until Next pointer points to NULL

IF value in Data of current Node is equal to Value then 'If we find our required Node

Found = current Node 'Mark the current Node with Found pointer

Exit Loop 'No need to finish the loop

ENDIF

Go to Node pointed by Next pointer of current Node

End Loop

IF the required Node is the first Node in the List then

Point the Start pointer to NULL 'Node deleted

ELSE

Point Next of previous Node to the Node pointed by Next of Found 'Node deleted (Previous.Next = Found.Next)

ENDIF

ENDIF

End Function

linear search

ReplyDeletepseudocode for sequential search

Begin

Accept the N elements

READ Element X to search

count=0

Label1 Repeat for index=0 to N-1

IF(Element[index]=X)

WRITE Element X found

ELSE

count++

Goto Label1:

ENDIF

END LOOP

IF(count>1)

WRITE Element X not found

ENDIF

End

binary search

ReplyDeleteBegin

ACCEPT N Elements

READ Element X to search

IF (range contains only one element):

Look for desired value

IF found

WRITE Element X found

Else:

1. Get midpoint of range(N)

2. Determine which half of range contains

the value

3. Search that half of the range using binary

search

Look for desired value

IF found

WRITE Element X found

ENDIF

ENDIF

End

INSERTION SORT

ReplyDeleteREAD numberofelements, array [];

FOR i=0 to numberofelements-1

J=i

WHILE j>0 and array[j]<array[j-1]

SWAP array[j] and array[j-1]

j—

WRITE array []

#include

ReplyDeleteint main()

{

int a[10],i,n,m,c=0,l,u,mid;

printf("Enter the size of an array: ");

scanf("%d",&n);

printf("Enter the elements in ascending order: ");

for(i=0;i<n;i++){

scanf("%d",&a[i]);

}

printf("Enter the number to be search: ");

scanf("%d",&m);

l=0,u=n-1;

while(l<=u)

{

mid=(l+u)/2;

if(m==a[mid])

{

c=1;

break; }

else if(m<a[mid])

{

u=mid-1; }

else

l=mid+1;

}

if(c==0)

printf("The number is not found.");

else

printf("The number is found.");

getch ();

return 0;

}

/*proram to calculate the pay of a worker*/

ReplyDelete#include

#include

float pays(int hours,float rate);

int main()

{

int hours,itrvar,num;

float rate;

float pay=0,totalpay=0;

printf("enter the number of workers");

scanf("%d",&num);

for(itrvar=0;itrvar0)/*condition to check hourly rate should be greater than 0*/

{

pay=pays(hours,rate);/*call of pays function*/

printf("Pay at %f rupees/hr for %d is %f\n",rate,hours,pay);

totalpay=totalpay+pay;

}/*end of if*/

else

{

printf("Total pay is=%f",totalpay);

exit(0);

}/*end of else*/

}/*end of loop*/

printf("Total pay is=%f",totalpay);

}/*end of main()*/

float pays(int hours,float rate)

{

float pay;

if(hours<40)/*check for number of hours worked for calculating pay*/

pay=hours*rate;

else if(hours>=40&&hours<60)

pay=hours*1.5*rate;

else if(hours>=60)

pay=hours*2.0*rate;

return pay;

}/*end of pays()*/

/*proram to calculate the pay of a worker*/

Delete#include

#include

float pays(int hours,float rate);

int main()

{

int hours,itrvar,num;

float rate;

float pay=0,totalpay=0;

printf("enter the number of workers");

scanf("%d",&num);

for(itrvar=0;itrvar0)/*condition to check hourly rate should be greater than 0*/

{

pay=pays(hours,rate);/*call of pays function*/

printf("Pay at %f rupees/hr for %d is %f\n",rate,hours,pay);

totalpay=totalpay+pay;

}/*end of if*/

else

{

printf("Total pay is=%f",totalpay);

exit(0);

}/*end of else*/

}/*end of loop*/

printf("Total pay is=%f",totalpay);

}/*end of main()*/

float pays(int hours,float rate)

{

float pay;

if(hours<40)/*check for number of hours worked for calculating pay*/

pay=hours*rate;

else if(hours>=40&&hours<60)

pay=hours*1.5*rate;

else if(hours>=60)

pay=hours*2.0*rate;

return pay;

}/*end of pays()*/

file pointer

ReplyDelete#include

#include

#include

int main()

{

FILE *fp1, *fp2;

char file1[10], file2[10], data[100];

printf("\n Enter name of source file: ");

scanf("%s",&file1);

printf("\n Enter name of destination file: ");

scanf("%s",&file2);

if((fp1 = fopen(file1,"r"))==NULL)

{

printf("\n %s not found\n\n", file1);

getch();

exit(0);

}

fscanf(fp1, "%s", data);

if((fp2 = fopen(file2, "w"))==NULL)

{

printf("\n %s not found\n\n", file2);

getch();

exit(0);

}

fprintf(fp2, "%s", data);

getch();

return 0;

}

#include

ReplyDelete#include

int main()

{

FILE *p,*q;

char b[10];

char a;

printf("enter the file name.txt");

scanf("%s",&b);

p=fopen(b,"r");

if (p==NULL)

{

printf("\n cannot locate the file");

exit ();

}

q=fopen("muddesir.txt","w");

if(q==NULL)

{

printf("\n cannot locate the file");

fclose(q);

exit ();

}

while(1)

{

a=fgetc(p);

if(a==EOF)

break;

else

fputc(a,q);

}

fclose(p);

fclose(q);

getch ();

}

#include

ReplyDelete#include

int Factorial(int Num)

{

int loop;

for(loop=Num; loop>0; loop--)

Num*=loop;

return Num;

}

int main()

{

int n, r, Choice=0;

float Result=0.0f;

printf("\n Enter n and r values: ");

scanf("%d%d",&n, &r);

printf("\n Enter:\n\n 1 for nCr\n\n 2 for nPr\n\n Your choice: ");

scanf("%d", &Choice);

if(Choice == 1)

{

printf("\n nCr = ");

Result = (Factorial(n)/(Factorial(r) * (Factorial(n-r))));

}

else if(Choice == 2)

{

printf("\n nPr = ");

Result = Factorial(n)/Factorial(n-r);

}

else

{

printf("\n Illegal choice!");

exit(0);

}

printf("%.2f",Result);

getch();

return 0;

}

Module for AcceptValues:

ReplyDelete1. FOR loop1=1 -> n

1.1 FOR loop2=1 -> 7

1.1.1 READ Data[loop1][loop2]

2. State[25] = {all names of states}

3. Sex[2] = {‘M’,’F’}

Module for Census:

1. FOR loop1=1 -> n

1.1 IF Data[4][loop1] = State[index1]

1.2 Print State[index1]

1.3 FOR loop2= 1 -> n

1.3.1 Index2 = 0

1.3.2 IF Data[1][loop2] = Sex[index2]

1.3.3 Print Sex[loop2]

1.3.4 FOR loop3= 1 -> n

1.3.4.1 SWITCH Data[7][loop3]

1.3.4.1.1 CASE 1 : Hindu++

1.3.4.1.2 CASE 2: Muslim++

1.3.4.1.3 CASE 3: Christian++

1.3.4.1.4 CASE 9: Others++

1.3.5 Print Hindu, Muslim, Christian, Others

1.3.6 index2++

1.4 index1++

Main() function:

1. Begin

2. GOTO AcceptValues

3. GOTO Census

4. End program

/****************

ReplyDeleteStudent database

*****************/

#include

#include

/* Sorting data using bubble sort */

int* Sort(int* Marks, int Num)

{

int temp, loop1, loop2;

for(loop1=0; loop1 Marks[loop2+1])

{

temp=Marks[loop2];

Marks[loop2]=Marks[loop2+1];

Marks[loop2+1]=temp;

}

return Marks;

}

/* Searching the details based on name */

int SearchByName(char* Name, char* Value, int n)

{

int loop;

for(loop=0; loop4)

printf("\n Illegal choice!!");

else

{

switch(Choice)

{

case 1: printf("\n Enter name to search: "); /* Searching by name */

scanf("%s",Value);

temp1= SearchByName(Name, Value, Num);

if(temp1!=-1)

printf("\n Name: %s\n ID: %d\n Marks: %d\n\n", Name[temp1], ID[temp1], Marks[temp1]);

else

printf("\n Name not found");

break;

case 2: printf("\n Enter ID to search: "); /* Searching by ID */

scanf("%d", &temp2);

temp1= Search(ID, temp2, Num);

if(temp1!=-1)

printf("\n Name: %s\n ID: %d\n Marks: %d\n\n", Name[temp1], ID[temp1], Marks[temp1]);

else

printf("\n ID not found");

break;

case 3: printf("\n Students record with rank\n\n"); /* Printind data rankwise */

for(loop=0; loop<Num; loop++)

printf("\n Rank: %d\n Name: %s\n ID: %d\n Marks: %d\n\n", loop+1, Name[loop], ID[loop], Marks[loop]);

break;

case 4: printf("\n Enter ID to search: "); /* Searching name by ID */

scanf("%d", &temp2);

temp1= Search(ID, temp2, Num);

if(temp1!=-1)

printf("\n Name: %s\n\n",Name[temp1]);

else

printf("\n ID not found");

break;

}

}

getch();

return 0;

}

I tried code 2 using for loop instead of while loop, but it is not working. What could be the possible reason. I replaced i=1;

ReplyDeletesum = 0;

while(i<n){

if(n%i==0)

sum=sum+i;

i++;

}

by

for(i=1;i<n;++i)

{

if(n%i==0)

sum+=i;

}

The compiler shows no errors but blank space after the line "The perfect numbers are:". Please reply.

I forgot to mention I also intialised sum to 0 in the beginning.

ReplyDelete#include

ReplyDelete#include

void mail ()

{

int n,i,sum;

clrscr();

printf("perfect number\n");

for(n=1;n<=100;n++)

{

i=1;

sum=0;

while(i<n)

{

if(n%i==0)

sum=sum+i;

i++;

}

if(sum==n)

printf("%\n",n);

}

getch();

}