Rajshahi University of Engineering & Technology: Department or Electrical and Electronic Engineering

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Heaven’s Light is our guide

Rajshahi University of Engineering & Technology


Department or Electrical and Electronic Engineering

K. M. Nasim (Rusho)
Roll:081014
Course No: EEE-152
Course name: Fundamentals of computing Sessional
GMail Address: rusho.eee.08@gmail.com
EEE-152__Programs & Their Outputs -2-

Chapter-1 (Overview of C)

1. Write a program to add two numbers.

Program:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{ Output: 40
clrscr();
int a,b,c;
a=13;
b=27;
c=a+b;
printf("%d",c);
getch();
}

2. Write a program to find the cosine of angle.

Program:
#include <stdio.h>
#include <math.h>
#include <conio.h> Output:
void main() Enter the value of angle
{ 45
clrscr(); Result=0.707106
float a,b,c;
printf("Enter the value of angle\n");
scanf("%f",&a);
b=3.1416*a/180;
c=cos(b);
printf("Result=%f",c);
getch();
}

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs -3-
3. Write a program to subtraction, multiplication, division of more than two numbers.

Program:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
clrscr();
float a,b,c;
scanf("%f %f %f",&a,&b,&c);
printf("a+b+c=%.2f a-b+c=%.2f a*b*c=%.2f a*b/c=%.2f",a+b+c,a-b+c,a*b*c,a*b/c);
getch();
}
Output:
5
10
15
a+b+c=30.00a-b+c=10.00 a*b*c=750.00 a*b/c=3.33

4. 1+2+3+4+5+…………………………….+50=? (While loop)

Program:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
clrscr();
float sum,i; Output:
sum=0;
i=1; 1275
while(i<=50)
{
sum=sum+i;
i=i+1;
}
printf("%.2f",sum);
getch();
}

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs -4-
5. 1*2*3*4*5……………………………….*n=? (While loop)
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int n;
printf("Enter the value of n\n"); Output:
scanf("%d",&n); Enter the value of n
float sum,i; 5
sum=1; 1*2*3*....*5=120
i=1;
while(i<=n)
{
sum=sum*i;
i=i+1;
}
printf("1*2*3*....*%d=%.0f",n,sum);
getch();
}

Chapter-2 (Constant, variables and data types)

1. Write a program to find smaller/greater number from a reference number.


Program:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main() Output:
{
clrscr(); Enter an integer number
int a; 25
printf("Enter an integer number\n"); Your value is greater than 20
scanf("%d",&a);
if(a>20)
printf("Your value is greater than 20");
else
printf("Your value is smaller than 20");
getch();
}

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs -5-

2. Write a program to calculate the average set of N(Three) numbers.

Program:

#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{ Output:
clrscr(); 25
float a,b,c,d; 13
scanf("%f %f %f",&a,&b,&c); 21
d=(a+b+c)/3; Average=19.666666
printf("Average=%f",d);
getch();
}

3. Write a program to read the price of an item an decimal form (Like 15.95) and print
the output in paise (like 1595 paise)

Program:

#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{ Output:
clrscr(); 11
float taka; paisa=1100
int paisa;
scanf("%f",&taka);
paisa=taka*100;
printf("paisa=%d",paisa);
getch();
}

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs -6-
4. Write a program that will calculate the sum of every third integer beginning with i=2
(i.e. the sum 2+5+8+11+……..) for all values of i that are less than 100.
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
float sum,i; Output:
sum=0; 1650
i=2;
while(i<100)
{
sum=sum+i;
i=i+3;
}
printf("\n%.0f",sum);
getch();
}

Chapter-3 (Operators and Expressions)

1. Write a program to print a sequence of square of numbers.


Program:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
float sum,i; Output:
sum=0; 1 4 9 16 25 36 49 64 81
i=1;
while(i<=9)
{
sum=i*i;
i=i+1;
printf(" %.0f",sum);
}
getch();
}

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs -7-
2. Write a program to show the round off errors (Sum of n terms of 1/n).
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
float sum,n,term; Output:
int count=1; Enter value of n
sum=0; 5
printf("Enter value of n\n"); sum=1.00
scanf("f",&n);
term=1.0/n;
while(count <=n)
{
sum= sum+term;
count= count+1;
printf(" sum=%.2f\n",sum);
}
getch();}

