Perimeter Programs — int & float
These three programs use the simplest possible formulas to practise int and float inputs. Perimeter just means the total distance around the outside of a shape.
- Square: all 4 sides equal → perimeter = 4 × side
- Rectangle: 2 lengths + 2 widths → perimeter = 2 × (length + width)
- Triangle: add all 3 sides → perimeter = a + b + c
#include <stdio.h> int main() { int side; int perimeter; printf("Enter side of square (cm): "); scanf("%d", &side); perimeter = 4 * side; // formula: 4 × side printf("Perimeter of square = %d cm\n", perimeter); return 0; }
Enter side of square (cm): 7 Perimeter of square = 28 cm
#include <stdio.h> int main() { float length, width; float perimeter; printf("Enter length (cm): "); scanf("%f", &length); printf("Enter width (cm): "); scanf("%f", &width); perimeter = 2 * (length + width); // formula: 2×(l+w) printf("Perimeter of rectangle = %.2f cm\n", perimeter); return 0; }
Enter length (cm): 8.5 Enter width (cm): 4.2 Perimeter of rectangle = 25.40 cm
#include <stdio.h> int main() { float a, b, c; float perimeter; printf("Enter side a: "); scanf("%f", &a); printf("Enter side b: "); scanf("%f", &b); printf("Enter side c: "); scanf("%f", &c); perimeter = a + b + c; // formula: a + b + c printf("Perimeter of triangle = %.2f cm\n", perimeter); return 0; }
Enter side a: 3.0 Enter side b: 4.0 Enter side c: 5.0 Perimeter of triangle = 12.00 cm
Use
int when the value will always be whole — number of sides, count of items, age.Use
float when the value might be decimal — measurements, prices, averages.
Area Programs — Triangle, Circle, Square
#include <stdio.h> int main() { float base, height, area; printf("Enter base (cm): "); scanf("%f", &base); printf("Enter height (cm): "); scanf("%f", &height); area = 0.5 * base * height; // formula: ½ × b × h printf("Area of triangle = %.2f sq cm\n", area); return 0; }
Enter base (cm): 10 Enter height (cm): 6 Area of triangle = 30.00 sq cm
#include <stdio.h> int main() { int side, area; printf("Enter side of square (cm): "); scanf("%d", &side); area = side * side; // formula: side² printf("Area of square = %d sq cm\n", area); return 0; }
Enter side of square (cm): 6 Area of square = 36 sq cm
#include <stdio.h> int main() { float radius, area; float pi = 3.14159; printf("Enter radius (cm): "); scanf("%f", &radius); area = pi * radius * radius; // formula: π × r² printf("Area of circle = %.2f sq cm\n", area); return 0; }
Enter radius (cm): 7 Area of circle = 153.94 sq cm
| Shape | Perimeter Formula | Area Formula | Type to use |
|---|---|---|---|
| Square | 4 × side | side × side | int or float |
| Rectangle | 2 × (l + w) | l × w | float |
| Triangle | a + b + c | 0.5 × b × h | float |
| Circle | 2 × π × r | π × r × r | float/double |
Implicit Conversion — Automatic
Implicit conversion happens automatically when C assigns a value of one type to a variable of another type. C always converts to the larger or more precise type — so no data is lost going upward (int → float → double).
Going downward (double → int) also happens automatically but data IS lost — the decimal part is simply dropped without any warning.
Type promotion ladder — C promotes upward automatically (safe)
1 byte
4 bytes
4 bytes
8 bytes
#include <stdio.h> int main() { // ── Safe: int stored in float (no data loss) ── float myFloat = 9; // int 9 → float 9.0 automatically printf("int 9 as float: %f\n", myFloat); // 9.000000 // ── Safe: int math in expression with float ── float result = 5 + 2.5; // int 5 → float 5.0, then +2.5 printf("5 + 2.5 as float: %f\n", result); // 7.500000 // ── Unsafe: float stored in int (decimal LOST) ── int truncated = 9.99; // 9.99 → 9, decimal dropped! printf("9.99 as int: %d\n", truncated); // 9 // ── Integer division — both operands int ── int x = 5, y = 2; int sum = x / y; // 5/2 = 2, decimal dropped! printf("5 / 2 as int: %d\n", sum); // 2 // ── Mixed: int + float = float result ── float mixed = x + 2.5; // x(5) promoted to float first printf("5 + 2.5 mixed: %f\n", mixed); // 7.500000 return 0; }
int 9 as float: 9.000000 5 + 2.5 as float: 7.500000 9.99 as int: 9 ← decimal LOST! 5 / 2 as int: 2 ← decimal LOST! 5 + 2.5 mixed: 7.500000
int x=5, y=2; int sum = x/y; gives 2 not 2.5.C sees two ints, does integer division, and stores 2. The
.5 is silently discarded. No error. No warning.
Explicit Conversion — Casting
Explicit conversion (also called type casting) is when you manually tell C to treat a value as a different type. You write the target type in parentheses before the value: (float)x.
This is the solution to the integer division problem — cast one operand to float before dividing and C does proper decimal division.
#include <stdio.h> int main() { int x = 5, y = 2; // Without cast — integer division printf("Without cast: %d\n", x / y); // 2 // With cast — (float) before x forces float math printf("With cast: %.2f\n", (float)x / y); // 2.50 // Using 1.0 trick — multiply by 1.0 forces float printf("Using 1.0: %.2f\n", x * 1.0 / y); // 2.50 // Cast double back to int double pi = 3.14159; int piInt = (int)pi; // chops off decimal manually printf("(int) 3.14159 = %d\n", piInt); // 3 // Cast char to int — see ASCII value char letter = 'A'; printf("'A' as int = %d\n", (int)letter); // 65 return 0; }
Without cast: 2 With cast: 2.50 Using 1.0: 2.50 (int) 3.14159 = 3 'A' as int = 65
| Conversion | Type | Syntax | Result | Data Lost? |
|---|---|---|---|---|
| int → float | Implicit or Explicit | float f = 9; | 9.000000 | No |
| float → int | Implicit (dangerous!) | int n = 9.99; | 9 | Yes — decimal |
| int / int | Implicit (int math) | 5 / 2 | 2 | Yes — decimal |
| (float)int / int | Explicit cast | (float)5 / 2 | 2.50 | No |
| char → int | Explicit | (int)'A' | 65 (ASCII) | No |
| double → int | Explicit | (int)3.99 | 3 | Yes — decimal |
Whenever you divide and want decimal result — cast at least one operand:
(float)a / b OR a / (float)b OR a * 1.0 / bAll three work. The cast only needs to be on one side.
sizeof Operator — How Big is Each Type?
sizeof() is a built-in C operator that returns the number of bytes a data type or variable uses in memory. It is not a function — it is evaluated by the compiler, not at runtime.
- Returns type
size_t— always print it with%zu(not %d) - Works on types:
sizeof(int) - Works on variables:
sizeof(myVar) - Works on expressions:
sizeof(a + b) - Results may vary slightly between 32-bit and 64-bit systems
#include <stdio.h> int main() { // sizeof with type names printf("sizeof(int) = %zu bytes\n", sizeof(int)); printf("sizeof(float) = %zu bytes\n", sizeof(float)); printf("sizeof(double) = %zu bytes\n", sizeof(double)); printf("sizeof(char) = %zu bytes\n", sizeof(char)); printf("sizeof(long int) = %zu bytes\n", sizeof(long int)); printf("sizeof(long double) = %zu bytes\n", sizeof(long double)); printf("\n"); // sizeof with variables — same result int myInt; float myFloat; double myDouble; char myChar; printf("sizeof(myInt) = %zu bytes\n", sizeof(myInt)); printf("sizeof(myFloat) = %zu bytes\n", sizeof(myFloat)); printf("sizeof(myDouble) = %zu bytes\n", sizeof(myDouble)); printf("sizeof(myChar) = %zu bytes\n", sizeof(myChar)); printf("\n"); // sizeof an array — total bytes used int arr[10]; printf("sizeof(int arr[10]) = %zu bytes\n", sizeof(arr)); // 40 printf("Number of elements = %zu\n", sizeof(arr) / sizeof(arr[0])); // 10 return 0; }
sizeof(int) = 4 bytes sizeof(float) = 4 bytes sizeof(double) = 8 bytes sizeof(char) = 1 bytes sizeof(long int) = 8 bytes sizeof(long double) = 16 bytes sizeof(myInt) = 4 bytes sizeof(myFloat) = 4 bytes sizeof(myDouble) = 8 bytes sizeof(myChar) = 1 bytes sizeof(int arr[10]) = 40 bytes Number of elements = 10
Why does sizeof(arr) = 40?
Because int arr[10] holds 10 integers, each 4 bytes → 10 × 4 = 40 bytes total.
The trick sizeof(arr) / sizeof(arr[0]) divides total bytes by bytes-per-element to get the count — 40 / 4 = 10 elements. This is the safest way to get array size in C.
sizeof() returns type size_t — which is an unsigned long. Printing it with %d (signed int) works on most systems but can cause compiler warnings or wrong output on some systems. %zu is always correct and safe.
#include <stdio.h> int main() { int a = 7, b = 2; float result; // Without cast — wrong answer printf("7 / 2 (int): %d\n", a / b); // With explicit cast — correct answer result = (float)a / b; printf("7 / 2 (float cast): %.2f\n", result); // Implicit — int auto-converts to float float autoConv = 100; // 100 → 100.000000 printf("Auto int→float: %f\n", autoConv); // sizeof all types printf("\n-- Memory sizes --\n"); printf("int : %zu bytes\n", sizeof(int)); printf("float : %zu bytes\n", sizeof(float)); printf("double : %zu bytes\n", sizeof(double)); printf("char : %zu bytes\n", sizeof(char)); return 0; }
7 / 2 (int): 3 7 / 2 (float cast): 3.50 Auto int→float: 100.000000 -- Memory sizes -- int : 4 bytes float : 4 bytes double : 8 bytes char : 1 bytes
Quick Quiz
What is the perimeter of a square with side = 9?
What does float f = 9; store in f?
What is the output of: int sum = 5 / 2; printf("%d", sum);
How do you fix integer division to get 2.50 from 5 / 2?
What format specifier should you use to print the result of sizeof(int)?
Lesson Checklist
- I can write perimeter of square using int
- I can write perimeter of rectangle using float
- I can write area of triangle using 0.5 × base × height
- I know when to use int vs float in geometry programs
- I understand implicit conversion — C promotes types automatically
- I know int stored in float adds .000000 — no data lost
- I know float stored in int LOSES the decimal part
- I understand int/int = int — decimal is dropped
- I can use explicit cast (float) to fix division
- I understand sizeof() and use %zu to print it
- I can use sizeof(arr)/sizeof(arr[0]) to get array element count
- I completed the quiz