Walk Lightly on this PLANET and yet leave such a FOOTPRINT that cannot be erased for thousands of Years..!!!
Visit Codstech for Cyber Security related Posts !

Visitors

Friday, February 11, 2011

C18 : Sort n numbers of array elements in ascending order

Back to Home   &  C


//Enter n numbers into an array and sort it in ascending order.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,n,a[20],t;
printf("Enter limit value\n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
 }
printf("Array elements in ascending order...\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
-------------------------------------------------------------------
Output
Enter limit value
3
Enter array elements
3
1
6
Array elements in ascending order...
1
3
6
===============================================
Back to Home   &  C

C17 : Enter n numbers into an Array and print it

Back to Home   &  C


//Program to enter n numbers into an array and print it

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,n;
printf("Enter limit value\n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Array elements are...\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
-----------------------------------------------------------------
Output
Enter limit value
2
Enter array elements
16
76
Array elements are...

16
76
===============================================
Back to Home   &  C

C16 : Array Declaration

Back to Home   &  C


//Program to declare an Array and initialize value to it

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5]={20,30,10,40,60};
int i;
for(i=0;i<5;i++)
 {
 printf("\n%d\n",a[i]);
 }
getch();
}
---------------------------------------------------------
Output
20
30
10
40
60
===============================================
Back to Home   &  C

C15 : Print numbers that is divisible by 7

Back to Home   &  C


//Program to print numbers that is completely divisible by 7 between 1 and n,
also count total number,and find its sum

#include<stdio.h>
#include<conio.h>
void main()
 {
 clrscr();
 int i=0,ct=0,sum=0,n;
 printf("Enter limit value\n");
 scanf("%d",&n);
 printf("The numbers are...\n");
 while(i<=n)
 {
 if(i%7==0)
 {
 printf("%d\n",i);
 ct++;
 sum=sum+i;
 }
 i++;
 }
 printf("sum=%d\n",sum);
 printf("count=%d",ct);
 getch();
 }
-----------------------------------------------------------------------------------
Output
Enter Limit
50
Numbers are...
0
7
14
21
28
35
42
49
sum=196
count=8
====================================
Back to Home   &  C