Generic Form of a 4 digit number
Generic Form of a 4 digit number - C Programming Example
This program is an example to figure out the generic form of a 4 digit number through C Programming.
C Programming Code
#include<stdio.h>
#include<conio.h>
main()
{
int num,a,b,c,d,e;
printf("Input a 4 digit number \n");
scanf("%d",&num);
a=num/1000;
num=num-a*1000;
b=num/100;
num=num-b*100;
c=num/10;
num=num-c*10;
d=num;
e=a+b+c+d;
printf("The generic form of the 4 digit number is %d", e);
getch();
}
Output of code
Explanation of code
· #include<stdio.h>
#include<conio.h>
Ø This are called header file.A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source filesThe C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header <stdio.h>.In the other hand <conio.h> is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C. This header declares several useful library functions for performing "console input and output" from a program.
· main()
Ø In C, program execution starts from the main() function. The main function can in-turn call other functions. When main calls a function, it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of function is reached.
· int num,a,b,c,d,e;
Ø int is a variable or data type. A variable declared to be of type int can be used for storing numbers which don’t have decimal points. Here we declare “num,a,b,c,d,e”variable in int type.
· printf() function
Ø C uses printf() function to write from the input devices. This function has been declared in the header file called stdio.h . Any text written within the pair of quotes ("") is displayed as such by printf() function on the screen.
· scanf() function
Ø This function is used to get input from the user of the program. scanf(“%d”,&num”) is used here. So, this program will read in a int value for “num” variable that the user enters on the keyboard (%d is for int values. As there is “&num”, the number user enters will be the value of “num” variable).
a=num/1000;
num=num-a*1000;
b=num/100;
num=num-b*100;
c=num/10;
num=num-c*10;
d=num;
This part of the code is simple, one just needs a bit patience to understand it. We know that generic form means the summation of the digits within a number, or in this case a 4 digit number.
Firstly let’s take a number such as 1234.
Then a = num/1000 as this data variable type is integer in value that’s why a will only scan and save 1 in it, not 1.234 the decimal point number.
Then again num= num-a*1000 here,
Num= 1234-1*1000
So, num= 1234-1000 or num=234
Then again the same processes are repeated where b=2,c=3 and finally d= num as then the value of num will become 4 in this case.
In such a way, any digits from a 4 digit number can be sequentially extracted and then finally
e= a=b+c+d
Here e is the summation of the digits within the 4 digit number or in other words the generic form of the number.
printf("The generic form of the 4 digit number is %d", e);
getch();
Then finally the output is shown in printf and finally getch(); is written to end the program as we used <conio.h> as header.
No comments