3. Write a program to find the solution of the quadratic equation.


Program:
#include<math.h>
#include<stdio.h>
#include<conio.h>
void main()
{clrscr();
float a,b,c,dis,r1,r2;
printf("Enter the value of coefficients\n");
scanf("%f %f %f",&a,&b,&c); Output:
dis=b*b-4*a*c; Enter the value of coefficients
if(dis<0) 5
printf("\nimaginary"); 10
else 2
{ r1=-0.225403and r2=-1.774597
r1=(-b+sqrt(dis))/(2*a);
r2=(-b-sqrt(dis))/(2*a);
printf("r1=%f and r2=%f",r1,r2);}
getch();
}

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs -8-

Chapter-4 (Decision making and branching)

1. Write a program to count the number of boys whose weight is less than 50kgs and
height is greater than 170 cm.

Program:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main ()
{
clrscr();
int count,i;
float weight,height;
count=0;
printf("Enter weight and height of 5 boys\n");
for (i=1; i<=5; i++)
{
scanf("%f%f",&weight,&height);
if(weight<50 && height>170)
count=count+1;
}
printf("Number of boys of weight <50kgs and height >170 cm=%d\n",count);
getch();
}

Output:
Enter weight and height of 5 boys
45
172
49
178
56
179
58
180
56
145
Number of boys of weight <50kgs and height >170 cm=2

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs -9-
2. Write a program to evaluate the power series e^x=1+x+x^2/2!+………+x^n/n! , 0<x<1.

Program:
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define ACCURACY 0.001
void main ()
{clrscr(); Output:
int n,count; Enter the value of x
float x,term,sum; 3
printf("Enter the value of x\n"); Terms=14 Sum=20.085470
scanf("%f",&x);
n=term=sum=count=1;
while(n<=100)
{term=term*x/n;
sum=sum+term;
count=count+1;
if(term<ACCURACY)
n=999;
else
n=n+1;}
printf("Terms=%d Sum=%f\n",count,sum);
getch();}

3. Write a program to select and print the largest of three numbers.

Program:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
clrscr();
int a,b,c;
printf("Input the three numbers respectively\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c) Output:
printf("The Largest number is %d",a); Input the three numbers respectively
else if(b>a && b>c) 5
printf("The Largest number is %d",b); 9
else 4
printf("The Largest number is %d",c); The Largest number is 9
getch();
}

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 10 -
4. Write a program to find the square root of a given number.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{ Output:
clrscr(); Enter the value of a:
float i, a; 9
printf("Enter the value of a:\n"); The sqrt of 9 is=3
scanf("%f",&a);
i=sqrt(a);
printf("The sqrt of %.2f is=%f",a,i);
getch();
}
5. Write a program to convert marks to its equivalent letter grade according to RUET grading system.
Program:
#include<stdio.h> case 13:
#include<conio.h> grade="B+";
#include<math.h> break;
//#include<string.h> case 12:
//index=marks/20 grade="B";
//switch(index) break;
void main() case 11:
{ grade="B-";
clrscr(); break;
int marks, i,index; case 10:
//index=marks/20 grade="C+";
//switch(index) break;
char*grade; case 9:
for(i=1;i<2;i++) grade="C";
{printf("Enter your marks:"); break;
scanf("%d",&marks); case 8:
index=marks/5; grade="D";
switch(index) break;
{case 20: default:
case 19: grade="fail";
case 18: break;
case 17: }
case 16: printf("\n%s\n",grade);
grade="A+"; }
break; getch();
case 15: }
grade="A"; Output:
break; Enter your marks:45
case 14: C
grade="A-"; Enter your marks:81
break; A+

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 11 -

6. Write a program to convert a CGPA to its equivalent results according to the following conditions:
If CGPA 3.75 then write ‘Honours’
3.00<CGPA<3.75, then write ‘First class’
2.00<CGPA<2.99, then write ‘Second class’
CGPA<1.99, then write ‘Fail’
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float CGPA;
printf("Enter your CGPA:"); Output:
scanf("%f",&CGPA); Enter your CGPA:2.99
if (CGPA>=3.75) your grade is Second class
printf("your grade is Honours");
else if (3.75>CGPA&&CGPA>=3)
printf("your grade is first class");
else if (3>CGPA&&CGPA>=1.99)
printf("your grade is Second class");
else
printf("your grade is fail");
getch();
}

