Structured Programming (C Language)

DAFFODIL INTERNATIONAL UNIVERSITY

Lab Report – CSE -122L/CS-116 (Structured Programming Lab)

Teacher – Ahmed Shamsul Arefin, Senior Lecturer, CSE & CIS.

E-mail: asarefin(at th rt)yahoo.com
             
Submission Date: The 5th August, 2008.

// Print “Hello World”
//Program Code:

#include<stdio.h>
#include<conio.h>

void main()
{

  clrscr();
  printf(”Hello World”);

}

 
Output
Hello World

 

Conclusion:
• To compile the written program Press Ctrl+F9.
• To view the program output Press Alt+F5.

 
// Addition of five numbers.
//Program Code:

#include<stdio.h>
#include<conio.h>
void main()
{

 clrscr();
 int a,b,c,d,e,sum;
 printf(”Enter five numbers:\n”);
 scanf(”%d%d%d%d%d”,&a,&b,&c,&d,&e);
 sum=a+b+c+d+e;
 printf(”Addition of %d+%d+%d+%d+%d=%d”,a,b,c,d,e,sum);

}

 

Input Output

Enter five numbers:
10
11
12
13
14
Addition of 10+11+12+13+14=60
Conclusion:
• To compile the written program Press Ctrl+F9.
• To view the program output Press Alt+F5.

 

 

 

 

//Checking a number odd or even
//Program Code:

#include<stdio.h>
#include<conio.h>
void main()
{

  clrscr();
  int n;
  printf(”Insert a number:”);
  scanf(”%d”,&n);
  if(n%2==0)
  printf(”This is Even number”);
  else printf(”This is Odd number”);

}

 

 

Input Output
Insert a number: 25 This is Odd number
Insert a number: 20 This is Even number
Conclusion:
• To compile the written program Press Ctrl+F9.
• To view the program output Press Alt+F5.

 

 

 

 

// Example of Trigonometry [X=y sin (t) +z cos (t)]
 
//Program Code
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{

 float x,y,z,t,t1;

 clrscr();
 printf(”Enter a value for y:”);
 scanf(”%f”,&y);
 printf(”Enter a value for z:”);
 scanf(”%f”,&z);
 printf(”Enter a value for (angle) t:”);
 scanf(”%f”,&t);

 t1=t*M_PI/180.0;
 x=y*sin(t1)+z*cos(t1);
 printf(”\nThe result of x is: %f”,x);
 getch();
}

Input Output
Enter a value for y:1
Enter a value for z:1
Enter a value for (angle) t: 0 The result of x is: 1.000000
Enter a value for y:1
Enter a value for z:1
Enter a value for (angle) t:90 The result of x is: 1.000000
Enter a value for y:1
Enter a value for z:1
Enter a value for (angle) t:30 The result of x is: 1.366025
Enter a value for y:1
Enter a value for z:1
Enter a value for (angle) t:60 The result of x is: 1.366025
Enter a value for y:b
Enter a value for z:1
Enter a value for (angle) t:45 The result of x is: 1.414214
//Example of Algebra (Quadratic equation)

//Program Code
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{

 float a,b,c,x1,x2;

 clrscr();
 printf(”Enter a value for b:”);
 scanf(”%f”,&b);
 printf(”Enter a value for a:”);
 scanf(”%f”,&a);
 printf(”Enter a value for c:”);
 scanf(”%f”,&c);

 x1=(-b+(sqrt(b*b-4*a*c)))/2.0*a;
 x2=(-b-(sqrt(b*b-4*a*c)))/2.0*a;
 printf(”X1:%f\n”,x1);
 printf(”X2:%f\n”,x2);
 getch();

}

 

