C SAMPLE Programs
C SAMPLE Programs
This section gives a collection of C Programs on Strings. Strings are nothing but array of characters terminated
by '\0' character.
C program to reverse a string
C program to convert string to integer
C program to find majority elements of array
C program to check palindrome string
C program to check anagram string
C program to compare two strings
C program to compare lowercase string to uppercase
C Programs using Loop
1. Write C program to print alphabets from a to z
2. Write C program to print ASCII values of all characters
3. Write C program to print multiplication table of a given number
4. Write a C program to print all natural numbers in reverse order
5. Write a C program to print sum of digits enter by user
6. Write C program to find sum of even numbers between 1 to n
7. Write C program to find sum of odd numbers between 1 to n
8. Write C program to swap first and last digit of a number
9. Write C program to find the sum of first and last digit of any number
10. Write C program to find first and last digit of any number
11. Write C program to calculate product of digits of a number
12. Write C program to reverse a number using while & for loop
13. Write C program to calculate power using while & for loop
14. Write C program to find factorial of any number
15. Write a C program to check whether a number is Armstrong number or not
16. Write C program to find Armstrong numbers between 1 to n
17. Write a C program to calculate compound Interest
18. Write a C program to check a enter number is Prime number or not using while & for loop
19. Write a C program to check whether a number is palindrome or not
20. Write C program to print number in words
21. Write C program to find HCF of two numbers
22. Write C program to find LCM of two numbers
List of C Looping programs using while, do while, for
C program to print ODD numbers from 1 to N using while loop.
This is an example of while loop - In this C program, we are going to learn how can we print all ODD numbers
from given range (1 to N) using while loop?
C program to print EVEN numbers from 1 to N using while loop.
This is an example of while loop - In this C program, we are going to learn how can we print all EVEN numbers
from given range (1 to N) using while loop?
C program to print all uppercase alphabets using while loop.
This is an example of while loop in C programming language - In this C program, we are going to print all
uppercase alphabets from ‘A’ to ‘Z’ using while loop.
C program to print all lowercase alphabets using while loop.
This is an example of while loop in C programming language - In this C program, we are going to print all
lowercase alphabets from ‘a’ to ‘z’ using while loop.
C program to print numbers from 1 to N using while loop.
This is an example of while loop in C programming language - In this C program, we are going to print
numbers from 1 to N using while loop.
C program to print numbers from 1 to 10 using while loop.
This is an example of while loop in C programming language - In this C program, we are going to print
numbers from 1 to 10 using while loop.
C program to read an integer and print its multiplication table.
In this program, we are reading an integer number and printing its multiplication table, we are implementing the
program using for, while and do while (through all loops).
C Program to print tables from numbers 1 to 20.
This program will print table of numbers from 1 to 20 using nested looping. There are two loops using parent
loop to run from 1 to 20 and child loop to run from 1 to 10 to print table of corresponding number which is
coming from parent loop.
C Program to check entered number is ZERO, POSITIVE or NEGATIVE until user does not want to quit.
This program will read an integer number and check whether entered number is Positive, Negative or Zero until
user does not want to exit.
C Program to find factorial of a number.
In this program, we will read and integer number and find the factorial using different methods - using simple
method (without using user define function), using User Define Function and using Recursion.
C Program to find sum of first N natural number, N must be taken by the user.
This program will read the value of N and calculate sum from 1 to N, first N natural numbers means numbers
from 1 to N and N will be read through user.
C program to print all prime numbers from 1 to N.
This program will read the value of N and print all prime numbers from 1 to N. The logic behind implement this
program - Run loop from 1 to N and check each value in another loop, if the value is divisible by any number
between 2 to num-1 (or less than equal to num/2) - Here num is the value to check it is prime of not.
C program to print all even and odd numbers from 1 to N.
This program will read value of N and print all numbers from 1 to N with EVEN and ODD after checking the
numbers whether they are EVEN or ODD.
C program to print all Armstrong numbers from 1 to N.
This program will read value of N and print all Armstrong Numbers from 1 to N.
C program to print square, cube and square root of all numbers from 1 to N.
This program will print Square, Cube and Square Root of all Numbers from 1 to N using loop.
C program to print all leap years from 1 to N.
This program will read value of N and print all Leap Years from 1 to N years. There are two conditions for leap
year: 1- If year is divisible by 400 ( for Century years), 2- If year is divisible by 4 and must not be divisible by
100 (for Non Century years).
C program to print all upper case and lower case alphabets.
In this C program, we are going to print all uppercase alphabets (from 'A' to 'Z') and lowercase alphabets (from
'a' to 'z').
C program to read age of 15 person and count total Baby age, School age and Adult age.
This program will read age of 15 person one by one (using while loop) and count total number of Baby Age,
School attending person age and Adult age person.
Write a program in C to display the first 10 natural numbers. Go to the editor
Expected Output :
1 2 3 4 5 6 7 8 9 10
Click me to see the solution
2. Write a C program to find the sum of first 10 natural numbers. Go to the editor
Expected Output :
The first 10 natural number is :
1 2 3 4 5 6 7 8 9 10
The Sum is : 55
Click me to see the solution
3. Write a program in C to display n terms of natural number and their sum.Go to the editor
Test Data : 7
Expected Output :
The first 7 natural number is :
1234567
The Sum of Natural Number upto 7 terms : 28
Click me to see the solution
4. Write a program in C to read 10 numbers from keyboard and find their sum and average. Go to the editor
Test Data :
Input the 10 numbers :
Number-1 :2
...
Number-10 :2
Expected Output :
The sum of 10 no is : 55
The Average is : 5.500000
Click me to see the solution
5. Write a program in C to display the cube of the number upto given an integer. Go to the editor
Test Data :
Input number of terms : 5
Expected Output :
Number is : 1 and cube of the 1 is :1
Number is : 2 and cube of the 2 is :8
Number is : 3 and cube of the 3 is :27
Number is : 4 and cube of the 4 is :64
Number is : 5 and cube of the 5 is :125
Click me to see the solution
6. Write a program in C to display the multiplication table of a given integer. Go to the editor
Test Data :
Input the number (Table to be calculated) : 15
Expected Output :
15 X 1 = 15
...
...
15 X 10 = 150
Click me to see the solution
7. Write a program in C to display the multipliaction table vertically from 1 to n. Go to the editor
Test Data :
Input upto the table number starting from 1 : 8
Expected Output :
Multiplication table from 1 to 8
1x1 = 1, 2x1 = 2, 3x1 = 3, 4x1 = 4, 5x1 = 5, 6x1 = 6, 7x1 = 7, 8x1 = 8
...
1x10 = 10, 2x10 = 20, 3x10 = 30, 4x10 = 40, 5x10 = 50, 6x10 = 60, 7x10 = 70, 8x10 = 80
Click me to see the solution
8. Write a program in C to display the n terms of odd natural number and their sum . Go to the editor
Test Data
Input number of terms : 10
Expected Output :
The odd numbers are :1 3 5 7 9 11 13 15 17 19
The Sum of odd Natural Number upto 10 terms : 100
Click me to see the solution
9. Write a program in C to display the pattern like right angle triangle using an asterisk. Go to the editor
*
**
***
1 2 3 4 5
15 14 13 12
6 7 8
11 10
9
1
123
12345
1234567
123456789
A
BAB
CBABC
DCBABCD
EDCBABCDE
1
212
32123
43212345
543212345
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
0
101
21012
3210123
432101234
54321012345
1
62
10 7 3
13 11 8 6
15 14 12 9 5
11
2 2
3 3
4 4
5 5
4 4
3 3
2 2
11
*****
*
***
*
*****
* *
** **
***
* *
* *
Q.11 How to print V-Shape pyramid in C as:
* *
**
*
1
6 2
10 7 3
13 11 8 4
15 14 12 9 5
*****
*
*
*
DCBABCD
CBABC
BAB
A
Aa
Bb Bb
Cc Cc Cc
Dd Dd Dd Dd
*
***
*****
*******
**
****
******
*******
******
****
**
*******
*****
***
*
Q.18 How to make a continuous number pyramid with adding increasing zero digit with each row as:
1
02
003
0004
00005
000006
0000007
00000008
000000009
5
456
34567
2345678
123456789
2345678
34567
456
5
A
BC
DEF
GHIJ
KLMNO
1111111
1222221
1233321
1234321
1233321
1222221
1111111
ABCDE
ABCD
ABC
AB
A
I
N
D
I
A
A
II
DDD
NNNN
IIIII
123454321
1234321
12321
121
1
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
Q.27 Write the following Odd and even number pattern program in C as:
1
246
1357 9
2 4 6 8 10 12 14
1 3 5 7 9 11 13 15 17
4
434
43234
4321234
43234
434
4
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
1
2
3
4
5
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
D
CDC
BCDCB
ABCDCBA
BCDCB
CDC
D
Q.34 Number pattern of half diamond in C :
4
343
23432
1234321
23432
343
4