Programming In C Practical 15

Question 1
Write a program to sort given array in ascending order.
Sort.cpp
C
				#include<stdio.h>
#include<conio.h>

void main()
{
	clrscr();
	int arr[20],i,j,size,temp;
	printf("\nEnter size of array: ");
	scanf("%d",&size);
	printf("\nEnter numbers in array: ");
	for(i=0;i<size;i++)
	{
		printf("\nEnter number at arr[%d]:",i+1);
		scanf("%d",&arr[i]);
	}

	for(i=0;i<size;i++)
	{
		for(j=0;j<size-i-1;j++)
		{
			if(arr[j]>arr[j+1])
			{
				temp=arr[j];
				arr[j]=arr[j+1];
				arr[j+1]=temp;
			}
		}
	}

	printf("\nSorted array is:");
	for(i=0;i<size;i++)
	{
		printf("\narr[%d]: %d",i+1,arr[i]);
	}
	getch();
}
			
Output
Question 2
Write a C program to generate the output as follows:
4
4 3
4 3 2
4 3 2 1
Pattern.cpp
C
				#include<stdio.h>
#include<conio.h>

void main()
{
	clrscr();
	int i,j;
	for(i=4;i>=1;i--)
	{
		for(j=4;j>=i;j--)
		{
			printf("%d\t",j);
		}
		printf("\n");
	}

	getch();
}
			
Output

Leave a Comment

Your email address will not be published. Required fields are marked *