In this article, after going through all the fundamentals of C programming languages we will learn C basic programming examples.

Do not forget that the best way of learning programming is by practising and developing your own concept.

If you really want to learn to program, then you must have your own point of view because understanding the concepts is always different for different brains.

## Table of Contents

#### C Basic Program

We have already learnt about the Input and Output functions in C previously and learn a very basic program of printing “Hello World“.

Now, we will dig into some more basic programs and move forward towards advanced topics.

**Program-1 WAP to enter two numbers and print their** **multiplication.**

```
#include <stdio.h>
int main()
{
int a, b, pro;
printf("Enter first number: "); // Asking user to enter 1st number
scanf("%d", &a);
printf("Enter second number: "); // Asking user to enter 2nd number
scanf("%d", &b);
// calculating product
pro = a * b;
printf("Product of %d * %d = %d", a, b, pro);
return 0;
}
```

**Output**

Enter first number: 5 Enter second number: 6 Product of 5 * 6 = 30

**Program-2 WAP to enter two numbers and print their** **quotient and remainder.**

```
#include <stdio.h>
int main()
{
int a, b, quo, rem;
printf("Enter first number(Dividend): ");
scanf("%d", &a);
printf("Enter second number(Divisor): ");
scanf("%d", &b);
// calculating Quotient
quo = a / b;
printf("Quotient = %d", quo);
// calculating Remainder
rem = a % b;
printf("\nRemainder = %d", rem);
return 0;
}
```

**Output**

Enter first number(Dividend): 78 Enter second number(Divisor): 5 Quotient = 15 Remainder = 3

**Program-3 WAP to enter number and print their square**

```
#include <stdio.h>
int main()
{
int a, sq;
printf("Enter number: ");
scanf("%d", &a);
// calculating square
sq = a * a;
printf("Square = %d", sq);
return 0;
}
```

**Output**

Enter number: 8 Square = 64

**Note:-**Similarly to calculate cube of any number, just multiply the same number 3 times (a*a*a)

**Program-4 WAP to enter two numbers and swap their values without using third variable**

```
#include <stdio.h>
int main()
{
int a, b;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
printf("Values before swapping\n");
printf("a= %d & b=%d", a,b);
// Swaping values
a=a+b;
b=a-b;
a=a-b;
printf("\nValues after swapping");
printf("\na= %d & b=%d", a,b);
return 0;
}
```

**Output**

Enter first number: 9 Enter second number: 6 Values before swapping a= 9 & b=6 Values after swapping a= 6 & b=9

**Program-5 WAP that inputs a student’s marks in three subjects (out of 100) and prints the average marks**

```
#include <stdio.h>
int main()
{
int eng,hindi, math, sum;
float avg ;
printf("Enter marks in English: ");
scanf("%d", &eng);
printf("\nEnter marks in Hindi: ");
scanf("%d", &hindi);
printf("\nEnter marks in Mathematics: ");
scanf("%d", &math);
sum=eng+hindi+math;
avg=sum/3;
printf("\nAverage marks =%f", avg);
return 0;
}
```

**Output**

Enter marks in English: 89 Enter marks in Hindi: 98 Enter marks in Mathematics: 92 Average marks =93.000000

**Program-6 WAP that accepts radius of circle and prints its area.**

```
#include <stdio.h>
int main()
{
int a;
float ar;
printf("Enter radius of the circle: ");
scanf("%d", &a);
// calculating area
ar = 3.14 * a * a;
printf("Area = %f", ar);
return 0;
}
```

**Output**

Enter radius of the circle: 5 Area = 78.500000

**Program-7 WAP to input principal, rate, time & calculate it’s simple interest & compound interest.**

```
#include <stdio.h>
#include<math.h>
int main()
{
int p, r, t,n;
float si, amt ;
printf("Enter Principal amount: ");
scanf("%d", &p);
printf("\nEnter rate of Inetrest: ");
scanf("%d", &r);
printf("\nEnter time: ");
scanf("%d", &t);
printf("\nEnter number of times ineterest is compounded: ");
scanf("%d", &t);
si=(p*r*t)/100;
printf("\nSimple Ineterest =%f", si);
amt=pow(p*(1+(r/n)), (n*t));
printf("\nCompound Interest= %lf", amt);
return 0;
}
```

* pow() function may not work in some online compiler and you will get the error (undefined reference to `pow’ error: ld returned 1 exit status*)

To run the program using GCC compiler use: gcc prg7.c -o test -lm where prg-7.c is the name of the file and test is the name of the file in which will be created after executing the command.

Then the test file has to be executed for the output using the command $ ./test

**Output**

Enter Principal amount: 500 Enter rate of Inetrest: 5 Enter time: 1 Enter number of times ineterest is compounded: 2 Simple Ineterest =50.000000 Compound Interest= 0.000000

**Program-8 WAP to input sides of a triangle & calculate it’s area by hero’s formula.**

Area of any triangle =sqrt(s*(s-sideOne)*(s-sideTwo)*(s-sideThree))

Where s = (sideOne + sideTwo + sideThree)/2

```
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, s, area;
printf("Enter the length of three sides of triangle\n");
scanf("%f %f %f", &a, &b, &c);
s = (a + b + c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of triangle : %0.4f\n", area);
return 0;
}
```

**Output**

Enter the length of three sides of triangle 5 6 7 Area of triangle : 14.6969

**Program-9 WAP to input a number and print its cube and cube root.**

Area of any triangle =sqrt(s*(s-sideOne)*(s-sideTwo)*(s-sideThree))

Where s = (sideOne + sideTwo + sideThree)/2

```
#include<stdio.h>
#include<math.h>
void main()
{
int num,cube;
float ans;
printf("Enter any number: ");
scanf("%d",&num);
cube=num*num*num;
printf("Cube of a number=%d",cube);
ans=pow(num, 1.0/3.0);
printf("\nCube root of %d is: %f",num,ans);
return ;
}
```

**Output**

Enter any number: 8 Cube of a number=512 Cube root of 8 is: 2.000000

**Program-10 WAP to input a number and print its square root.**

```
#include<stdio.h>
#include<math.h>
void main()
{
int num;
float ans;
printf("Enter any number: ");
scanf("%d",&num);
ans=sqrt(num);
printf("\nSquare root of %d is: %f",num,ans);
return ;
}
```

**Output**

Enter any number: 9 Square root of 9 is: 3.000000

Try executing the program here.