7. Write a program to determine whether a number is ‘odd’ or ‘even’ and print the message
NUMBER IS ODD
OR
NUMBER IS EVEN
(a) Without using else option (b) with else option
(a)
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
clrscr(); Output:
int i, a; Enter your number:7
printf("Enter your number:"); your number is odd
scanf("%d",&a);
if(a%2==0)
printf("your number is even");
if(a%2==1)
printf("your number is odd");
getch();}

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 12 -
(b)
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
clrscr(); Output:
int i, a; Enter your number:6
printf("Enter your number:"); your number is even
scanf("%d",&a);
if(a%2==0)
printf("your number is even");
else
printf("your number is odd");
getch();
}

8. Write a program to find the number of and the sum of all integers greater than 100 and less than 200
that are divisible by 7.

Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
clrscr();
int i,sum=0,count=0;
for (i=101;(i>100&&i<200);i++)
{
if(i%7==0)
{
sum=sum+i;
count++;
}
}
printf("count=%d sum=%d",count,sum);
getch();
}

Output:

105 112 119 126 133 140 147 154 161 168 175 182 189 196
count=14 sum=2107

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 13 -
9. Write a program to produce the following form of Floyd’s triangle.
1
01
101
0101
10101
Program:
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int i,j,k,a,b,n,c;
printf("How many lines will you print:");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
if(i%2==1)
if(j%2==1) printf("1");
else printf("0");

else if(j%2==0) printf("1"); Output:


else printf("0"); How many lines will you print:5
1
printf("\n"); 01
} 101
getch(); 0101
} 10101

Chapter-6 (Decision making and looping)

1. Write a program to evaluate the equation y=x^n , where n is a non-negative integer.


Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{ {
clrscr(); y=y*x;}
int n,i; printf("%.2f",y);
float x,y=1; getch();
printf("Enter the value of x:"); }
scanf("%f",&x); Output:
printf("Enter the value of n:"); Enter the value of x:4
scanf("%d",&n); Enter the value of n:3
for(i=1;i<=n;i++) 64.00

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 14 -
2. Write a program to print the multiplication table from 1*1 to 12*10 as shown below:
1 2 3 4………………..10
2 4 6 8………………..20
3 6 9 12………………30
.
.
12 ……………120
Program:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
clrscr(); Output:
int i,j,y; 1 2 3 4 5 6 7 8 9 10
for(i=1;i<=12;i++) 2 4 6 8 10 12 14 16 18 20
{ 3 6 9 12 15 18 21 24 27 30
for(j=1;j<=10;j++) 4 8 12 16 20 24 28 32 36 40
{ 5 10 15 20 25 30 35 40 45 50
y=i*j; 6 12 18 24 30 36 42 48 54 60
printf("%4d",y); 7 14 21 28 35 42 49 56 63 70
} 8 16 24 32 40 48 56 64 72 80
printf("\n"); 9 18 27 36 45 54 63 72 81 90
} 10 20 30 40 50 60 70 80 90 100
getch(); 11 22 33 44 55 66 77 88 99 110
} 12 24 36 48 60 72 84 96 108 120

3. Write a program to print the “Power of 2” for the power 0 to 20, both positive and negative.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
clrscr();
long int p=1;
int n;
double q;
printf("\n 2 to the power n n 2 to the power -n\n");
for (n=0;n<=20;n++)
{ if(n==0)
p=1;
else
p=p*2;
q=1/(double)p;
printf("\n%10ld%15d%20.12lf\n",p,n,q);}
getch();
}

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 15 -
4. Write a program to compute the average of a set of numbers.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
clrscr();
float i,x,sum=0,avg;
printf("Enter the values:");
printf("\nEnter a negative number at the end:");
for(i=1;i<=1000;i++)
{
scanf("%f",&x);
if(x<0)
break; Output:
sum=sum+x; Enter the values:
} Enter a negative number at the end:12
avg=sum/(i-1); 23
printf("\n"); 24
printf("number of values =%f\n",i-1); -1
printf("sum =%f\n",sum);
printf("average =%f",avg); number of values =3.000000
getch(); sum =59.000000
} average =19.666666