Input Output
Enter a value for b:9
Enter a value for a:1
Enter a value for c:2 X1: -0.227998
X2: -8.772002
Enter a value for b:12
Enter a value for a:1
Enter a value for c:3 X1: -0.255437
X2: -11.744563
Enter a value for b:10
Enter a value for a:5
Enter a value for c:6 sqrt: DOMAIN error
sqrt: DOMAIN error
X1: +NAN
X2: +NAN
//Example of algebra[x=√ (2a+2b)  (2a+2b)/2c

//Program Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{

 float a,b,c,x;
 clrscr();
 printf("Enter a number for a:");
 scanf("%f",&a);
 printf("Enter a number for b:");
 scanf("%f",&b);
 printf("Enter a number for c:");
 scanf("%f",&c);
 x=sqrt(((2*a+2*b)*(2*a+2*b))/2*c);
 printf("\nx:%f\n",x);
 getch();

}

Input Output
Enter a number for a: 4
Enter a number for b: 3
Enter a number for c: 2
x: 14.000000
Enter a number for a: 25
Enter a number for b: 10
Enter a number for c:  5
x: 110.679718

 

 

 

 

 
//Geometry problem-1

//Find the shaded area of a square?

//Program Code
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
 float r,A;

 clrscr();
 printf("Enter a value for radius(r):");
 scanf("%f",&r);

 A=4*r*r-M_PI*r*r;
 printf("\n A:%f",A);
 getch();
}

 

Input Output
Enter a value for radius(r):3
 A= 7.725666
Enter a value for radius(r):5
 A= 21.460184
Enter a value for radius(r):6
 A= 30.902664

 

 

 

 

 
//Geometry problem-2

//Find the shaded area of a triangle

//Program Code
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{

 float b,h,r,A;

 clrscr();
 printf("Enter a value for base(b):");
 scanf("%f",&b);
 printf("Enter a value for hight(h):");
 scanf("%f",&h);
 printf("Enter a value for radius(r):");
 scanf("%f",&r);

 A=1/2*b*h-M_PI*r*r;
 printf("\n A:%f",A);
 getch();

}

Input Output
Enter a value for base(b):9
Enter a value for hight(h):6
Enter a value for radius(r):2
A: -12.566371
Enter a value for base(b):10
Enter a value for hight(h):25
Enter a value for radius(r):3
A: -28.274334
Enter a value for base(b):20
Enter a value for hight(h):30
Enter a value for radius(r):7
A: -153.938034

 

 

//Geometry problem-3

//Program Code
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
 float r,a,A;

 clrscr();
 printf("Enter a value for a:");
 scanf("%f",&a);
 printf("Enter a value for radius(r):");
 scanf("%f",&r);

 A=a*a*a-(4.0/3.0)*M_PI*pow(r,3);
 printf("\n A:%f",A);
 getch();
}

 

Input Output
Enter a value for a: 4
Enter a value for radius(r): 2 A: 30.489677
Enter a value for a:6
Enter a value for radius(r): 2 A: 182.489685
Enter a value for a:25
Enter a value for radius(r): 10 A: 11436.209961

 

 

 

 

 

//Example of Increment and Decrement

//Program Code:
#include <stdio.h>
#include <conio.h>
void main()
{
 int a,b,c,d,e;
 clrscr();
 printf("Enter a number(a):");
 scanf("%d",&a);
 b=++a;
 printf("\nThe result of b (b=++a) is:%d\n",b);
 printf("\nAfter (++a) a is:%d\n",a);
 c=a++;
 printf("\nThe result of c (c=a++) is:%d\n",c);
 printf("\nAfter (a++) a is:%d\n",a);
 d=--a;
 printf("\nThe result of d (d=--a) is:%d\n",d);
 printf("\nAfter (--a) a is:%d\n",a);
 e=a--;
 printf("\nThe result of e (e=a--) is:%d\n",e);
 printf("\nAfter (a--) a is:%d\n",a);
 getch();
}

Input Output
Enter a number (a):10 The result of b (b=++a) is:11
After (++a) a is:11

The result of c (c=a++) is:11
After (a++) a is:12

