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.

0 comments:

Post a Comment

If possible, leave a positive comment. No hate speech or elicit comments. Thank you.