II-122 Programming Concepts
6. Display ASCII values and corresponding characters
void main()
{
char x;
int i;
clrscr();
for(i=65;i<=68;i++)
{
printf("%d %c",i,i);
}
getche();
}
OUTPUT:
65 A 66 B 67 C 68 D
7. Leap year program
void main()
{
int y,x=0;
clrscr();
for(y=2000;y<=2007;y++)
{
if(y%4==0 && y%100!=0 || y%400==0)
{
printf("%d",y);
x++;
}
}
printf(" %d",x);
getche();
}
OUTPUT:
2000 2004
2
8. Largest from an array
void main()
{
int x[5]={5,15,3,10,7};
int i;
clrscr();
for (i=0;i<5;i++)
M06_ITL-ESL4791_02_SE_C06.indd 122 12/22/2012 5:02:22 PM
Loop Control Statements II-123
{
if(x[i]>x[i+1])
x[i+1]=x[i];
}
printf ("%d ",x[i]);
getche();
}
OUTPUT:
15
9. Identify exact square numbers up to 100
#include <math.h>
void main()
{
int i,x,count=0;
clrscr();
for(i=0;i<100;i++)
{
x=sqrt(i);
if(i==x*x)
{
printf("%d",i);
count+=1;
}
}
getche();
}
OUTPUT:
0 1 4 9 16 25 36 49 64 81
10. Prime number program
void main()
{
int i=19,n;
clrscr();
{
for(n=2;n<i-1;n++)
{
if(i%n==0)
{
printf("nThe number %d is not a prime",i);
M06_ITL-ESL4791_02_SE_C06.indd 123 12/22/2012 5:02:22 PM
II-124 Programming Concepts
getche();
exit();
}
}
printf(" The number %d is a prime",i);
getche();
}
}
OUTPUT:
The number 19 is a prime
Programs on Nested for Loop
11. Some mathematical problem
void main()
{
int i,j=0;
clrscr();
for (i=48;i<=49;i++)
{
for (j=49;j<=50;j++)
printf("%c%c",i,j);
printf(" ");
}
getche();
}
OUTPUT:
01 02
11 12
12. Multiples of 1, 2 and 3
#include <math.h>
void main()
{
int i,x,j;
clrscr();
for(i=1;i<=4 ;i++)
{
for(j=1;j<=3;j++)
printf("%d",i*j);
printf(" ");
printf("");
M06_ITL-ESL4791_02_SE_C06.indd 124 12/22/2012 5:02:22 PM
Loop Control Statements II-125
}
getche();
}
OUTPUT:
1 2 3
2 4 6
3 6 9
4 8 12
13. Prime number program
#include<math.h>
#include<stdio.h>
void main()
{
int i,n=0,j,x;
clrscr();
printf(" Prime numbers from 1 to 15:-");
printf("");
for(i=1;i<=15;i++)
{
x=sqrt(i);
for(j=2;j<=x;j++)
{
if(i%j==0)
goto end;
}
printf("%d",i);
n++;
end:;
}
printf(" Total prime numbers from
1 to 15:- %d ",n);
getche();
}
OUTPUT:
Prime numbers from 1 to 15: 1 2 3 5 7 11 13
Total prime numbers from 1 to 15: 7
14. Display stars in speciļ¬c order.
#include <stdio.h>
void main()
{
M06_ITL-ESL4791_02_SE_C06.indd 125 12/22/2012 5:02:22 PM
II-126 Programming Concepts
int x=3,i,j;
clrscr();
for (i=1;i<=x;i++)
{
for (j=1;j<=i;j++)
{
printf ("*");
}
printf(" ");
}
getche();
}
OUTPUT:
*
* *
* * *
15. Display numbers in ascending order
void main()
{
int x[5]={5,15,3,10,7};
int i,j,temp;
clrscr();
for (i=0;i<5;i++)
{
for (j=i;j<4;j++)
{
if(x[i]>x[j+1])
{
temp=x[i];
x[i]=x[j+1];
x[j+1]=temp;
}
}
}
for (i=0;i<5;i++)
printf ("%d ",x[i]);
getche();
}
OUTPUT:
3 5 7 10 15
M06_ITL-ESL4791_02_SE_C06.indd 126 12/22/2012 5:02:22 PM
..................Content has been hidden....................

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