SImple Programs
Prepared By: Rajendra Prasad Joshi
1: /*First C program*/
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
Output:
Hello, world!
2: /* program to understand arithmetic operators*/
#include<stdio.h>
int main() {
int a = 6, b = 8;
printf("Addition = %d\n", a + b);
printf("Subtraction = %d\n", a - b);
printf("Multiplication = %d\n", a * b);
printf("Quotient = %d\n", a / b);
printf("Remainder = %d\n", a % b);
return 0;
}
Output:
Addition = 14
Subtraction = -2
Multiplication = 48
Quotient = 0
Remainder = 6
1
3. /* program to take user input and print it */
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
printf("You entered: %d", number);
return 0;
}
Output:
Enter an integer: 56
You entered: 56
4. /* Program to understand logical operators*/
#include <stdio.h>
int main() {
int a = 3, b = 5, c = 9;
printf("and logical operator= %d\n", a > b && a > c);
printf("or logical operator= %d\n", a > b || a > c);
printf("not logical operator= %d\n", !(a > c));
return 0;
}
Output:
and logical operator= 0
or logical operator= 0
not logical operator= 1
2
5./*Program to display ASCII value of a character */
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
// %d displays the integer value of a character
// %c displays the actual character
printf("ASCII value of %c = %d", c, c);
return 0;
}
Output:
Enter a character: G
ASCII value of G = 71
6. Program to declare variable.
#include <stdio.h>
int main() {
// create integer variable
int age = 25;
// print the age variable
printf("Age: %d", age);
return 0;
}
Output:
Age: 25
3
7. /*Program to Find the Size of int, float, double and char */
#include<stdio.h>
int main() {
int intType;
float floatType;
double doubleType;
char charType;
// sizeof evaluates the size of a variable
printf("Size of int: %zu bytes\n", sizeof(intType));
printf("Size of float: %zu bytes\n", sizeof(floatType));
printf("Size of double: %zu bytes\n", sizeof(doubleType));
printf("Size of char: %zu byte\n", sizeof(charType));
return 0;
}
Output:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte