C/Data Type/Int
Содержание
- 1 Adds up 5 numbers
- 2 Assign int value to three int variable
- 3 Assign value to short int
- 4 Averaging ten numbers - storing the numbers the hard way
- 5 Convert float to int
- 6 Define and use int
- 7 Define int and initialize it
- 8 Define int variable and output
- 9 Display a table of squares and cubes
- 10 Divide the first number by the second
- 11 Do calculation on int
- 12 For and sum:Sum the integers from 1 to a user-specified number
- 13 How to define variable and use them
- 14 Int variable: value assign and calculation
- 15 maximum and minimum value of int
- 16 Output the short and unsigned short int
- 17 Printf and scanf pair for int value
- 18 Simple int calculation
- 19 Using a variable
Adds up 5 numbers
#include <stdio.h>
int main() {
int total = 0;
int current;
int counter = 0;
char line[80];
while (counter < 5) {
printf("Number? ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%d", ¤t);
total += current;
++counter;
}
printf("The grand total is %d\n", total);
return (0);
}
Assign int value to three int variable
#include <stdio.h>
int main(void)
{
int i, j, k;
i = j = k = 99;
printf("%d %d %d", i, j, k);
return 0;
}
Assign value to short int
#include <stdio.h>
int main(void)
{
short int i;
i = -10;
printf("%d", i);
return 0;
}
Averaging ten numbers - storing the numbers the hard way
#include <stdio.h>
void main()
{
int number0 = 10, number1 = 40, number2 = 50, number3 = 80, number4 = 80;
int number5 = 20, number6 = 30, number7 = 60, number8 = 70, number9 = 110;
long sum = 0L; /* Sum of the numbers */
float average = 0.0f; /* Average of the numbers */
sum = number0 + number1+ number2 + number3 + number4+
number5 + number6 + number7 + number8 + number9;
average = (float)sum/10.0f;
printf("Average of the ten numbers entered is: %f", average);
}
Convert float to int
#include <stdio.h>
int main(void)
{
int i;
float f;
f = 34.0098;
i = f; /* convert float to int */
printf("%f %d", f, i);
return 0;
}
Define and use int
#include <stdio.h>
int main(void)
{
int i;
i = 10;
i = -i;
printf(" i = %d", i);
return 0;
}
Define int and initialize it
#include <stdio.h>
int main(void)
{
int i = -1;
printf(" i is initialized to %d", i);
return 0;
}
Define int variable and output
/* Variables */
#include <stdio.h>
void main()
{
int a; /* Declare an int variable called a */
int b; /* and another int variable called b*/
a = 7; /* Store 7 in the variable a */
b = 7; /* Store 7 in the variable b*/
/* Display some output */
printf("a is %d and b is %d ", a, b);
}
Display a table of squares and cubes
#include <stdio.h>
int main(void)
{
int i;
for(i = 1; i < 20; i++)
printf("%8d %8d %8d\n", i, i * i, i * i * i);
return 0;
}
Divide the first number by the second
#include <stdio.h>
int main(void)
{
int a, b;
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
if(b)
printf("%d\n", a/b);
else
printf("Cannot divide by zero.\n");
return 0;
}
Do calculation on int
#include <stdio.h>
int main(void)
{
int i;
for(i = 1; i < 11; i++)
printf("%d %d %d\n", i, i * i, i * i * i);
return 0;
}
For and sum:Sum the integers from 1 to a user-specified number
#include <stdio.h>
void main() {
long sum = 0L;
int count = 10; /* The number of integers to be summed */
int i = 0; /* The loop counter */
/* Sum integers from 1 to count */
for(i = 1 ; i <= count ; i++)
sum += i;
printf("\nTotal of the first %d numbers is %ld\n", count, sum);
}
How to define variable and use them
#include <stdio.h>
main() {
int i,j,k; // Defining variables
i = 6;
j = 8;
k = i + j;
printf("sum of two numbers is %d \n",k);
}
Int variable: value assign and calculation
#include <stdio.h>
int main(void)
{
int a, b;
a = 1;
a = a + 1;
b = a;
b = b - 1;
printf("%d %d", a, b);
return 0;
}
maximum and minimum value of int
#include <stdio.h>
main() {
int i,j ;
i = 1;
while (i > 0) {
j = i;
i++;
}
printf ("Maximum value of integer is %d\n",j);
printf ("The value of integer after overflow is %d\n",i);
}
Output the short and unsigned short int
#include <stdio.h>
int main(void)
{
short int i; /* a signed short integer */
unsigned short int u; /* an unsigned short integer */
u = 303000;
i = u;
printf("%hd %hu", i, u);
return 0;
}
Printf and scanf pair for int value
#include <stdio.h>
int main(void)
{
int len, width;
printf("Length: ");
scanf("%d", &len);
printf("Width: ");
scanf("%d", &width);
printf("Area = %d", len * width);
return 0;
}
Simple int calculation
#include <stdio.h>
main() {
int i,j,k;
scanf("%d%d",&i,&j);
k = i + j;
printf("sum is %d \n",k);
}
Using a variable
/* Using a variable */
#include <stdio.h>
void main()
{
int s; /* Declare a variable called s */
s = 10000; /* A simple arithmetic assignment statement */
printf("s is %d.", s);
}