Basic programs in c lannguage

Thanks for Visiting here

Print Friendly and PDF

C Language basic program total 8 programs 

1. Program to Print an Integer

#include <stdio.h>
int main() {
int number;

printf("Enter an integer: ");

// reads and stores input
scanf("%d", &number);

// displays output
printf("You entered: %d", number);

return 0;
}

Output

Enter an integer: 25
You entered: 25

2. Program to Add Two Integers

#include <stdio.h>
int main() {

int number1, number2, sum;

printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);

// calculating sum
sum = number1 + number2;

printf("%d + %d = %d", number1, number2, sum);
return 0;
}

Output

Enter two integers: 12
11
12 + 11 = 23

3. Program to Multiply Two Numbers

#include <stdio.h>
int main() {
double a, b, product;
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);

// Calculating product
product = a * b;

// %.2lf displays number up to 2 decimal point
printf("Product = %.2lf", product);

return 0;
}

Output

Enter two numbers: 2.4
1.12
Product = 2.69

4. Program to Print ASCII Value

#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

5. Program to Compute Quotient and Remainder

#include <stdio.h>
int main() {
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", &dividend);
printf("Enter divisor: ");
scanf("%d", &divisor);

// Computes quotient
quotient = dividend / divisor;

// Computes remainder
remainder = dividend % divisor;

printf("Quotient = %d\n", quotient);
printf("Remainder = %d", remainder);
return 0;
}

Output

Enter dividend: 25
Enter divisor: 4
Quotient = 6
Remainder = 1

6. Program to Find the Size of Variables

#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

7. Program Using the long keyword

#include <stdio.h>
int main() {
int a;
long b; // equivalent to long int b;
long long c; // equivalent to long long int c;
double e;
long double f;

printf("Size of int = %zu bytes \n", sizeof(a));
printf("Size of long int = %zu bytes\n", sizeof(b));
printf("Size of long long int = %zu bytes\n", sizeof(c));
printf("Size of double = %zu bytes\n", sizeof(e));
printf("Size of long double = %zu bytes\n", sizeof(f));

return 0;
}

Output

Size of int = 4 bytes 
Size of long int = 8 bytes
Size of long long int = 8 bytes
Size of double = 8 bytes
Size of long double = 16 bytes

8. Swap Numbers Using Temporary Variable

#include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);

// value of first is assigned to temp
temp = first;

// value of second is assigned to first
first = second;

// value of temp (initial value of first) is assigned to second
second = temp;

// %.2lf displays number up to 2 decimal points
printf("\nAfter swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf", second);
return 0;
}

Output

Enter first number: 1.20
Enter second number: 2.45

After swapping, first number = 2.45
After swapping, second number = 1.20




Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad