Addition of 2 Numbers: C++

This video tutorial illustrates a simple program of adding two integer numbers.

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream .h>
#include<conio .h>
 
void main()
{
 
 int a, b;
 clrscr();
 
 cout< <"Enter two no\n";
 cin>>a>>b;
 
 int sum = a + b;
 
 cout< <endl<<"Sum is: "<<sum;
 
 getch();
 
}

We can declare variables anywhere in a cpp program.

Video Tutorial: Addition of 2 Integer Numbers


[youtube https://www.youtube.com/watch?v=GBbOJZLkBBA&ap=%2526fmt%3D22]

YouTube Link: https://www.youtube.com/watch?v=GBbOJZLkBBA [Watch the Video In Full Screen.]



In this program we take input from the user, add it and then echo the result back to the screen.

Output
Enter two no
100
200

Sum is: 300

Colorful Text Output: C program

This video tutorial shows a simple method to get the output in different colors, on the dosprompt: using a simple C program.

Video Tutorial: Colorful Text Output: C program


[youtube https://www.youtube.com/watch?v=TpasN_6RMMM]

YouTube Link: https://www.youtube.com/watch?v=TpasN_6RMMM [Watch the Video In Full Screen.]



Full Source code: Colorful Text Output: C program C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include< stdio.h> /* Leave No space b/w < and stdio.h */ 
void main()
{
int i;
clrscr();
 
for( i=0; i<=15; i++ )
{
textcolor(i);
cprintf("Microsoft, Google, Yahoo, Oracle, eBay, PayPal \r\n");
}
 
getch();
 
}

textcolor() function takes color code as it’s parameter, and this color is taken by cprintf() function and the parameter(string) passed to cprintf is printed in the color given by textcolor() function.

Color Name
———-
Color code
———-
BLACK
BLUE
GREEN
CYAN
RED
MAGENTA
BROWN
LIGHTGRAY
DARKGRAY
LIGHTBLUE
LIGHTGREEN
LIGHTCYAN
LIGHTRED
LIGHTMAGENTA
YELLOW
WHITE
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

OutPut:
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal
Microsoft, Google, Yahoo, Oracle, eBay, PayPal

Write Data To Text File In C: Character by Character

This video tutorial demonstrates the creation of a simple file and writing data to text file ‘character by character’ using a simple C program.

  #include< stdio.h>

is the only header file required to include.

DOSBOX
I’m using a software called DOSBOX to write my c program. Using this software we can run 32-bit programs on our 62-bit machine.
I’m mounting my C:/DOSFOLDER here. So C:/DOSFOLDER will be treated as my C drive.

Here is the DOS code to do that:

z:\> mount c "c:/DOSFOLDER"
z:\>c:
c:\>cd TC
c\TC:>tc

We take a pointer variable of FILE type, so that this variable can be used to further reference to the file.

FILE *p;
char ch;

ch is a char type variable to store user entered characters and to transfer it to the file.

  p = fopen("c:\\Hello.txt","w");

fopen function takes two parameters. First one being the path of the file(if the file doesn’t exist, it creates one by itself), second parameter indicates the write mode. i.e., the file is opened in write mode and is intended for the write operation.

Note: If you are using DOSBOX software, then the file will be created at this location c:/DOSFOLDER/Hello.txt

       clrscr();
 
        printf("\nEnter the text nd hit Enter to Terminate\n");

clrscr() clears the screen and the print statement asks the user to enter some text and hit enter to terminate.

while( (ch=getche()) !='\r' )
fputc( ch,p);

getche() is a function that gets a character from the keyboard, echoes to screen.
These characters are stored in char variable ch. If the entered character is not “Enter Key” that is \r charriage return character, then the control enters the while loop and using fputc function it transfers the content(character) present in ch to p(variable which references the file we have opened for writing).

fclose(p);

closes the file, opened for write operation.

Video Tutorial: Creating and Writing Data To A Simple File: in C


[youtube https://www.youtube.com/watch?v=4V1qAw-XImI]

YouTube Link: https://www.youtube.com/watch?v=4V1qAw-XImI [Watch the Video In Full Screen.]



Full Source code to Creating and Writing Data To A Simple File: in C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include< stdio.h> /* no space b/w < and stdio.h */ 
void main()
{
FILE *p;
char ch;
 
p = fopen("c:\Hello.txt","w");
clrscr();
 
printf("\nEnter the text nd hit Enter to Terminate\n");
 
while( (ch = getche()) != '\r' )
 fputc( ch, p );
 
fclose(p);
}

Output:
Recruitment’s in C.

Simple Interest: C Program

Video Tutorial illustrating: C Program To Calculation of Simple Interest.

Header File:

   #include<stdio.h>

To be able to use printf function, we have to include stdio.h header file.

main()
This is the starting point of C program execution.
since it does not return any value, we write void main()

Declaration of Variables:

   int p, n;
float r, si;

variables p and n are integer type variables i.e., they can hold integer values.
variables r and si are float or real type variables i.e., they can hold fractional values.

   clrscr();

to clear the output screen.

Initialization of Variables:

   p = 1000;
n = 3;
r = 8.5;

Principle p = 1000
n or time or duration = 3
rate of interest r = 8.5

Formula to calculate simple interest:

     si = ( p * n * r ) / 100;

The mathematical formula si = ( p x n x r ) / 100; is written as si = ( p * n * r ) / 100;

Output statement:

printf("%f\n",si);

the function printf is present inside stdio.h header file. %f is formatting string for float or real type values.

getch();

function to return to the console window.

Video Tutorial: C Program to calculate Simple Interest


[youtube https://www.youtube.com/watch?v=TGgd6KvL_W0]

YouTube Link: https://www.youtube.com/watch?v=TGgd6KvL_W0 [Watch the Video In Full Screen.]



Full Source code to calculate simple interest in C program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
 
void main()
{
 
int p, n;
float r, si;
 
clrscr();
 
p = 1000;
n = 3;
r = 8.5;
 
si = ( p * n * r ) / 100;
 
printf("%f\n",si);
 
getch();
}

Output:
255.000000