5. Write a program to evaluate the series 1/1-x=1+x+x^2+…………..+x^n


For -1<x<1 to evaluate to 0.01 percent accuracy.
Program:
#include<stdio.h>
#include<conio.h>
#define ACCURACY .0001
main()
{
clrscr();
float i,x,term=1,sum=1;
printf("input the value of x:");
scanf("%f",&x);
for(i=1;i<=100;i++)
{
term=term*x;
sum=sum+term;
if(term<=ACCURACY)
break;
}
printf("Sum=%f",sum); Output:
getch(); input the value of x:.75
} sum=3.999774

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 16 -

6. Write a program using while loop to reverse the digits of the number. For example, the number
12345 should be written as 54321

Program:
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int n,s=0;
long int a,b;
printf("\nEnter a number of 5 digits=");
scanf("%ld",&a);
printf("\n The reverse digits are =");
for(n=1;n<=5;n++)
{
b=a%10;
printf("%ld",b);
a=a/10;
} Output:
getch(); Enter a number of 5 digits=46524
} The reverse digits are=42564

7. Write a program to compute the sum of digits of a given integer number.

Prograns:
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int n,s=0;
long int a,b;
printf("\nEnter a number of 5 digits=");
scanf("%ld",&a);
for(n=1;n<=5;n++)
{
b=a%10;
a=a/10;
s=s+b;
}
printf("\n sum of this 5 digits is =%d",s); Output:
getch(); Enter a number of 5 digits=13245
} sum of this 5 digits is =15

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 17 -
8. The number in the sequence
1 1 2 3 5 8 13 21 ……………….
are called Fibonacci numbers. Write a program using a do…..while loop to calculate and print the
first m Fibonacci numbers.
(Hints: After the first two numbers in the series, each number is the sum off two preceding
numbers.)
Program:
#include<stdio.h>
#include<conio.h>
void main()
{clrscr();
int a,b,c,d;
printf("Enter the value of d=");
scanf("%d",&d);
a=1;
b=0;
do
{c=a+b;
if(c>d)
break;
printf("%3d",c);
a=b;
b=c;
}
while(2 >1); Output:
getch();} Enter the value of d=8
1 1 2 3 5

9. Write a program that will read a positive integer and print its binary equivalent.
(Hints: The bit of the binary representation of an integer can be generated by repeatedly dividing
the number and the successive quotients by 2 and saving the remainder, which is either 0 or 1, after
each division.)
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{clrscr();
long int a,e=0,b=0,c=1,d ;
printf("Enter Your Decimal number:");
scanf("%ld",&a);
while(a>0)
{c=a%2; printf("%ld",e);
d=c*pow(10,b); //printf("\n%ld",e);
//printf("%3ld",d); getch();}
a=a/2; Output:
e=e+d; Enter Your Decimal number:6
b++;} 110

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 18 -

10. Write a program to read the age of 100 (Here 5) persons and count the number of
persons in the age group 50 to 60. Use for and continue statement.
Program:
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a,c=0,n;
printf("Enter the age:");
for(a=1;a<=5;a++)
{
scanf("%d",&n);
if(n>=50&&n<=60) Output:
c++; Enter the age=45
continue; 55
} 56
printf("The no of persons in the age 45
50 to 60 is %d",c); 78
getch(); The no of persons in the age 50 to 60
} is=2

Chapter-7 (Array)

1. Write a program to find the sum of 10 real numbers by using one dimensional array.
Program:
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int num[5],i,sum=0;
for(i=0;i<5;i++)
{ Output:
printf("\nEnter num[%d]: ",i+1); Enter num[1]:5
scanf("%d",&num[i]); Enter num[2]:8
sum=sum+num[i];} Enter num[3]:7
printf("sum=%d",sum); Enter num[4]:9
getch(); Enter num[5]:3
} Sum=32

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 19 -
2. Write a program to print multiplication table using two dimensional arrays.
Program:
#include<stdio.h>
#include<conio.h>
#define ROW 3
#define COL 3
void main()
{
clrscr();
int row,col,pro[ROW][COL];
int i,j;
printf("MULTIPLICATION TABLE\n");
printf(" ");
for(j=1;j<=COL;j++)
printf("%4d",j);
printf("\n");
printf("---------------------\n");
for(i=0;i<ROW;i++)
{row=i+1;
printf("%d|",row); Output:
for(j=1;j<=COL;j++) MULTIPLICATION TABLE
{ 1 2 3
col=j;
pro[i][j]=row*col;
---------------------
printf("%4d",pro[i][j]);} 1| 1 2 3
printf("\n");} 2| 2 4 6
getch();} 3| 3 6 9

