Example #1: Program to Count Number of Digits in an Integer
int main()
{
int n;
int count = 0;
printf("Enter an integer: ");
scanf("%d", &n);
while(n != 0)
{
// n = n/10
n /= 10;
++count;
}
printf("Number of digits: %d", count);
}
Example: Reverse an Integer
#include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
printf("Reversed Number = %d",reversedNumber);
return 0;
}
Example #1: Multiplication Table Up to 10
#include <stdio.h>
int main()
{
int n, i;
printf("Enter an integer: ");
scanf("%d",&n);
for(i=1; i<=10; ++i)
{
printf("%d * %d = %d \n", n, i, n*i);
}
return 0;
}
Output
Enter an integer: 9
9*1=9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
Example #1: Fibonacci Series up to n number of terms
#include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm = 0;
printf("Enter the number of terms: ");
scanf("%d",&n);
// displays the first two terms which is always 0 and 1
printf("Fibonacci Series: %d, %d, ", t1, t2);
// i = 3 because the first two terms are already dislpayed
for (i=3; i <= n; ++i)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
printf("%d, ",nextTerm);
}
return 0;
}
Output
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
Example #1: Program to Check Vowel or consonant
#include <stdio.h>
int main()
{
char c
int isLowercaseVowel, isUppercaseVowel;
printf("Enter an alphabet: ");
scanf("%c",&c);
// evaluates to 1 (true) if c is a lowercase vowel
isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// evaluates to 1 (true) if c is an uppercase vowel
isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true
if (isLowercaseVowel || isUppercaseVowel)
printf("%c is a vowel.",c);
else
printf("%c is a consonant.",c);
return 0;
}
Output
Enter an alphabet: G
G is a consonant.
Example: 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;
}
C program to find Maximum element in Array
#include <stdio.h>
int main()
int array[100], max, size, c, loc = 1;
printf("Enter the number of elements in array\n");
scanf("%d", &size);
printf("Enter %d integers\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
max = array[0];
for (c = 1; c < size; c++)
if (array[c] > max)
max = array[c];
loc= c+1;
printf("Maximum element is present at location %d and it's value is %d.\n", loc, max);
return 0;
C program to find Minimum element in Array
#include <stdio.h>
int main()
int array[100], min, size, c, loc = 1;
printf("Enter the number of elements in array\n");
scanf("%d",&size);
printf("Enter %d integers\n", size);
for ( c = 0 ; c < size ; c++ )
scanf("%d", &array[c]);
min = array[0];
for ( c = 1 ; c < size ; c++ )
if ( array[c] < min )
min = array[c];
loc = c+1;
printf("Minimum element is present at location %d and it's value is %d.\n", loc, min);
return 0;
}
C program to reverse an Array
#include <stdio.h>
int main()
int num, c, d, a[100], b[100];
printf("Enter the number of elements in array\n");
scanf("%d", &num);
printf("Enter the array elements\n");
for (c = 0; c < num ; c++)
scanf("%d", &a[c]);
for (c = num - 1, d = 0; c >= 0; c--, d++)
b[d] = a[c];
printf("Reverse array is\n");
for (c = 0; c < num; c++)
printf("%d\n", b[c]);
return 0;
}
C Program to Sort the Array in an Ascending Order
void main()
{
int i, j, a, n, num[30];
printf("Enter the size of an array \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &num[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (num[i] > num[j])
{
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; ++i)
printf("%d\n", num[i]);
}
C Program to Sorting an array in descending order
void main()
int i, j, a, n, num[30];
printf("Enter the size of an array \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &num[i]);
for (i = 0; i < n; ++i)
for (j = i + 1; j < n; ++j)
if (num[i] > num[j])
a = num[i];
num[i] = num[j];
num[j] = a;
printf("The numbers arranged in ascending order are given below \n");
for (i = n; i>0; --i)
printf("%d\n", num[i-1]);
C Program to Sorting an array in ascending and descending order
int main()
int a[10], i=0, j=0, n, t;
printf ("\n Enter the no. of elements: ");
scanf ("%d", &n);
printf ("\n");
for (i = 0; i <n; i++)
printf ("\n Enter the %dth element: ", (i+1));
scanf ("%d", &a[i]);
for (j=0 ; j<(n-1) ; j++)
for (i=0 ; i<(n-1) ; i++)
if (a[i+1] < a[i])
t = a[i];
a[i] = a[i + 1];
a[i + 1] = t;
printf ("\n Ascending order: ");
for (i=0 ; i<n ; i++)
printf (" %d", a[i]);
printf ("\n Descending order: ");
for (i=n ; i>0 ; i--)
printf (" %d", a[i-1]);
/* indicate successful completion */
return 0;
C program to insertion sort using Array
/* insertion sort ascending order */
#include <stdio.h>
int main()
int n, array[1000], c, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 1 ; c <= n - 1; c++)
d = c;
while ( d > 0 && array[d] < array[d-1])
t = array[d];
array[d] = array[d-1];
array[d-1] = t; d--;
}}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++)
{
printf("%d\n", array[c]);
return 0;
Cprogram to delete an element an Array
#include <stdio.h>
int main()
int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete element\n"); scanf("%d", &position); if ( position >=
n+1 ) printf("Deletion not possible.\n");
else
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
printf("Resultant array is\n");
for( c = 0 ; c < n - 1 ; c++ )
printf("%d\n", array[c]);
return 0;
}
Source Code to Copy String Manually
#include <stdio.h>
int main()
{
char s1[100], s2[100], i;
printf("Enter string s1: ");
scanf("%s",s1);
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
printf("String s2: %s",s2);
return 0;
}
Source code to reverse a sentence using recursion.
/* Example to reverse a sentence entered by user without using strings. */
#include <stdio.h>
void Reverse();
int main()
{
printf("Enter a sentence: ");
Reverse();
return 0;
}
void Reverse()
{
char c;
scanf("%c",&c);
if( c != '\n')
{
Reverse();
}
printf("%c",c);
Source Code to Find Number of Vowels, Consonants, Digits and White Space
Character
#include<stdio.h>
int main(){
char line[150];
int i,v,c,ch,d,s,o;
o=v=c=ch=d=s=0;
printf("Enter a line of string:\n");
gets(line);
for(i=0;line[i]!='\0';++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
++v;
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
++c;
else if(line[i]>='0'&&c<='9')
++d;
else if (line[i]==' ')
++s;
}
printf("Vowels: %d",v);
printf("\nConsonants: %d",c);
printf("\nDigits: %d",d);
printf("\nWhite spaces: %d",s);
return 0;
}