C Programing Labsheet 4 Basic
C Programing Labsheet 4 Basic
Department of Avionics
MA231: C PROGRAMMING LAB
Lab-sheet Basic
Lab Sheet No: 4
1.
Write a program to compute the average of the ten numbers 1, 4, 9... 81, 100, that is, the
average of the squares of the numbers from 1 to 10.
Program :
#include<stdio.h>
#include<conio.h>
int main()
{
int i,s;
float avg,sum=0;
for (i=1;i<=10;i++)
{
s=i*i;
sum=sum+s;
}
avg=sum/10;
printf("average=%f",avg);
getch();
}
Output :
Average=38.500000
2.
Write a program to print the numbers between 1 and 10, along with an indication of
whether each is even or odd, like this:
1 is odd
2 is even ...
(Hint: use the % operator.)
Program :
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
if(i%2==0)
printf("\n%d is even",i);
else
printf("\n%d is odd",i);
}
getch();
}
Output :
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
3.
Write a program to print the first 7 positive integers and their factorials. (The factorial of 1 is
1, the factorial of 2 is 1 * 2 = 2, the factorial of 3 is 1 * 2 * 3 = 6, the factorial of 4 is 1 * 2 * 3 * 4 = 24,
etc.).
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j=1;
for(i=1;i<=7;i++)
{
j=j*i;
printf("\n%d!=%d",i,j);
}
getch();
}
Output:
1! =1
2! =2
3! =6
4! =24
5! =120
6! =720
7! =5040
4.
Write a program to print the first 10 Fibonacci numbers. Each Fibonacci number is the sum
of the two preceding ones. The sequence starts out 0, 1, 1, 2, 3, 5, 8, ...
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,a=0,b=1,c;
printf("%d,%d,",a,b);
for(i=0;i<=7;i++)
{
c=a+b;
printf("%d,",c);
a=b;
b=c;
}
getch();
}
Output:
0,1,1,2,3,5,8,13,21,34
Discussion: Here we had taken i<=7 because upto 7 numbers we will get first 10 Fibonacci numbers.
5.
Write a program to print the following pattern:
Ques. (a)
A
BB
CCC
DDDD
EEEEE
FFFFFF
Ques.(b)
A
AB
ABC
ABCD
ABCDE
ABCDEF
Program: (a)
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
char c='A';
for (i=1;i<=6;i++)
{
printf("\t%c",c);
for(j=2;j<=i;j++)
{
printf("\t%c",p);
}
ch++;
printf("\n");
}
getch();
}
Output:
A
BB
CCC
DDDD
EEEEE
FFFFFF
Program: (b)
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
char c='A';
for (i=1;i<=6;i++)
{
for(j=1;j<=i;j++)
{
printf(" %c",c);
ch++;
}
printf("\n");
}
getch();
}
Output:
A
AB
ABC
ABCD
ABCDE
ABCDEF
Bonus:
1.
Write a program to display any one letter out of the following letters in a grid of 15 rows and
18 columns of stars (*).
A, S, H, U, Z, W, V, C, Y, T, L, O, Q
E.g.,
******************
******************
******************
****
****
****
******************
******************
******************
****
****
****
******************
******************
******************
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;i<=15;i++)
{for(j=1;j<=18;j++)
{
if(i>3&&i<7&&j>4)
printf("");
else if(i>9&&i<13&&j>4)
printf("");
else
printf("*");
}printf("\n");
}
getch();
}
Output:
******************
******************
******************
****
****
****
******************
******************
******************
****
****
****
******************
******************
******************