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
}
02. C Program to Print Your Name
#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;
}
03. C Program to Add Two Numbers (User Input)
#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;
}
04. C Program to Add Two Numbers (User Input + Validation)
#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;
}
05. C Program to Substraction Two Numbers (User Input + Validation)
#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;
}
06. C Program to Swap Two Numbers Using Temp (or using a third variable)
#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;
}
07. C Program to Swap Two Numbers Using Without Using Temp (or without using a third variable)
#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;
}
08. C Program to Find ASCII value of a character
#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;
}
09. C Program to Multiply two floating numbers
#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;
}
10. C Program to Find quotient and remainder
#include <stdio.h>
int main(void) {
int dividend, divisor;
printf("Enter dividend and divisor: ");
if (scanf("%d %d", ÷nd, &divisor) == 2) {
if (divisor == 0) {
printf("Error: Division by zero.\n");
} else {
printf("Quotient = %d, Remainder = %d\n", dividend / divisor, dividend % divisor);
}
}
return 0;
}
11. C Program to Size of data types using sizeof()
#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;
}
12. C Program to Convert Celsius to Fahrenheit
#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;
}
13. C Program to Calculate the Area of a Circle
#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
}
14. C Program to Calculate the Area of a Rectangle
#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
}
15. C Program to Calculate Simple Interest
#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;
}
16. C Program to Print Average of Three Numbers
#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;
}
17. C Program to Print Square of a Number
#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;
}
18. C Program to Print Cube of a Number
#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;
}
19. C Program to Print Perimeter of Rectangle
#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;
}
20. C Program to Print Circumference of Circle
#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;
}
21. C Program to Print Perimeter of Square
#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;
}
22. C Program to Print Area of Square
#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;
}
23. C Program to Print Perimeter of Triangle
#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;
}
24. C Program to Print Area of Right-Angle Triangle
#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;
}
25. C Program to Print Average of Four Numbers
#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;
}