II-150 Programming Concepts
7. Factorial of 5
void main()
{
int i,m=1,num[5]={1,2,3,4,5};
clrscr();
for(i=0;i<=4;i++)
m=m*num[i];
printf("Total is %d ",m);
getche();
}
OUTPUT:
120
8. Arrange numbers in ascending order
void main()
{
int i,k,temp,num[6]={4,6,8,3,2,10};
clrscr();
for(i=0;i<=5;i++)
{
for(k=i+1;k<5;k++)
{
if(num[i]>num[k])
{
temp=num[i];
num[i]=num[k];
num[k]=temp;
}
}
}
for(i=0;i<6;i++)
printf(" %d",num[i]);
getche();
}
OUTPUT:
2
3
4
6
8
10
M07_ITL-ESL4791_02_SE_C07.indd 150 12/22/2012 5:03:01 PM
Array II-151
9. Reversing text and a number
void main()
{
char name[7]={'M','A','L','A','L','A','M'};
int i,rev[]={4,5,6};
clrscr();
for(i=6;i>=0;i--)
{
printf("%c",name[i]);
}
printf(" ");
for(i=2;i>=0;i--)
{
printf("%d",rev[i]);
}
getche();
}
OUTPUT:
MALALAM
10. Display array elements with pointer
void main()
{
int j[4]={2,3,4,5};
int i;
clrscr();
for(i=0;i<=3;i++)
printf(" %d",*(j+i));
getche();
}
OUTPUT:
2 3 4 5
M07_ITL-ESL4791_02_SE_C07.indd 151 12/22/2012 5:03:01 PM
II-152 Programming Concepts
Two Dimensional Array
11. Program to print array elements in reverse order.
void main()
{
int i,j;
int a[3][3]={{9,8,7},{6,5,4},{3,2,1}};
clrscr();
printf("Elements of an array in reverse order. ");
for(i=2;i>=0;i--)
{
for(j=2;j>=0;j--)
printf("%5d",a[i][j]);
printf(" ");
}
getche();
}
OUTPUT:
Elements of an array in reverse order
1 2 3
4 5 6
7 8 9
12. Display odd numbers using two for loops.
#include <stdio.h>
#include <conio.h>
void main()
{
int j,k,x[4][2]={1,2,3,4,5,6,7,8};
clrscr();
for(j=0;j<4;j++)
for(k=0;k<1;k++)
printf(" %d",x[j][k]);
}
OUTPUT:
1 3 5 7
M07_ITL-ESL4791_02_SE_C07.indd 152 12/22/2012 5:03:01 PM
Array II-153
13. Display numbers in some sequence using two for loops
#include <stdio.h>
#include <conio.h>
void main()
{
int j,k,x[4][2]={1,2,3,4,5,6,7,8};
clrscr();
for(j=0;j<3;j++)
for(k=0;k<1;k++)
printf(" %d",x[j][k]);
}
OUTPUT:
1 3 5
14. Display some numbers using an array
#include <stdio.h>
#include <conio.h>
void main()
{ int x[3][3]={4,5,6,7,8,9,10,11,12};
int j,k;
clrscr();
for(j=0;j<3;j++)
for(k=0;k<3;k++)
printf(" %d ",x[k][j]);
}
OUTPUT:
4 7 10 5 8 11 6 9 12
M07_ITL-ESL4791_02_SE_C07.indd 153 12/22/2012 5:03:01 PM
II-154 Programming Concepts
More Than Two Dimensional Array
15. Display numbers using two for loops.
#include <stdio.h>
#include <conio.h>
void main()
{
int array[2][2][2][2]={1,2,3,4,5,6,7,8};
int a,b,c,d;
clrscr();
for(b=0;b<2;b++)
for(a=0;a<2;a++)
printf(" %d ",array[a][b][0][0]);
}
OUTPUT:
1 0 5 0
Find the bug/s in the following program/s.
1.
void main()
{
int a[5]={5,2,3,1,4};
int i,result=1;
clrscr();
for(i=0;i<=5;i++)
result=result*a[i];
printf(" result of multiplication =%d ",result);
getche();
}
Bug: No bug
M07_ITL-ESL4791_02_SE_C07.indd 154 12/22/2012 5:03:01 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset