Object

Write a program that prints the sum of two random numbers.

Algorithm

  1. Declare integer variables a, b and sum.
  2. a = rand()%8.
  3. b = rand()%9.
  4. sum = a + b.
  5. Write sum.
  6. Exit.

Flowchart

C++ Source Code

// program 72
#include<iostream>
using namespace std;

int main()
{
  int a, b, sum;

  a = rand() % 8;	// in the range 0 to 7
  b = rand() % 9;	// in the range 0 to 8
  sum = a + b;

  cout << a << " + " << b << " = " << sum;

  return 0;
}

C Source Code

/*program 72*/
#include<stdio.h>
#include<stdlib.h>

int main()
{
  int a, b, sum;
  a = rand()%8;
  b = rand()%9;
  sum = a + b;
  printf("%d + %d = %d", a, b, sum);
  
  return 0;
}

Output

1 + 8 = 9

Design a site like this with WordPress.com
Get started