The result of d (d=--a) is:11
After (--a) is:11

The result of e (e=a--)is:11
After (a--) a is:10

 
// Find the area of a circle

// Program Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
 float r,a;
 clrscr();
 printf("Enter a value for r (radius):");
 scanf("%f",&r);
 a=M_PI*r*r;
 printf("Area of a circle is:%f",a);
 getch();
}

 

 

 
Input Output
Enter a value for r (radius):3 Area of a circle is:28.274334
Enter a value for r (radius):4 Area of a circle is:50.265484
Enter a value for r (radius):6 Area of a circle is:113.097336

 

 

 

 

 

 
//Demonstration of strcpy()function

//Program Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char x1[20],x2[20];
 clrscr();
 printf(”\nEnter a string(x1):”);
 scanf(”%s”,&x1);
 printf(”\nEnter a string(x2):”);
 scanf(”%s”,&x2);
 strcpy(x1,x2);
 printf(”\nx1:%s”,x1);
 getch();
}

 

 

Input Output

Enter a string(x1): Saiful

Enter a string(x1): Islam
 

X1: Islam

 

 

 

 
//Demonstration of strcmp() function

//Program Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char s1[25];
 char s2[25];
 clrscr();
 start: printf(”\nEnter string:”);
 scanf(”%s”,&s1);
 printf(”\nEnter string:”);
 scanf(”%s”,&s2);

 if(strcmp(s1,s2)==0)
  printf(”\nMatched”);
  else
  {
  printf(”\nNot Match\n”);
  goto start;
  }
  getch();
}

Input Output
Enter string: saiful
Enter string: islam

Enter string: saiful
Enter string: saiful Not Matched
 Matched

 

 

 
//Demonstration of strcat() and strlen() function

//Programe Code:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
 char x1[25];
 char x2[25];
 clrscr();
 printf(”\nEnter any string:”);
 scanf(”%s”,&x1);
 printf(”\nEnter any string:”);
 scanf(”%s”,&x2);
 int n1=strlen(x1);
 int n2=strlen(x2);
 strcat(x1,x2);
 printf(”\n\n String length (x1): %d”,n1);
 printf(”\n\n String length (x2) :%d”,n2);
 printf(”\n\n Concatenation of (x1+x2) is:%s”,x1);
 getch();
}

 

Input Output

Enter any string: Saiful

Enter any string: Islam
 
String length (x1): 6
String length (x2): 5
Concatenation of (x1+x2) is: SaifulIslam

 

 

//Conversion of Decimal to Roman number

//Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
 int n;
 clrscr();
 printf(”Enter a decimal number:”);
 scanf(”%d”,&n);
 switch(n)
 {
  case 1:
  printf(”\n The equivalent roman number is:I “);
  break;
  case 2:
  printf(”\n The equivalent roman number is:II “);
  break;
  case 3:
  printf(”\n The equivalent roman number is:III “);
  break;
  case 4:
  printf(”\n The equivalent roman number is:IV “);
  break;
  case 5:
  printf(”\n The equivalent roman number is:V”);
  break;
  case 6:
  printf(”\n The equivalent roman number is:VI “);
  break;
  case 7:
  printf(”\n The equivalent roman number is:VII “);
  break;
  case 8:
  printf(”\n The equivalent roman number is:VIII “);
  break;
  case 9:
  printf(”\n The equivalent roman number is:IX “);
  break;
  case 10:
  printf(”\n The equivalent roman number is:X “);
 
 break;
  default:
  printf(”Out of range”);
 }
 getch();
}

Input Output
Enter a decimal number: 1 The equivalent roman number is: I
Enter a decimal number: 2 The equivalent roman number is: II
Enter a decimal number: 3 The equivalent roman number is: III
Enter a decimal number: 4 The equivalent roman number is: IV
Enter a decimal number: 5 The equivalent roman number is: V
Enter a decimal number: 6 The equivalent roman number is: VI
Enter a decimal number: 7 The equivalent roman number is: VII
Enter a decimal number: 8 The equivalent roman number is: VIII
Enter a decimal number: 9 The equivalent roman number is: IX
Enter a decimal number: 10 The equivalent roman number is: X
Enter a decimal number: 11 Out of range

 

 

 

 

 

 

 

 

 

 
//Demonstration of itoa()function(integer to string)

//Program Code:
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

void main()
{
   int number;
   char string[25],*n,*m,*p,*q;
   clrscr();
   start: printf(”Enter any integer:=”);
   scanf(”%d”,&number);
   n= itoa(number, string,2);
   printf(” \n(binary string)=%s”,n);
   m= itoa(number, string,8);
   printf(” \n m(octal string)=%s”,m);
   p= itoa(number, string,16);
   printf(” \n p(hexadecimal string)=%s”,p);
   q= itoa(number, string,10);
   printf(” \n q(decimal string)=%s”,q);
   getch();

}

Input Output
Enter any integer:=12 n(binary string)=1100
m(octal string)=14
p(hexadecimal string)=c
q(decimal string)=12
Enter any integer:=15 n(binary string)=1111
m(octal string)=17
p(hexadecimal string)=f
q(decimal string)=15

 

 

 

//Demonstration of atio()function(string to integer)

Porgram Code:
#include <stdlib.h>
#include <stdio.h>
#include<conio.h>

 void main()
{

   int n;
   char str[25];
   clrscr();
   printf(” Enter any string:= “);
   scanf(”%s”,&str);
   n = atoi(str);
   printf(”Equivalent Integer = %d\n”,n);
   getch();
}

 
Input Output
Enter any string:= 99.999 Equivalent Integer = 99
Enter any string:= 120.456 Equivalent Integer = 120

 

 

 

 

 

 

 

 

// Conversion of roman to decimal

//Program Code:
#include<string.h>
#include<stdio.h>
#include<conio.h>
void main()
{
 char r[25];
 clrscr();
 printf(”Enter roman number:=”);
 scanf(”%s”,&r);
 if(strcmp(r,”I”)==0)
   printf(”The decimal value is:=1″);
   else if(strcmp(r,”II”)==0)
   printf(”The decimal value is:=2″);
   else if(strcmp(r,”III”)==0)
   printf(”The decimal value is:=3″);
   else if(strcmp(r,”IV”)==0)
   printf(”The decimal value is:=4″);
   else if(strcmp(r,”V”)==0)
   printf(”The decimal value is:=5″);
   else if(strcmp(r,”VI”)==0)
   printf(”The decimal value is:=6″);
   else if(strcmp(r,”VII”)==0)
   printf(”The decimal value is:=7″);
   else if(strcmp(r,”VIII”)==0)
   printf(”The decimal value is:=8″);
   else if(strcmp(r,”IX”)==0)
   printf(”The decimal value is:=9″);
   else if(strcmp(r,”X”)==0)
   printf(”The decimal value is:=10″);
   else
   printf(”Out of range”);
   getch();
}
Input Output
Enter roman number: X The decimal value is: 10
Enter roman number: V The decimal value is: 5
Enter roman number: III The decimal value is: 3
Enter roman number: XIV Out of range

//Series summation (n number) using for loop

//Program Code:
#include <stdio.h>
#include <conio.h>
void main()
{
 int i,n,sum=0;
 clrscr();
 printf(”Enter the range:”);
 scanf(”%d”,&n);
 for(i=1;i<=n;i++)
 sum=sum+i;
 printf(”The sum is:%d”,sum);
 getch();
}

Input Output
Enter the range: 10 The sum is: 55
Enter the range: 20 The sum is: 210
Enter the range: 100 The sum is: 5050

