0% found this document useful (0 votes)
13 views40 pages

C Programming Examples and Exercises

Uploaded by

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

C Programming Examples and Exercises

Uploaded by

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

[Link]

com/c-
programming/examples

[Link]
simple-c-programs/

[Link]
learn_c_by_examples/
simple_programs_in_c.htm

C Program to Print Ascii Value Of


Character
The void main() indicates that the main() function
will not return any value, but the int main()
indicates that the main() can return integer type
data.

#include<stdio.h>
void main()
{
/*Program To Print Ascii Value Of Character */
char a;
int b;
printf("Enter The Character ");
scanf("%c", &a);
b=a;
printf("\nOutput Is = %d " ,b);
}
gcc vijay.c
./[Link]

#include<stdio.h>
int main()
{
int i=0;
int empID[5];
empID[0]=10280;
empID[1]=10260;
empID[2]=10270;
empID[3]=10285;
empID[4]=10275
for(i=0;i<4;i++)
{
printf("%d n” ,empID[i]);
}
return 0;
}
Output:
10280
10260
10270
10285
10275

include <stdio.h>
#

void main()
{
int num;
printf("Enter a number: \n");
scanf("%d", &num);
if (num > 0)
printf("%d is a positive number \n", num);
else if (num < 0)
printf("%d is a negative number \n", num);
else
printf("0 is neither positive nor negative");
}

Output 1:
Enter a number:
0
0 is neither positive nor negative
Output 2:
Enter a number:
-3
-3 is a negative number
Output 3:
Enter a number:
100
100 is a positive number

C++ Program To Find Area Of Triangle

#include<stdio.h>
int main()
{
float b,h,area;
printf("Enter Height and Base Of Triangle : ");
scanf("%f %f",&h,&b);
area = (h*b)/2;
printf("Area of Triangle is: %f\n",area);
return 0;

#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;
}

#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;
}

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;
}
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;
}

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

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;
}

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;
}

Program to Check Even or Odd


#include <stdio.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
// true if num is perfectly divisible by 2
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}

#include <stdio.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
(num % 2 == 0) ? printf("%d is even.", num) :
printf("%d is odd.", num);
return 0;
}

C Program to Check Whether a Character is a

Vowel or Consonant

#include <stdio.h>
int main()
{
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);
// evaluates to 1 if variable c is a lowercase
vowel
lowercase_vowel = (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u');

// evaluates to 1 if variable c is a uppercase


vowel
uppercase_vowel = (c == 'A' || c == 'E' || c ==
'I' || c == 'O' || c == 'U');
// evaluates to 1 (true) if c is a vowel
if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}

C Program to Find the Largest Number Among

Three Numbers

#include <stdio.h>
int main()
{
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the
largest
if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);
// if n2 is greater than both n1 and n3, n2 is the
largest
if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);
// if n3 is greater than both n1 and n2, n3 is the
largest
if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);
return 0;
}

C Program to Check Leap Year


#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
// leap year if perfectly divisible by 400
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}

return 0;
}
Check Positive or Negative Using if...else
#include <stdio.h>
int main()
{
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num <= 0.0)
{
if (num == 0.0)
printf("You entered 0.");
else
printf("You entered a negative number.");
}
else
printf("You entered a positive number.");
return 0;
}

Program to Check Alphabet


#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <=
'Z'))
printf("%c is an alphabet.", c);
else
printf("%c is not an alphabet.", c);
return 0;
}

Sum of Natural Numbers Using for Loop


#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
sum += i;
}
printf("Sum = %d", sum);
return 0;
}
Sum of Natural Numbers Using while Loop
#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
i = 1;
while (i <= n) {
sum += i;
++i;
}

printf("Sum = %d", sum);


return 0;
}

Factorial of a Number
#include <stdio.h>
int main()
{
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
// shows error if the user enters a negative
integer
if (n < 0)
printf("Error! Factorial of a negative number
doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
return 0;
}

Fibonacci Series up to n terms


#include <stdio.h>
int main()
{
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}

Fibonacci Sequence Up to a Certain Number


#include <stdio.h>
int main()
{
int t1 = 0, t2 = 1, nextTerm = 0, n;
printf("Enter a positive number: ");
scanf("%d", &n);
// displays the first two terms which is always 0
and 1
printf("Fibonacci Series: %d, %d, ", t1, t2);
nextTerm = t1 + t2;
while (nextTerm <= n) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}

Output
Enter a positive integer: 100
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
89,

Program to Print English Alphabets


#include <stdio.h>
int main()
{
char c;
for (c = 'A'; c <= 'Z'; ++c)
printf("%c ", c);
return 0;
}

Program to Check Palindrome


#include <stdio.h>
int main()
{
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;

// reversed integer is stored in reversed variable


while (n != 0)
{
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}

// palindrome if orignal and reversed are equal


if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);
return 0;
}

Output
Enter an integer: 1001
1001 is a palindrome.

Program to Check Prime Number