Chapter-9 (User Defined Function)

1. Write a program to find the value of factorial of n by using recursion.


Program:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{clrscr();
long int n;
unsigned long int factorial(int n);
printf("Enter the value of n:\n");
scanf("%ld",&n);
printf("%ld",factorial(n));
getch();}
unsigned long int factorial(int n)
{if(n<=1) Output:
return (1); Enter the value of n:
else 5
return(n*factorial(n-1));} 120

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 20 -
2. Use recursive calls to evaluate f(x)=x-x^3/3!+x^5/5!-x^7/7!+………..

Chapter-11 (Pointer)

1. Write a program to access the address of variables.


2. Write a program to illustrate the use of pointer in arithmetic operations.
3. Write a program to compute the sum of all elements stored in an array by using pointer.
4. Write a function using pointers to exchange the values stored in two locations in the memory.

Chapter-12 (File management in C)

1. A file named DATA contains a series of integer numbers. Code a program to read these numbers and
then write all odd numbers to a file called ODD and all even numbers to a file to be called EVEN.

Home assignment-01
Decision making and looping
Q.1. Write programs to print the following output using while loops.

(a) 1 (c) ***** (e) 1


11 **** 2 2
111 *** 3 3 3
1111 ** 4 4 4 4
11111 * 5 5 5 5 5

(b) 11111 (d) 1


1111 22
111 333
11 4444
1 55555

(a)
Program:
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a=1,b,c,n;
printf("Enter the value of n:");
scanf("%d",&n);
while(a<=n)

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 21 -
{
b=1;
while(b<=a)
{
b++;
printf("1"); Output:
} Enter the value of n:5
printf("\n"); 1
a++; 11
} 111
getch(); 1111
} 11111

(b)
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i,j,n,c;
printf("Enter the value of n:");
scanf("%d",&n);
c=1;
i=1;
while(i<=n)
{j=5; Output:
while(j>=i) Enter the value of n:5
{printf("%d",c); 11111
j--;} 1111
i++; 111
printf("\n");} 11
getch();} 1

(c)
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i,j,n;
printf("Enter the value of n:");

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 22 -
scanf("%d",&n);
i=1;
while(i<=n)
{j=5;
while(j>=i)
{ Output;
printf("*"); Enter the value of n:5
j--; *****
} ****
i++; ***
printf("\n");} **
getch();} *

(d)
Program:
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a=1,b,c,n;
printf("Enter the value of n:");
scanf("%d",&n);
while(a<=n)
{b=n;
while(b>=a){
printf(" ");
b--;}
c=1; Output;
while(c<=a) Enter the value of n:5
{printf("%2d",a); 1
c++;} 22
printf("\n\n"); 333
a++;} 4444
getch();} 55555

(e)
Program:
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a=1,b,c,n;
printf("Enter the value of n:");
scanf("%d",&n);
while(a<=n)

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 23 -
{
/*b=n;
while(b>=a){
printf(" ");
b--;
}*/
c=1; Output;
while(c<=a) Enter the value of n:5
{printf("%2d",a); 1
c++;} 2 2
printf("\n\n"); 3 3 3
a++;} 4 4 4 4
getch();} 5 5 5 5 5

Q.2. Write programs to print the following output using for loops.

(a) 1 (c) ***** (e) 1


11 **** 2 2
111 *** 3 3 3
1111 ** 4 4 4 4
11111 * 5 5 5 5 5

(b) 11111 (d) 1


1111 22
111 333
11 4444
1 55555

(a)
Program:
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a,b,c,n;
printf("Enter the value of n:"); Output:
scanf("%d",&n); Enter the value of n:5
for(a=1;a<=n;a++) 1
{for(b=1;b<=a;b++) 11
printf("1"); 111
printf("\n");} 1111
getch();} 11111

(b)

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 24 -
Program:
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a,b,c,n;
printf("Enter the value of n:"); Output:
scanf("%d",&n); Enter the value of n:5
for(a=1;a<=n;a++) 11111
{for(b=n;b>=a;b--) 1111
printf("1"); 111
printf("\n");} 11
getch();} 1
(c)
Program:
#include<stdio.h>
#include<conio.h>
main()
{clrscr();
int a,b,c,n;
printf("Enter the value of n:"); Output;
scanf("%d",&n); Enter the value of n:5
for(a=1;a<=n;a++) *****
{for(b=n;b>=a;b--) ****
printf("*"); ***
printf("\n");} **
getch();} *

(d)
Program:
#include<stdio.h>
#include<conio.h>
main()
{clrscr();
int a,b,c,n;
printf("Enter the value of n:");
scanf("%d",&n);
for(a=1;a<=n;a++)
{//for(b=n;b>=a;b--) Output;
//printf(" "); Enter the value of n:5
for(c=1;c<=a;c++) 1
printf("%2d",a); 22
printf("\n\n");} 333
getch();} 4444
55555
(e)
Program:

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 25 -
#include<stdio.h>
#include<conio.h>
main()
{clrscr();
int a,b,c,n;
printf("Enter the value of n:");
scanf("%d",&n);
for(a=1;a<=n;a++) Output;
{for(b=n;b>=a;b--) Enter the value of n:5
printf(" "); 1
for(c=1;c<=a;c++) 2 2
printf("%2d",a); 3 3 3
printf("\n\n");} 4 4 4 4
getch();} 5 5 5 5 5
Q.3. Write programs to print the following output using do-while loops.
(a) 1 (c) ***** (e) 1
11 **** 2 2
111 *** 3 3 3
1111 ** 4 4 4 4
11111 * 5 5 5 5 5

(b) 11111 (d) 1


1111 22
111 333
11 4444
1 55555
(a)
Program:
#include<stdio.h>
#include<conio.h>
main()
{clrscr();
int a=1,b,c,n;
printf("Enter the value of n:");
scanf("%d",&n);
do
{b=1;
do
{b++; Output:
printf("1");} Enter the value of n:5
while(b<=a); 1
printf("\n"); 11
a++;} 111
while(a<=n); 1111
getch();} 11111

(b)
Program:

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 26 -
#include<stdio.h>
#include<conio.h>
main()
{clrscr();
int a=1,b,c,n; a++;}
printf("Enter the value of n:"); while(a<=n);
scanf("%d",&n); getch();}
do Output:
{b=n; Enter the value of n:5
do 11111
{b--; 1111
printf("1");} 111
while(b>=a); 11
printf("\n"); 1
(c)
Program:
#include<stdio.h>
#include<conio.h>
main()
{clrscr();
int a=1,b,c,n;
printf("Enter the value of n:");
scanf("%d",&n);
do
{b=n;
do
{b--; Output;
printf("*");} Enter the value of n:5
while(b>=a); *****
printf("\n"); ****
a++;} ***
while(a<=n); **
getch();} *

(d)
Program:
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a=1,b,c,n;
printf("Enter the value of n:");
scanf("%d",&n);
do
{
b=n;
do

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 27 -
{
printf(" ");
b--;}
while(b>=a);
c=1;
do
{printf("%2d",a);
c++;} Output;
while(c<=a); Enter the value of n:5
printf("\n\n"); 1
a++;} 22
while(a<=n); 333
getch(); 4444
} 55555

(e)

Program:

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a=1,b,c,n;
printf("Enter the value of n:");
scanf("%d",&n);
do
{
b=n;
do
{
printf(" ");
b--;
}
while(b>=a);
c=1;
do
{
printf("%2d",a);
c++;
}
while(c<=a); printf("\n\n");

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 28 -
a++; Enter the value of n:5
} 1
while(a<=n); 2 2
getch(); 3 3 3
} 4 4 4 4
Output; 5 5 5 5 5

Q.4. Write programs to print the following shapes.

(a) (b)
1 1111111111
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1111111111
1111111
(a)
Program:
#include<stdio.h>
#include<conio.h>
main()
{clrscr();
int a,b,c,n;
printf("Enter the value of n:");
scanf("%d",&n);
for(a=1;a<=1;a++)
{printf("1");}
printf("\n");
for(a=1;a<=n-3;a++) Output;
{printf("1"); Enter the value of n:7
for(b=1;b<=a;b++) 1
{printf(" ");} 1 1
printf("1\n");} 1 1
for(a=1;a<=n;a++) 1 1
{printf("1");} 1 1
getch();} 1111111

(b)
Program:
#include<stdio.h>
#include<conio.h>

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 29 -
#include<math.h>
void main()
{clrscr();
int i,j,n;
printf("Enter n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{for(j=1;j<=10;j++) Output;
{if(i==1||j==1||i==n||j==10) Enter n: 6
{printf("1");} 1111111111
else 1 1
{putchar(' ');}} 1 1
printf("\n");} 1 1
getch();} 1111111111
Q.5. Write programs to print the following shapes.

(a) 1 (b) 1
232 1 1
34543 1 1 1
4567654 1 1 1 1
567898765 1 1 1 1 1
1 1 1 1 1 1
(a)
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i,j,c,n;
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
putchar(' ');
}
c=i;
for(j=1;j<=2*i-1;j++)
{
printf("%d",c);

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 30 -
if(j<i)
{
c++;
}
else
{c--;} Output;
} Enter n:5
printf("\n"); 1
} 232
getch(); 34543
} 4567654
567898765

(b)

Program:

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a,b,c,n;
printf("Enter the value of n:");
scanf("%d",&n);

for(a=1;a<=n;a++)
{
for(b=1;b<=n-a;b++)
{
putchar(' ');
}
for(b=1;b<=2*a-1;b++)
{
if(b==1)
printf("1");
else if(b==2*a-1)
printf("1");
else if(a==n-1)
printf("1"); printf("1");
else }

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 31 -
printf("\n"); 1
} 1 1
getch(); 1 1 1
} 1 1 1 1
Output; 1 1 1 1 1
Enter the value of n:6 1 1 1 1 1 1

Q.6. Write programs to print the uppercase letters (i.e. A to Z) so that the output look like

(a) 1 (b) 111111111111111


1 1 1
1 1 1
1 1 1
11111111 1
1 1 1
111111111111111

(c) 11111 (d) 11111111


1 1
1 1
1 11111111
1 1
1 1
11111 11111111

(a)
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int n,i,j;
printf("Enter n :");
scanf("%d",&n);
printf("\n");
for (i=1;i<=n;i++)
{for(j=1;j<=n-i;j++)
{putchar(' ');}

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 32 -
for(j=1;j<=2*i-1;j++)
{if(j==1)
{printf("1");}
else if(j==2*i-1)
{printf("1");} Output;
else if(i==n-1) Enter n:6
{printf("1");} 1
else 1 1
{printf(" ");}} 1 1
printf("\n");} 1 1
getch(); 11111111
} 1 1

(b)
Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{clrscr();
int i,j,n;
printf("Enter n: ");
scanf("%d",&n);
for(i=1;i<=n;i++) Output;
{for(j=1;j<=12;j++) Enter n: 7
{if((i==1||j==1||i==n||j==12||j==12-i+1) 111111111111111
&&(i==1||i==n||j==12-i+1)) 1
{printf("1");} 1
else 1
{putchar(' ');}} 1
printf("\n");} 1
getch();} 111111111111111

(c)
Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{clrscr();
int n,i,j;
printf("Enter n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 33 -
{for(j=1;j<=5;j++)
{
if((i==1||j==1||i==n||j==5||j==3)
&&(i==1||i==n||j==3)) Output;
{printf("1");} Enter n: 7
else 11111
{putchar(' ');} 1
} 1
printf("\n"); 1
} 1
getch();} 1
11111

(d)
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int n,i,j;
printf("Enter n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{for(j=1;j<=8;j++)
{ Output;
if((j==1||i==n||j==8||i==1||i==4) Enter n: 7
&&(j==1||i==1||i==n||i==4)) 11111111
{printf("1");} 1
else 1
{putchar(' ');} 11111111
} 1
printf("\n");} 1
getch();} 11111111

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 34 -

K. M. Nasim (Rusho) RUET’EEE’08


EEE-152__Programs & Their Outputs - 35 -

K. M. Nasim (Rusho) RUET’EEE’08

You might also like