C Program Examples
C Program Examples
1 + 2+ 3 =6
Note: 6 is the smallest perfect number.
Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28
Some more perfect numbers: 496, 8128
#include<stdio.h>
int main(){
int n,i=1,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
return 0;
}
Sample output:
Enter a number: 6
6 is a perfect number
@Alok
Page 1
@Alok
Page 2
}
if(count==0 && num!= 1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}
Sample output:
Enter a number: 5
5 is a prime number
Write a c program to check whether a number is strong
or not
Definition of strong number:
A number is called strong number if sum of the
factorial of its digit is equal to number itself. For
example: 145 since
1! + 4! + 5! = 1 + 24 + 120 = 145
#include<stdio.h>
int main(){
int num,i,f,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num){
i=1,f=1;
r=num%10;
while(i<=r){
f=f*i;
i++;
}
sum=sum+f;
num=num/10;
}
@Alok
Page 3
if(sum==temp)
printf("%d is a strong number",temp);
else
printf("%d is not a strong number",temp);
return 0;
}
Sample output:
Enter a number: 145
145 is a strong number
C program to find whether a number is palindrome or not
#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num){
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf("%d is a palindrome",temp);
else
printf("%d is not a palindrome",temp);
return 0;
}
Sample output:
Enter a number: 131
131 is a palindrome
Fibonacci series in c using for loop
@Alok
Page 4
#include<stdio.h>
int main(){
int k,r;
long int i=0l,j=1,f;
//Taking maximum numbers form user
printf("Enter the number range:");
scanf("%d",&r);
printf("FIBONACCI SERIES: ");
printf("%ld %ld",i,j); //printing firts two values.
for(k=2;k<r;k++){
f=i+j;
i=j;
j=f;
printf(" %ld",j);
}
return 0;
}
Sample output:
Enter the number range: 15
FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21 34 55 89 144 233
377
Factorial program in c without using recursion
#include<stdio.h>
int main(){
int i=1,f=1,num;
printf("Enter a number: ");
scanf("%d",&num);
while(i<=num){
f=f*i;
i++;
}
@Alok
Page 5
@Alok
Page 6
return 0;
}
Sample output:
Enter any number: 12
Reversed of number: 21
C program to calculate sum of digits
#include<stdio.h>
int main(){
int num,sum=0,r;
printf("Enter a number: ");
scanf("%d",&num);
while(num){
r=num%10;
num=num/10;
sum=sum+r;
}
printf("Sum of digits of number:
return 0;
}
Sample output:
Enter a number: 123
Sum of digits of number:
%d",sum);
@Alok
Page 7
}
printf("\n%d to the power %d is: %ld",num,pow,sum);
return 0;
}
How to add two numbers without using the plus operator
in c
#include<stdio.h>
int main(){
int a,b;
int sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
//sum = a - (-b);
sum = a - ~b -1;
printf("Sum of two integers: %d",sum);
return 0;
}
Write a c program or code to subtract
without using subtraction operator
two
numbers
#include<stdio.h>
int main(){
int a,b;
int sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
sum = a + ~b + 1;
@Alok
Page 8
@Alok
Page 9
num=num/10;
sum+=r;
}
if(sum>10)
num=sum;
else
break;
}
printf("\nSum of the digits in single digit is:
%ld",sum);
return 0;
}
Non Fibonacci
#include<stdio.h>
#include<conio.h>
void main(void)
{
int n,n1,n2,n3,n4;
clrscr();
// INPUT SECTION //
printf("Enter the range....");
scanf("%d",&n);
// END OF INPUT SECTION //
printf("\nThe non-fibonacci series is....\n");
n1=3;
n2=5;
n3=4;
while(n3<n)
{
while(n3<n2)
{
printf("\n\t%d",n3);
n3++;
}
n4=n1+n2;
n1=n2;
@Alok
Page 10
n2=n4;
n3++;
}
getch();
}
=
=
=
=
=
=
=
=
=
=
"zero"; break;
"one"; break;
"two"; break;
"three"; break;
"four"; break;
"five"; break;
"six"; break;
"seven"; break;
"eight"; break;
"nine"; break;
}
}
@Alok
Page 11
for(j=i-1;j>=0;j--){
printf("%s ",word[j]);
}
return 0;
}
Sample output:
Enter any integer: 23451208
two three four five one two zero eight
Conversion from uppercase to lower case using c program
#include<stdio.h>
#include<string.h>
int main(){
char str[20];
int i;
printf("Enter any string->");
scanf("%s",str);
printf("The string is->%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nThe string in lower case is->%s",str);
return 0;
}
COUNTING DIFFERENT CHARACTERS IN A STRING USING C
PROGRAM
#include <stdio.h>
int isvowel(char chk);
int main(){
char text[1000], chk;
int count;
count = 0;
@Alok
Page 12
@Alok
Page 13
}
}
printf("The sorted string\n");
for(i=0;i<=n;i++)
puts(str[i]);
return 0;
}
Concatenation of two strings in c programming language
#include<stdio.h>
int main(){
int i=0,j=0;
char str1[20],str2[20];
puts("Enter first string");
gets(str1);
puts("Enter second string");
gets(str2);
printf("Before concatenation the strings are\n");
puts(str1);
puts(str2);
while(str1[i]!='\0'){
i++;
}
while(str2[j]!='\0'){
str1[i++]=str2[j++];
}
str1[i]='\0';
printf("After concatenation the strings are\n");
puts(str1);
return 0;
}
@Alok
Page 14
@Alok
Page 15
#include<stdio.h>
int main(){
int a[3][3],b[3][3],c[3][3],i,j;
printf("Enter the First matrix->");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the Second matrix->");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
printf("\nThe First matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("\nThe Second matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",b[i][j]);
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nThe Addition of two matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
}
return 0;
}
Sum of diagonal elements of a matrix in c
#include<stdio.h>
int main(){
@Alok
Page 16
int a[10][10],i,j,sum=0,m,n;
printf("\nEnter the row and column of matrix: ");
scanf("%d %d",&m,&n);
printf("\nEnter the elements of matrix: ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(i==j)
sum=sum+a[i][j];
}
}
printf("\n\nSum of the diagonal elements of a matrix
is: %d",sum);
return 0;
}
@Alok
Page 17
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[i][j]=0;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
b[i][j]=a[j][i];
printf("\n%d",b[i][j]);
}
}
printf("\n\nTraspose of a matrix is -> ");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",b[i][j]);
}
}
return 0;
}
C program to find the largest element in an array
#include<stdio.h>
int main(){
int a[50],size,i,big;
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ,
size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
@Alok
Page 18
}
printf("\nBiggest: %d",big);
return 0;
}
C program to find the second largest element in an array
#include<stdio.h>
int main(){
int a[50],size,i,j=0,big,secondbig;
printf("Enter the size of the array: ");
scanf("%d",&size);
printf("Enter %d elements in to the array: ", size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++){
if(big<a[i]){
big=a[i];
j = i;
}
}
secondbig=a[size-j-1];
for(i=1;i<size;i++){
if(secondbig <a[i] && j != i)
secondbig =a[i];
}
printf("Second biggest: %d", secondbig);
return 0;
}
C program to find the second smallest element in an array
#include<stdio.h>
int main(){
int a[50],size,i,j=0,small,secondsmall;
printf("Enter the size of the array: ");
@Alok
Page 19
scanf("%d",&size);
printf("Enter %d elements in to the array: ", size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
small=a[0];
for(i=1;i<size;i++){
if(small>a[i]){
small=a[i];
j = i;
}
}
secondsmall=a[size-j-1];
for(i=1;i<size;i++){
if(secondsmall > a[i] && j != i)
secondsmall =a[i];
}
printf("Second smallest: %d", secondsmall);
return 0;
}
REMOVE DUPLICATE ELEMENTS IN AN ARRAY USING C PROGRAM
#include<stdio.h>
int main(){
int arr[50];
int *p;
int i,j,k,size,n;
printf("\nEnter size of the array: ");
scanf("%d",&n);
printf("\nEnter %d elements into the array: ",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
size=n;
p=arr;
for(i=0;i<size;i++){
for(j=0;j<size;j++){
if(i==j){
continue;
}
@Alok
Page 20
else if(*(p+i)==*(p+j)){
k=j;
size--;
while(k < size){
*(p+k)=*(p+k+1);
k++;
}
j=0;
}
}
}
printf("\nThe array after removing duplicates is: ");
for(i=0;i < size;i++){
printf(" %d",arr[i]);
}
return 0;
}
Write a program (wap) to delete an element at desired
position from an array in c language
#include<stdio.h>
int main(){
int a[50],i,pos,size;
printf("\nEnter size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ",size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
printf("\nEnter position where to delete: ");
scanf("%d",&pos);
i=0;
while(i!=pos-1)
i++;
while(i<10){
a[i]=a[i+1];
i++;
}
@Alok
Page 21
size--;
for(i=0;i<size;i++)
printf(" %d",a[i]);
return 0;
}
C program to generate and print armstrong numbers
#include <stdio.h>
int main()
{
int r;
long number = 0, c, sum = 0, temp;
printf("Enter an integer upto which you want to find
armstrong numbers\n");
scanf("%ld",&number);
printf("Following armstrong numbers are found from 1
to %ld\n",number);
for( c = 1 ; c <= number ; c++ )
{
temp = c;
while( temp != 0 )
{
r = temp%10;
sum = sum + r*r*r;
temp = temp/10;
}
if ( c == sum )
printf("%ld\n", c);
sum = 0;
}
return 0;
}
@Alok
Page 22
@Alok
Page 23