#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 2; i <= n / 2; ++i) {
// condition for non-prime
if (n % i == 0) {
flag = 1;
break;
}
}
if (n == 1)
{
printf("1 is neither prime nor composite.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
Output
Enter a positive integer: 29
29 is a prime number.

Check Armstrong Number of three digits


#include <stdio.h>
int main()
{
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0)
{
// remainder contains the last digit
remainder = originalNum % 10;
result += remainder * remainder *
remainder;
// removing last digit from the orignal
number
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.",
num);
return 0;
}
Output
Enter a three-digit integer: 371
371 is an Armstrong number.

Armstrong Numbers Between Two Integers


#include <math.h>
#include <stdio.h>
int main()
{
int low, high, number, originalNumber, rem, count
= 0;
double result = 0.0;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
printf("Armstrong numbers between %d and %d
are: ", low, high);
// swap numbers if high < low
if (high < low)
{
high += low;
low = high - low;
high -= low;
}

// iterate number from (low + 1) to (high - 1)


// In each iteration, check if number is Armstrong
for (number = low + 1; number < high; +
+number) {
originalNumber = number;

// number of digits calculation


while (originalNumber != 0)
{
originalNumber /= 10;
++count;
}
originalNumber = number;
// result contains sum of nth power of individual
digits
while (originalNumber != 0) {
rem = originalNumber % 10;
result += pow(rem, count);
originalNumber /= 10;
}
// check if number is equal to the sum of nth
power of individual digits
if ((int)result == number) {
printf("%d ", number);
}

// resetting the values


count = 0;
result = 0;
}

return 0;
}

Output
Enter two numbers(intervals): 200
2000
Armstrong numbers between 200 and 2000 are:
370 371 407 1634

Calculate Length of String without Using


strlen() Function
#include <stdio.h>
int main()
{
char s[] = "Programming is fun";
int i;
for (i = 0; s[i] != '\0'; ++i);
printf("Length of the string: %d", i);
return 0;
}
Output
Length of the string: 18

Concatenate Two Strings Without Using


strcat()
#include <stdio.h>
int main()
{
char s1[100] = "programming ", s2[] = "is
awesome";
int length, j;
// store length of s1 in the length variable
length = 0;
while (s1[length] != '\0')
{
++length;
}

// concatenate s2 to s1
for (j = 0; s2[j] != '\0'; ++j, ++length)
{
s1[length] = s2[j];
}

// terminating the s1 string


s1[length] = '\0';
printf("After concatenation: ");
puts(s1);
return 0;
}

Output
After concatenation: programming is awesome

Copy String Without Using strcpy()


#include <stdio.h>
int main()
{
char s1[100], s2[100], i;
printf("Enter string s1: ");
fgets(s1, sizeof(s1), stdin);

for (i = 0; s1[i] != '\0'; ++i)


{
s2[i] = s1[i];
}
s2[i] = '\0';
printf("String s2: %s", s2);
return 0;
}
Output
Enter string s1: Hey fellow programmer.
String s2: Hey fellow programmer.

Store Information in Structure and Display it


#include <stdio.h>
struct student
{
char firstName[50];
int roll;
float marks;
} s[5];

int main()
{
int i;
printf("Enter information of students:\n");
// storing information
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");

// displaying information
for (i = 0; i < 5; ++i)
{
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
Output
Enter information of students:
For roll number1,
Enter name: Tom
Enter marks: 98

For roll number2,


Enter name: Jerry
Enter marks: 89
.
.
.
Displaying Information:

Roll number: 1
Name: Tom
Marks: 98
.
.
.

Example 1: Half Pyramid of *


*
**
***
****
*****
C Program
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}

Example 2: Half Pyramid of Numbers


1
12
123
1234
12345
C Program
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}

Example 3: Half Pyramid of Alphabets


A
BB
CCC
DDDD
EEEEE
C Program
#include <stdio.h>
int main() {
int i, j;
char input, alphabet = 'A';
printf("Enter an uppercase character you want to
print in the last row: ");
scanf("%c", &input);
for (i = 1; i <= (input - 'A' + 1); ++i) {
for (j = 1; j <= i; ++j) {
printf("%c ", alphabet);
}
++alphabet;
printf("\n");
}
return 0;
}

Example 4: Inverted half pyramid of *


*****
****
***
**
*
C Program
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}

Example 5: Inverted half pyramid of numbers


12345
1234
123
12
1
C Program
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}

Example 6: Full Pyramid of *


*
***
*****
*******
*********
C Program
#include <stdio.h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}

Example 7: Full Pyramid of Numbers


1
232
34543
4567654
567898765
C Program
#include <stdio.h>
int main() {
int i, space, rows, k = 0, count = 0, count1 = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
++count;
}
while (k != 2 * i - 1) {
if (count <= rows - 1) {
printf("%d ", i + k);
++count;
} else {
++count1;
printf("%d ", (i + k - 2 * count1));
}
++k;
}
count1 = count = k = 0;
printf("\n");
}
return 0;
}

Example 8: Inverted full pyramid of *


*********
*******
*****
***
*
C Program
#include <stdio.h>
int main() {
int rows, i, j, space;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (space = 0; space < rows - i; ++space)
printf(" ");
for (j = i; j <= 2 * i - 1; ++j)
printf("* ");
for (j = 0; j < i - 1; ++j)
printf("* ");
printf("\n");
}
return 0;
}
Example 9: Pascal's Triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
C Program
#include <stdio.h>
int main() {
int rows, coef = 1, space, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 0; i < rows; i++) {
for (space = 1; space <= rows - i; space++)
printf(" ");
for (j = 0; j <= i; j++) {
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
Example 10: Floyd's Triangle.
1
23
456
7 8 9 10
C Program
#include <stdio.h>
int main() {
int rows, i, j, number = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; ++j) {
printf("%d ", number);
++number;
}
printf("\n");
}
return 0;
}

#include <stdio.h>
struct student {
char name[50];
int roll;
float marks;
} s;

int main() {
printf("Enter information:\n");
printf("Enter name: ");
fgets([Link], sizeof([Link]), stdin);

printf("Enter roll number: ");


scanf("%d", &[Link]);
printf("Enter marks: ");
scanf("%f", &[Link]);

printf("Displaying Information:\n");
printf("Name: ");
printf("%s", [Link]);
printf("Roll number: %d\n", [Link]);
printf("Marks: %.1f\n", [Link]);

return 0;
}
Output
Enter information:
Enter name: Jack
Enter roll number: 23
Enter marks: 34.5
Displaying Information:
Name: Jack
Roll number: 23
Marks: 34.5

You might also like