Basic Input & Output Examples

Learn Basic Input and Output in C programming with CodeKilla. Understand how to use printf() and scanf() for taking user input and displaying output effectively in C.

01. Hello World Program in C
#include <stdio.h> 
int main(void) { // Main function where program execution begins
    printf("Hello, World!\n"); // Prints text to the console
    return 0; // Ends the program successfully
}
#include<stdio.h>
int main(void) {
char name[100];
printf("Enter your name: ");
if (fgets(name, sizeof(name), stdin)) {
// remove trailing newline if present
size_t i = 0; while (name[i] != '\0') { if (name[i] == '\n') { name[i] = '\0'; break; } i++; }
printf("Hello, %s!\n", name);
}
return 0;
}
#include <stdio.h>
int main(void){
int num1,num2,sum;
// Prompt the user to enter two integers
printf("Enter two integers separated by space: ");
// Take input from the user
scanf("%d %d",&num1,&num2);
// Calculate the sum
sum=num1+num2;
// Display the result
printf("Sum=%d\n",sum);
return 0;
}
#include <stdio.h>
int main(void){
    int num1,num2;
    printf("Enter two integers separated by space: ");
    if(scanf("%d %d",&num1,&num2)==2){
        int sum=num1+num2;
        printf("Sum=%d\n",sum);
    }else{
        printf("Invalid input. Please enter two integers.\n");
    }
    return 0;
}
#include <stdio.h>
int main(void){
int num1,num2;
// Prompt the user to enter two integers
printf("Enter two integers separated by space: ");
// Validate input
if(scanf("%d %d",&num1,&num2)==2){
// Calculate the difference
int diff=num1-num2;
// Display the result
printf("Difference=%d\n",diff);
}else{
// Invalid input message
printf("Invalid input. Please enter two integers.\n");
}
return 0;
}
#include <stdio.h>
int main(void) {
    int x, y, temp;
    printf("Enter two integers to swap: ");
    if (scanf("%d %d", &x, &y) == 2) {
        printf("Before swap: x = %d, y = %d\n", x, y);
        temp = x;
        x = y;
        y = temp;
        printf("After swap: x = %d, y = %d\n", x, y);
    }
    return 0;
}
#include <stdio.h>
int main(void) {
int x, y;
printf("Enter two integers to swap (without temp): ");
if (scanf("%d %d", &x, &y) == 2) {
printf("Before swap: x = %d, y = %d\n", x, y);
x = x + y;
y = x - y;
x = x - y;
printf("After swap: x = %d, y = %d\n", x, y);
}
return 0;
}
#include <stdio.h>
int main(void) {
char ch;
printf("Enter a character: ");
// skip whitespace
if (scanf(" %c", &ch) == 1) {
printf("Character: %c, ASCII value: %d\n", ch, (int)ch);
}
return 0;
}
#include <stdio.h>
int main(void) {
double a, b;
printf("Enter two floating-point numbers: ");
if (scanf("%lf %lf", &a, &b) == 2) {
printf("Product = %g\n", a * b);
}
return 0;
}
#include <stdio.h>
int main(void) {
int dividend, divisor;
printf("Enter dividend and divisor: ");
if (scanf("%d %d", &dividend, &divisor) == 2) {
if (divisor == 0) {
printf("Error: Division by zero.\n");
} else {
printf("Quotient = %d, Remainder = %d\n", dividend / divisor, dividend % divisor);
}
}
return 0;
}
#include <stdio.h>
int main(void) {
printf("Size of char: %zu byte(s)\n", sizeof(char));
printf("Size of int: %zu byte(s)\n", sizeof(int));
printf("Size of short: %zu byte(s)\n", sizeof(short));
printf("Size of long: %zu byte(s)\n", sizeof(long));
printf("Size of long long: %zu byte(s)\n", sizeof(long long));
printf("Size of float: %zu byte(s)\n", sizeof(float));
printf("Size of double: %zu byte(s)\n", sizeof(double));
return 0;
}
#include <stdio.h>
int main(void) {
double celsius;
printf("Enter temperature in Celsius: ");
if (scanf("%lf", &celsius) == 1) {
double fahrenheit = (celsius * 9.0 / 5.0) + 32.0;
printf("%.2f C = %.2f F\n", celsius, fahrenheit);
}
return 0;
}
#include <stdio.h> // Include standard input-output header
#define PI 3.14159 // Define constant value for PI
int main(void){
float radius,area; // Declare variables
printf("Enter the radius of the circle:"); // Ask user for radius
scanf("%f",&radius); // Read input value
area=PI*radius*radius; // Calculate area using formula πr²
printf("Area of the circle=%.2f\n",area); // Display the result
return 0; // End of program
}
#include <stdio.h> // Include standard input-output header
int main(void){
float length,width,area; // Declare variables
printf("Enter length and width of the rectangle:"); // Ask user for input
scanf("%f%f",&length,&width); // Read length and width
area=length*width; // Calculate area using formula length × width
printf("Area of the rectangle=%.2f\n",area); // Display the result
return 0; // End of program
}
#include <stdio.h>
int main(void){
    float p,r,t,si; // p=Principal, r=Rate, t=Time, si=Simple Interest
    printf("Enter Principal, Rate, and Time: ");
    scanf("%f%f%f",&p,&r,&t); // input values
    si=(p*r*t)/100; // formula to calculate simple interest
    printf("Simple Interest = %.2f\n",si); // display result
    return 0;
}
#include <stdio.h>

