C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Exercise 3

C Programming

In this exercise section of the tutorial we will write some C program.

Write a program in C to compute (a + b)2 where the value of a and b will be provided by the user

The formula of (a + b)2 is given below.

(a + b)2 = a2 + b2 + 2ab

Now we will convert this into a valid C expression.

Let x = (a + b)2
      = a2 + b2 + 2ab
      = (a * a) + (b * b) + (2 * a * b)

Now we will write the code.

/**
 * file: algebra.c
 * author: yusuf shakeel
 * date: 2010-12-15
 * description: algebra program
 */

#include <stdio.h>
int main(void)
{
  //variables
  float a, b, x;
  
  //user input
  printf("Enter value of a: ");
  scanf("%f", &a);
  
  printf("Enter value of b: ");
  scanf("%f", &b);
  
  //compute
  x = (a * a) + (b * b) + (2 * a * b);
  
  //output
  printf("Result = %f\n", x);
  
  printf("End of code\n");
  return 0;
}

Output

Enter value of a: 4
Enter value of b: 2
Result = 36.000000
End of code

Write a program in C to compute (a + b)3 where the value of a and b will be provided by the user

Try to solve this problem yourself.

Help:

Let x = (a + b)3
      = a3 + b3 + 3a2b + 3ab2

Write a program in C to compute Simple Interest based on user input

To find Simple Interest we will need the Principal, Rate of Interest and Time.

Click here to learn more about Simple Interest.

To keep things simple we will assume Time is in years and an integer value whereas, Principal and Rate of Interest are float type.

Formula for Simple Interest is given below.

SI = PRT/100
   = (P * R * T) / 100

where,
P = Principal
R = Rate of Interest
T = Time

Now, lets write the C code.

/**
 * file: simple-interest.c
 * author: yusuf shakeel
 * date: 2010-12-15
 * description: simple interest program
 */

#include <stdio.h>
int main(void)
{
  //variables
  float si, p, r;
  int t;
  
  //user input
  printf("Enter P: ");
  scanf("%f", &p);
  
  printf("Enter R: ");
  scanf("%f", &r);
  
  printf("Enter T: ");
  scanf("%d", &t);
  
  //compute
  si = (p * r * t) / 100;
  
  //output
  printf("SI = %f\n", si);
  
  printf("End of code\n");
  return 0;
}

Output

Enter P: 1000
Enter R: 10
Enter T: 2
SI = 200.000000
End of code