Advertisement
stsharin

Untitled

Dec 9th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. //Problem: find the first odd and last even number then swap them and print
  2.  
  3. #include<stdio.h>
  4. int main()
  5. {
  6. int a[1000],size,emax,omax,emaxi,omaxi,temp=0,flag=0;
  7. printf("Enter array size: ");
  8. scanf("%d",&size);
  9.  
  10. int i;
  11. for(i=0;i<size;i++){
  12. scanf("%d",&a[i]);
  13. }
  14. emax=0; omax=0;
  15. for(i=0;i<size;i++){
  16. if(a[i]%2==0){
  17. emax=a[i];
  18. emaxi=i;
  19. }
  20. else if(a[i]%2!=0){
  21. if(flag==0){
  22. omax=a[i];
  23. omaxi=i;
  24. flag++;
  25. }
  26. }
  27. }
  28.  
  29. temp= a[emaxi];
  30. a[emaxi]=a[omaxi];
  31. a[omaxi]=temp;
  32.  
  33. for(i=0;i<size;i++){
  34. printf("%d ",a[i]);
  35. }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement