//interchange adjacent elements of an array
#include<stdio.h>
int main()
{
int i,t;
int num[10]={1,2,3,4,5,6,7,8,9,10};
for(i=0;i<=9;i+2)
{
t=num[i];
num[i]=num[i+1];
num[i+1]=t;
}
for(i=0;i<=9;i++)
printf("%d",num[i]);
return 0;
}
1. //interchange adjacent elements of an array
2. #include<stdio.h>
3. int main(){
4. int i,t;
5. int num[10]={1,2,3,4,5,6,7,8,9,10};
6. for(i=0;i<=9;i+=2){
7. t=num[i];
8. num[i]=num[i+1];
9. num[i+1]=t;
10. }
11. for(i=0;i<=9;i++) printf("%d",num[i]);
12. return 0;
13. }
Check the line number 6, you forgot to increment the counter variable “i”.
It should be “i+=2” instead of “i+2”, otherwise it would be an infinite loop.
1 Like
In first loop condition you had to write i=i+2 not i+2
for(i=0;i<=9;i+2) this line is incorrect
for(i=0;i<=9;i=i+2)
or
for(i=0;i<=9;i+=2)
in for loop you should write i=i+2 or i+=2
These forums XD