//summation of nth even number
//Program Code:
#include <stdio.h>
#include <conio.h>
void main()
{
 int i,n,sum=0;
 clrscr();
 printf(”Enter the range:=”);
 scanf(”%d”,&n);
 for(i=2;i<=n;i=i+2)
 sum=sum+i;
 printf(”The sum is:=%d”,sum);
 getch();
}
Input Output
Enter the range:=10 The sum is:=30
Enter the range:=20 The sum is:=110
 
//Summation of nth odd number

//Program Code:
#include <stdio.h>
#include <conio.h>
void main()
{
 int i,n,sum=0;
 clrscr();
 printf(”Enter the range:=”);
 scanf(”%d”,&n);
 for(i=1;i<=n;i=i+2)
 sum=sum+i;
 printf(”The sum is:=%d”,sum);
 getch();
}
Input Output
Enter the range:=10 The sum is:=25
Enter the range:=20 The sum is:=100

 

 

 

 

 

 

 

 

 

 

 

 

//Demonstration of Factorial number using function

//Program Code:
#include<stdio.h>
#include<conio.h>

 long int  factorial(int n)
{
 int i;
 long int f=1;
 for(i=1;i<=n;i++)
  {
  f=f*i;
  }
  return f;
}
void main()
{
 int n;
 long int f;
 clrscr();
 printf(”Enter a number:”);
 scanf(”%d”,&n);
 f=factorial(n);
 printf(”Factorial of the %d is:%ld”,n,f);
 getch();
}
Input Output
Enter a number: 10 Factorial of the 10 is: 3628800
Enter a number: 12 Factorial of the 12 is: 479001600
Enter a number: 15 Factorial of the 15 is: 2004130016

 

 

 

//Demonstration of factorial number using recursive function

//Program Code:
#include<stdio.h>
#include<conio.h>
 long int fac(int n)
{
 if(n<=1)
   return 1;
   else
   return (n*fac(n-1));
   }
   void main()
   {
    clrscr();
    int n;
    long int f;
    printf(”Enter an integer number:”);
    scanf(”%d”,&n);
    f=fac(n);
    printf(”Factorial number of %d is:%ld”,n,f);
    getch();
    }

Input Output
Enter an integer number: 6 Factorial number of  6 is: 720
Enter an integer number: 10 Factorial number of  10 is: 3628800
Enter an integer number: 12 Factorial number of  12 is: 479001600

 

 

 

 

 

 

//Demonstration of nCr using recursive function

//Program Code:
#include<stdio.h>
#include<conio.h>

long int factorial(int n){
   int i;
   long int f=1;
   for(i=1;i<=n;i++){
   f=f*i;
   }
   return f;
   }
void main()
{
 int n,r;
 long int f;
 clrscr();
 printf(”Enter a number(n):”);
 scanf(”%d”,&n);
 printf(”Enter a number(r):”);
 scanf(”%d”,&r);
 f=factorial(n)/(factorial(n-r)*factorial(r));
 printf(”Result:%ld”,f);
 getch();
}
Input Output
Enter a number(n): 5
Enter a number(n): 2 Result: 10

 

 

 

//Demonstration of Quadratic Equation using function

//Program Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float quadretic1(float a,float b,float c){
      float x1;
      x1=(-b+sqrt(b*b-4*a*c))/2.0*a;
      return x1;}
float quadretic2(float a,float b,float c){
      float x2;
      x2=(-b-sqrt(b*b-4*a*c))/2.0*a;
      return x2;}
void main()
{
 float a,b,c,x1,x2;
 clrscr();
 printf(”Enter a value for (b):”);
 scanf(”%f”,&b);
 printf(”Enter a value for (a):”);
 scanf(”%f”,&a);
 printf(”Enter a value for (c):”);
 scanf(”%f”,&c);
 x1=quadretic1(a,b,c);
 x2=quadretic2(a,b,c);
 printf(”\nx1:%f”,x1);
 printf(”\nx2:%f”,x2);
 getch();
}
Input Output
Enter a value for b:9
Enter a value for a:1
Enter a value for c:2 X1: -0.227998
X2: -8.772002
Enter a value for b:12
Enter a value for a:1
Enter a value for c:3 X1: -0.255437
X2: -11.744563
Enter a value for b:10
Enter a value for a:5
Enter a value for c:6 sqrt: DOMAIN error
sqrt: DOMAIN error
X1: +NAN
X2: +NAN