int main() {
    float a, b, c, average; // Declare float variables

    printf("Enter three numbers: ");
    scanf("%f %f %f", &a, &b, &c);

    average = (a + b + c) / 3; // Calculate average
    printf("Average = %.2f\n", average);
    return 0;
}
#include <stdio.h>

int main() {
    int num, square;

    printf("Enter a number: ");
    scanf("%d", &num);

    square = num * num; // Multiply number by itself
    printf("Square = %d\n", square);
    return 0;
}
#include <stdio.h>

int main() {
    int num, cube;

    printf("Enter a number: ");
    scanf("%d", &num);

    cube = num * num * num; // Multiply number three times
    printf("Cube = %d\n", cube);
    return 0;
}
#include <stdio.h>

int main() {
    float length, width, perimeter;

    printf("Enter length and width: ");
    scanf("%f %f", &length, &width);

    perimeter = 2 * (length + width); // Perimeter formula
    printf("Perimeter = %.2f\n", perimeter);
    return 0;
}
#include <stdio.h>
#define PI 3.1416

int main() {
    float radius, circumference;

    printf("Enter radius: ");
    scanf("%f", &radius);

    circumference = 2 * PI * radius; // Circumference formula
    printf("Circumference = %.2f\n", circumference);
    return 0;
}
#include <stdio.h>

int main() {
    float side, perimeter;

    printf("Enter side of square: ");
    scanf("%f", &side);

    perimeter = 4 * side; // Perimeter formula
    printf("Perimeter = %.2f\n", perimeter);
    return 0;
}
#include <stdio.h>

int main() {
    float side, area;

    printf("Enter side of square: ");
    scanf("%f", &side);

    area = side * side; // Area formula
    printf("Area = %.2f\n", area);
    return 0;
}
#include <stdio.h>

int main() {
    float a, b, c, perimeter;

    printf("Enter three sides of triangle: ");
    scanf("%f %f %f", &a, &b, &c);

    perimeter = a + b + c; // Sum of all sides
    printf("Perimeter = %.2f\n", perimeter);
    return 0;
}
#include <stdio.h>

int main() {
    float base, height, area;

    printf("Enter base and height: ");
    scanf("%f %f", &base, &height);

    area = 0.5 * base * height; // Area formula
    printf("Area = %.2f\n", area);
    return 0;
}
#include <stdio.h>

int main() {
    float a, b, c, d, average;

    printf("Enter four numbers: ");
    scanf("%f %f %f %f", &a, &b, &c, &d);

    average = (a + b + c + d)/4; // Average formula
    printf("Average = %.2f\n", average);
    return 0;
}