0% found this document useful (0 votes)
26 views15 pages

C Language Programming Examples

The document contains a collection of 15 C language programs that demonstrate various programming concepts. These include calculating the area and perimeter of a circle, finding ASCII values, checking for leap years, and sorting arrays, among others. Each program includes user input, processing logic, and output statements.

Uploaded by

tihsijgsh1331
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views15 pages

C Language Programming Examples

The document contains a collection of 15 C language programs that demonstrate various programming concepts. These include calculating the area and perimeter of a circle, finding ASCII values, checking for leap years, and sorting arrays, among others. Each program includes user input, processing logic, and output statements.

Uploaded by

tihsijgsh1331
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Language Programs

1. Program to find area and perimeter of circle

#include <stdio.h>

#include <math.h>

int main() {

float radius, area, perimeter;

// User input

printf("Enter the radius of the circle: ");

scanf("%f", &radius);

// Area and Perimeter calculations

area = M_PI * radius * radius;

perimeter = 2 * M_PI * radius;

// Output

printf("Area of the circle: %.2f\n", area);

printf("Perimeter of the circle: %.2f\n", perimeter);

return 0;

2. Program to find ASCII value of character


#include <stdio.h>

int main() {

char ch;

// User input

printf("Enter a character: ");

scanf("%c", &ch);

// Display ASCII value

printf("ASCII value of %c is %d\n", ch, ch);

return 0;

3. Program to find size of int, float, double and char

#include <stdio.h>

int main() {

printf("Size of int: %zu bytes\n", sizeof(int));

printf("Size of float: %zu bytes\n", sizeof(float));

printf("Size of double: %zu bytes\n", sizeof(double));

printf("Size of char: %zu bytes\n", sizeof(char));


return 0;

4. Program to swap two numbers

#include <stdio.h>

int main() {

int a, b, temp;

// User input

printf("Enter two numbers: ");

scanf("%d %d", &a, &b);

// Swap numbers

temp = a;

a = b;

b = temp;

// Output swapped numbers

printf("After swapping: a = %d, b = %d\n", a, b);

return 0;

5. Program to check whether a character is vowel or consonant


#include <stdio.h>

int main() {

char ch;

// User input

printf("Enter a character: ");

scanf("%c", &ch);

// Check for vowel or consonant

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||

ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {

printf("%c is a vowel.\n", ch);

} else {

printf("%c is a consonant.\n", ch);

return 0;

6. Program to check leap year

#include <stdio.h>

int main() {
int year;

// User input

printf("Enter a year: ");

scanf("%d", &year);

// Leap year check

if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {

printf("%d is a Leap Year.\n", year);

} else {

printf("%d is not a Leap Year.\n", year);

return 0;

7. Program to find largest number among three numbers

#include <stdio.h>

int main() {

int a, b, c;

// User input

printf("Enter three numbers: ");

scanf("%d %d %d", &a, &b, &c);


// Finding the largest number

if (a >= b && a >= c) {

printf("%d is the largest number.\n", a);

} else if (b >= a && b >= c) {

printf("%d is the largest number.\n", b);

} else {

printf("%d is the largest number.\n", c);

return 0;

8. Program to find factorial of a number

#include <stdio.h>

int main() {

int num;

long long factorial = 1;

// User input

printf("Enter a number: ");

scanf("%d", &num);

// Factorial calculation
if (num < 0) {

printf("Factorial is not defined for negative numbers.\n");

} else {

for (int i = 1; i <= num; i++) {

factorial *= i; // factorial = factorial * i

printf("Factorial of %d is %lld\n", num, factorial);

return 0;

9. Program to display Fibonacci sequence

#include <stdio.h>

int main() {

int n, first = 0, second = 1, next;

// User input

printf("Enter the number of terms: ");

scanf("%d", &n);

// Display Fibonacci sequence

printf("Fibonacci Sequence: ");

for (int i = 0; i < n; i++) {


if (i == 0) {

printf("%d ", first);

} else if (i == 1) {

printf("%d ", second);

} else {

next = first + second; // Next term = sum of previous two terms

first = second;

second = next;

printf("%d ", next);

printf("\n");

return 0;

10. Program to find GCD of two numbers

#include <stdio.h>

int main() {

int a, b, gcd;

// User input

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

// Euclidean algorithm to find GCD

for (int i = 1; i <= a && i <= b; i++) {

if (a % i == 0 && b % i == 0) {

gcd = i; // i is the GCD

// Output GCD

printf("GCD of %d and %d is %d\n", a, b, gcd);

return 0;

11. Program to reverse a number

#include <stdio.h>

int main() {

int num, reversed = 0, remainder;

// User input

printf("Enter a number: ");

scanf("%d", &num);
// Reversing the number

while (num != 0) {

remainder = num % 10; // Extract the last digit

reversed = reversed * 10 + remainder; // Build the reversed number

num /= 10; // Remove the last digit

// Output reversed number

printf("Reversed Number: %d\n", reversed);

return 0;

12. Program to check whether a number is palindrome or not

#include <stdio.h>

int main() {

int num, originalNum, reversed = 0, remainder;

// User input

printf("Enter a number: ");

scanf("%d", &num);

// Store the original number

originalNum = num;
// Reversing the number

while (num != 0) {

remainder = num % 10; // Extract the last digit

reversed = reversed * 10 + remainder; // Build the reversed number

num /= 10; // Remove the last digit

// Checking if the number is palindrome

if (originalNum == reversed) {

printf("%d is a Palindrome number.\n", originalNum);

} else {

printf("%d is not a Palindrome number.\n", originalNum);

return 0;

13. Program to display Armstrong numbers between two intervals

#include <stdio.h>

#include <math.h>

int main() {

int lower, upper, num, remainder, sum, n;


// User input for interval

printf("Enter the lower and upper limits: ");

scanf("%d %d", &lower, &upper);

printf("Armstrong numbers between %d and %d are:\n", lower, upper);

// Loop through the given interval

for (int i = lower; i <= upper; i++) {

num = i;

sum = 0;

n = 0;

// Finding the number of digits

while (num != 0) {

num /= 10;

++n;

num = i;

// Calculate the sum of powers of digits

while (num != 0) {

remainder = num % 10;

sum += pow(remainder, n); // Calculate the power of each digit

num /= 10;

}
// Check if the number is an Armstrong number

if (sum == i) {

printf("%d ", i);

printf("\n");

return 0;

14. Program to print corner elements of matrix

#include <stdio.h>

int main() {

int rows, cols;

// User input for matrix size

printf("Enter the number of rows and columns: ");

scanf("%d %d", &rows, &cols);

int matrix[rows][cols];

// Input elements of the matrix


printf("Enter the elements of the matrix:\n");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

scanf("%d", &matrix[i][j]);

// Printing corner elements

printf("Corner elements of the matrix are:\n");

if (rows > 1 && cols > 1) {

printf("Top-left corner: %d\n", matrix[0][0]);

printf("Top-right corner: %d\n", matrix[0][cols-1]);

printf("Bottom-left corner: %d\n", matrix[rows-1][0]);

printf("Bottom-right corner: %d\n", matrix[rows-1][cols-1]);

} else {

printf("Matrix must have at least 2 rows and 2 columns.\n");

return 0;

15.C Program to Sort an element in Array..


#include <stdio.h>

int main() {
int n;

// User input for array size


printf("Enter the number of elements: ");
scanf("%d", &n);

int arr[n];

// User input for array elements


printf("Enter the elements of the
array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Sorting the array using bubble sort


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swapping the elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

// Displaying sorted array


printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

return 0;
}

You might also like