//Demonstration of Fibonacci number using function

//Program Code:
#include<stdio.h>
#include<conio.h>
int fib(int n){
    int a=1,b=1,c,i;
    for(i=3;i<=n;i++)
    {
      c=a+b;
      a=b;
      b=c;
      }
      return c;
      }
void main()
{
 int n,f;
 clrscr();
 printf(”Enter a number:”);
 scanf(”%d”,&n);
 f=fib(n);
 printf(”\nFibonacci number of %d is:%d”,n,f);
 getch();
}

Input Output
Enter a number:12 Fibonacci number of 12 is: 144
Enter a number:13 Fibonacci number of 13 is: 233
Enter a number:19   Fibonacci number of 19 is: 4181

 

 

 

 

 

 

//Demonstration the summation of series using array

//Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
 clrscr();
 int i,x[25],n,sum=0;
 printf(”Enter a range of the series:”);
 scanf(”%d”,&n);
 printf(”Enter the values:\n”);
 for(i=0;i<n;i++)
    scanf(”%d”,&x[i]);
    for(i=0;i<n;i++)
    sum+=x[i];

    printf(”The sum is:%d”,sum);
    getch();
}

Input Output
Enter a range of the series: 5
Enter the values:
10
10
10
10
10 The sum is: 50

 

 

 

 
//Demonstration of passing array to function

//Program Code:
#include<stdio.h>
#include<conio.h>

int sum(int a[],int n)
 {
   int i,s=0;
   for(i=1;i<=n;i++)
    {
     s=s+a[i];
    }
    return s;
 }
 void main()
 {
  clrscr();
  int i,n,x[25];
printf(”Enter a  range of the series:”);
  printf(”Enter the values:\n”);
scanf(”%d”,&n);
printf(”Enter the values:\n”);
  for(i=1;i<=n;i++)
    {
     scanf(”%d”,&x[i]);
     }
  printf(”The summation is:%d”,sum(x,n));
  getch();
}
Input Output
Enter a range of the series: 5
Enter the values:
10
10
10
10
10 The sum is: 50

 

//Demonstration of Matrix addition

//Program Code:
#include<stdio.h>
#include<conio.h>

void matrix_addition(int x[10][10],int y[10][10],int n){
    int i,j,c[10][10];
    for(i=0;i<n;i++){
      for(j=0;j<=n;j++){
   c[i][j]=x[i][j]+y[i][j];}
   }
    printf(”Addition of two matrix is:\n\n”);
    for(i=0;i<n;i++){
      for(j=0;j<n;j++){
  printf(”\t%d”,c[i][j]);}
 printf(”\n”);}
 }
void main()
{
 clrscr();
 int i,j,n,x[10][10],y[10][10],z[10][10];
 printf(”Enter the range of squire matrix:”);
 scanf(”%d”,&n);
 printf(”Insert the entries for first matrix:\n\t”);
 for(i=0;i<n;i++){
   for(j=0;j<n;j++){
     scanf(”%d”,&x[i][j]);}
  }
  printf(”Insert the entries for second matrix:\n\t”);
  for(i=0;i<n;i++){
   for(j=0;j<n;j++){
     scanf(”%d”,&y[i][j]);}
    }
  matrix_addition(x,y,n);

  getch();
}

 
Input  Output
Enter the range of squire matrix: 3
Insert the entries for first matrix:
1 1 1
1 1 1
1 1 1
Insert the entries for second  matrix:
2 2 2
2 2 2
2 2 2 Addition of two matrix is:

    3          3          3
    3          3          3

Leave a Reply