C program print integer

This c program ask user to input an integer and then prints it. Input is done using scanf function and number is printed on screen using printf.

C programming code

#include <stdio.h>
 
int main()
{
  int a;
 
  printf("Enter an integer\n");
  scanf("%d", &a);
 
  printf("Integer that you have entered is %d\n", a);
 
  return 0;
}
Download integer program.
Output of program:
input number
In c language we have data type for different types of data, for integer data it is int, for character date char, for floating point data it's float and so on. For large integers you can use long or long long data type. To store integers which are greater than 2^18-1 which is the range of long long data type you may use strings. In the below code we store an integer in a string and then display it.

C program to store integer in a string

#include <stdio.h>
 
int main () 
{
   char n[1000];
 
   printf("Input an integer\n");
   scanf("%s", n);
 
   printf("%s", n);
 
   return 0;
}
Output of program:
Input an integer
13246523123156432123154131212341564313219
13246523123156432123154131212341564313219
Advantage of using string is that we can store very very large integers. C programming language does not has a built in type to handle large numbers.

No comments:

Post a Comment