Sunday, 28 October 2018

Lesson 1: Basic C Programming Problem Sets

Program 1: Write a C program that calculates the sum of two input numbers, and 

display the result.

Algorithm:

Input

Enter two numbers (n1, n2)

Process

Compute the sum (sum = n1 + n2)

Output

Display the sum (sum)


Solution:

Source code

Output

#include <stdio.h>
main()
{
    int n1, n2, sum;

     clrscr();

     printf("\n Enter 1st number: ");
     scanf("%d", &n1);

     printf("\n Enter 2nd number: ");
     scanf("%d", &n2);

     sum = n1 + n2;

     printf("\n The sum is: %d", sum);
     getch();
}


Enter 1st number: 10

Enter 2nd number:  5


The sum is: 15


Program 2: Write a C program that calculates the Area of a sphere using this formula:

A = πr2, value of π is 3.1416.

Algorithm:

Input

Enter radius (r)

Process

Compute the Area (A = Pi * (r * r))

Output

Display the Area (A)

Solution:

Source code

Output

#include <stdio.h>
#define Pi 3.1416

main()
{
     float A, r;
     clrscr();


     printf("Enter value of radius: ");
     scanf("%f", &r);

     A = Pi * (r * r);

     printf("The area of Sphere is: %f", A);
     getch();


Enter value of radius: 1

The area of Sphere is: 3.1416

Explanation

We set a constant value for Pi by using #define command. 


Notice also the format specifier used in this program %f for variables with floating data type.



Program 3: Write a program that computes the average of three input quizzes, then 

display the result.

Algorithm:

Input

Enter three quizzes (q1, q2, q3)

Process

Compute the average (ave = (q1 + q2 + q3)/3)

Output

Display the average (ave)

Solution:

Source code

Output

#include <stdio.h>
main()
{
  int q1, q2, q3;
  float ave;
  clrscr();
  printf("Enter three quizzes: ");
  scanf("%d %d %d", &q1, &q2, &q3);
  ave = (q1 + q2 + q3)/3;
  printf("The average is: %f", ave);
  getch();


Enter three quizzes: 90 90 90

The average is: 90

Explanation

The variable ave was declared of data type float to handle quotient values


We call this an assignment statement which stores a value or a computational result in a variable. They are commonly used to perform most arithmetic operations in  a program.

     ave = (q1 + q2 + q3)/3;








Program 4: Write a program that will compute and display the sum and product of two numbers.

Algorithm:

Input

Enter First and Second Number (num1, num2)

Process

Compute the sum and product

Sum=num1+num2

Product = num1*num2

Output

Display the sum and product

Solution:

Source code

Output

#include <stdio.h>
main()
{
int num1, num2, sum, product;
clrscr();
printf("\nFirst Number: ");
scanf("%d", &num1);

printf("\nSecond Number: ");
scanf("%d", &num2);


printf(“\nSum of two numbers: %d”, sum=num1+num2);


printf(“\nProduct of two numbers: %d”, product=num1*num2);

getch();


First Number: 4

Second Number: 5


Sum of two numbers: 9

Product of two numbers: 20


Explanation

Assignment statement can also be used in printf() statement.


printf(“\nSum of two numbers: %d”, sum=num1+num2);


printf(“\nProduct of two numbers: %d”, product=num1*num2);



Program 5: Write a program that accepts and display person’s name and age.

Algorithm:

Input

Enter Person’s Name and Age

Process

Read Person’s Name and Age

Output

Display Person’s Name and Age

Solution:

Source code

Output

#include <stdio.h>
main()
{
int age;

char name[10];

clrscr();
printf("\nEnter Name: ");
scanf("%s", &name);

printf("\nEnter Age: ");
scanf("%d", &age);

printf(“\nWelcome %s, you are %d years old”, name, age);

getch();


Enter Name: Juan

Enter Age: 16


Welcome Juan, you are 16 years old

Explanation


Simple use of %s format specifier using string of characters.

Structure of C Program and Format Specifiers



Structure of a C program


<#include directive>

<#define directive>

main()

{

<variable declaration section>

Statements

}


  1. #include directive – “contains information needed by the program to ensure the correct operation of Turbo C’s standard library functions."

Example: #include <stdio.h>


  1. #define directive – “used to shorten the keywords in the programs.”

Example: #define g gotoxy


  1. Variable declaration section – “it is the place where you declare your variable.”


  1. Body of the program – “ start by typing main() and the { and } (open and close braces). All statements should be written inside the { and } braces.”


NOTE: Turbo C is a case-sensitive program, use lowercase letters only.


Commonly used include files in C language

  1. alloc.h – declares memory management functions

  2. conio.h – declares various functions used in calling IBM-PC ROM BIOS

  3. ctype.h – contains information used by the classification and character conversion macros.

  4. math.h – declares prototype for the math functions.

  5. stdio.h – defines types and macros needed for standard I/O.

  6. string.h – declares several string manipulation and memory manipulation routines.


Basic Input/ Output Statements of C


Usually, our program involves three main operations. The first one is the input operation that uses input functions such as scanf, getch(), gets(), getche(), getchar() and others. The second is the process operation. In this part of our program, we can see some equations that are calculated, conditions that are evaluated, and tasks being performed. The third part is the output operation. Here, we use output statements such as printf() function, puts(), putch(), putchar functions and other output statement functions. To learn this technique, we can jump right in and type our first program that involves these three main operations.



FORMAT SPECIFIERS 

All format specifiers start with a percent sign(%) and are followed by a single letter indicating the type of data and how data are to be formatted.

Format Specifier

Use

Sample Usage

%c

Used for single character

scanf(“%c”, &ch);

printf(“%c”,ch);

%d

Decimal number(whole number)

scanf(“%d”, &num);

printf(“%d”,num);

%e

Scientific notation/exponential form

scanf(“%e”, &result);

printf(“%e”,result);

%f

Number with floating or decimal point

scanf(“%f”, &pesos);

printf(“%f”,pesos);

%o

Octal number

scanf(“%o”, &value);

printf(“%o”,value);

%s

String of characters

scanf(“%s”, &str);

printf(“%s”,str);

%u

Unsigned number

scanf(“%c”, &nos);

printf(“%c”,nos);

%x

Hexadecimal number

scanf(“%x”, &value);

printf(“%x”,value);

%X

Capital number for hexadecimal number

scanf(“%X”, &nos);

printf(“%X”,nos);

%%

Print a percent sign

scanf(“%%”, &value);

printf(“%%”,value);