Chapter 3 the simplest C program design -- sequential program design

Chapter 3 the simplest C program design -- sequential program design

Example 3.1 convert the temperature in Fahrenheit method to that in Celsius method

#include<stdio.h>
 int main()
 {
   float f,c;
   f=64.0;
   c=(5.0/9)*(f-32);
   printf("f=%f\nc=%f\n",f,c);
   return 0;
 } 

The operation results are as follows:

Example 3.2 calculation of deposit interest

 #include <stdio.h>
 int main()
 {
   float p0=1000,r1=0.0036,r2=0.0225,r3=0.0198,p1,p2,p3;
   p1=p0*(1+r1);
   p2=p0*(1+r2);
   p3=p0*(1+r3/2)*(1+r3/2);
   printf("p1=%f\np2=%f\np3=%f\n",p1,p2,p3);
   return 0;
 }

The operation results are as follows:

Example 3.3 given an uppercase letter, it is required to output it in lowercase

#include<stdio.h>
int main()
{
  char c1,c2;
  c1='A';
  c2=c1+32;
  printf("%c\n",c2);
  printf("%d\n",c2);
  return 0;
}

The operation results are as follows:

Example 3.4 gives the side length and area of the triangle

#include<stdio.h>
#include<math.h>
int main()
{
  double a,b,c,s,area;
  a=3.67;
  b=5.43;
  c=6.21;
  s=(a+b+c)/2;
  area=sqrt(s*(s-a)*(s-b)*(s-c));
  printf("a=%f\tb=%f\tc=%f\n",a,b,c);
  printf("area=%f\n",area);
  return 0;
}

The operation results are as follows:

Example 3.5 find the root of the equation ax2+bx+c=0. a,b,c input by keyboard

#include<stdio.h>
#include<math.h>
int main()
{
  double a,b,c,disc,x1,x2,p,q;
  scanf("%lf%lf%lf",&a,&b,&c);
  disc=b*b-4*a*c;
  p=-b/(2.0*a);
  q=sqrt(disc)/(2.0*a);
  x1=p+q;
  x2=p-q;
  printf("x1=%7.2f\nx2=%7.2f\n",x1,x2);
  return 0;
}

The operation results are as follows:

Example 3.6 output real number with% f, only 6 decimal places can be obtained

#include<stdio.h>
int main()
{ 
  double a=1.0;
  printf("%f\n",a/3);
  return 0;
}

The operation results are as follows:

Example 3.7 effective digits of float data

#include<stdio.h>
int main()
{
  float a;
 a=10000/3.0;
 printf("%f\n",a);
 return 0;
}

The operation results are as follows:

Example 3.8 successively output three characters of BOY

#include<stdio.h>
int main()
{
  char  a='B',b='O',c='Y';
  putchar(a);
 putchar(b);
 putchar(c);
 putchar('\n');
 return 0;
}
#include<stdio.h>
int main()
{
 int a=66,b=79,c=89;
 putchar(a);
 putchar(b);
 putchar(c);
 putchar('\n');
 return 0;
}

The operation results are as follows:

Example 3.9 input three characters of BOY from the keyboard and output them to the screen

 #include<stdio.h>
  int main()
 {
  char a,b,c;
  a=getchar();
  b=getchar();
  c=getchar();
  putchar(a);
  putchar(b);
  putchar(c);
  putchar('\n');
   return 0;
 }

The operation results are as follows:

Example 3.10 input an uppercase letter from the keyboard and display the corresponding lowercase letter on the display screen

#include<stdio.h>
int main()
{
 char c1,c2;
 c1=getchar();
 c2=c1+32;
 putchar(c2);
 putchar('\n');
 return 0;
}
#include<stdio.h>
int main()
{
 char c1,c2;
 c1=getchar();
 c2=c1+32;
 printf("Capital:%c\n Lowercase letters:%c\n",c1,c2);
 return 0;
}

The operation results are as follows:

Posted by elum.chaitu on Thu, 05 Dec 2019 14:25:58 -0800