Saturday 19 March 2016

Ascending Order


Write a program to find whether the given array is sorted in ascending order.

Input Format: 
Input consists of n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the first array. Assume that the maximum value of n is 15.

Output Format: 
Print yes if the array is sorted in asecending order. Print no if the array is not sorted in ascending order.

Sample Input 1: 
 5
2
3
6
8
1

Sample Output 1: 
no

Sample Input 2: 
 5
 2
 3
 6
 8
10

Sample Output 2: 
 yes

Code
  #include<stdio.h>
int main(){
  int n,i,a[15],flag=1;
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  if(i>0)
  {
    if(!(a[i-1]<a[i]))
      flag=0;
  }
  }
  if (flag)
    printf("yes");
  else 
    printf("no");
  return 0;   
}

2 comments:

  1. Great post!!Thanks for sharing it with us....really needed.You’ll be able to order checks for personal or business use. you will not need to concern yourself with having someone else do this once arduous task.Click here

    ReplyDelete
  2. a small correction...... if(flag==0)
    printf("yes");

    ReplyDelete