C Examples
---->Note that all programs below have been designed, programmed and tested on the basis of GCC Compiler(Linux). And all of them may not be compatible with Turbo C or ANSI C compiler but maximum compability has been tried.
Some C Programmes
1. A Program to print Fibonnaci Series upto N.
#include<stdio.h>
#include<conio.h>
int main(void)
{
int n,a,b,i;
clrscr();
a=0;
b=1;
printf("Program for Fibonnaci Seriesn");
printf("Enter the number upto which the fibonnaci series to be printed:");
scanf("%d",&n);
printf("Fibonnaci Numbersn");
printf("%dn",a);
for (i=1;i<n;i++)
{
a=a+b;
b=a-b;
printf("%dn",a);
}
printf("press any key to exit");
getch();
return 0;
}
2. A Program to calculate HCF or GCD of Two Numbers.
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a,b,min,i;
clrscr();
printf("Program to calculate HCF or GCD of two numbersn");
printf("Enter the first number:");
scanf("%d",&a);
printf("Enter the second number:");
scanf("%d",&b);
min=(a>b)?b:a;
for (i=min;i>=1;i--)
{
if (a%i==0 && b%i==0)
{
break;
}
}
printf("The HCF of %d and %d is %d.n",a,b,i);
printf("press any key to exit");
getch();
return 0;
}
3. A Program to calculate HCF or GCD of Three Numbers.
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a,b,c,min1,min2,i;
clrscr();
printf("Program to calculate HCF or GCD of three numbersn");
printf("Enter the first number:");
scanf("%d",&a);
printf("Enter the second number:");
scanf("%d",&b);
printf("Enter the third number:");
scanf("%d",&c);
min1=(a>b)?b:a;
min2=(min1>c)?c:min1;
for (i=min2;i>=1;i--)
{
if (a%i==0 && b%i==0 && c%i==0)
{
break;
}
}
printf("The HCF of %d, %d and %d is %d.n",a,b,c,i);
printf("press any key to exit");
getch();
return 0;
}
4. A Program to calculate LCM of Two Numbers.
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a,b,min,i;
clrscr();
printf("Program to calculate LCM of two numbersn");
printf("Enter the first number:");
scanf("%d",&a);
printf("Enter the second number:");
scanf("%d",&b);
min=(a>b)?b:a;
for (i=min;i>=1;i--)
{
if (a%i==0 && b%i==0)
{
break;
}
}
printf("The LCM of %d and %d is %d.n",a,b,i*(a/i)*(b/i));
printf("press any key to exit");
getch();
return 0;
}
5. A Program to calculate LCM of Three Numbers.
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a,b,c,a1,b1,c1,a2,b2,c2,a3,b3,c3,min1,min2,i,j,k,l;
clrscr();
printf("Program to calculate LCM of three numbersn");
printf("Enter the first number:");
scanf("%d",&a);
printf("Enter the second number:");
scanf("%d",&b);
printf("Enter the third number:");
scanf("%d",&c);
min1=(a>b)?b:a;
min2=(min1>c)?c:min1;
for (i=min2;i>=1;i--)
{
if (a%i==0 && b%i==0 && c%i==0)
{
break;
}
}
a1=a/i;
b1=b/i;
c1=c/i;
min1=(a1>b1)?b1:a1;
for (j=min1;j>=1;j--)
{
if (a1%j==0 && b1%j==0)
{
break;
}
}
a2=a1/j;
b2=b1/j;
min1=(a2>c1)?c1:a2;
for (k=min1;k>=1;k--)
{
if (a2%k==0 && c1%k==0)
{
break;
}
}
a3=a2/k;
c2=c1/k;
min1=(b2>c2)?c2:b2;
for (l=min1;l>=1;l--)
{
if (b2%l==0 && c2%l==0)
{
break;
}
}
b3=b2/l;
c3=c2/l;
printf("LCM=%dn",i*j*k*l*a3*b3*c3);
printf("press any key to exit");
getch();
return 0;
}
6. A Program to print a Number in Reverse Order and check whether it is Palindrome or Not.
#include<stdio.h>
#include<conio.h>
int main(void)
{
int num,n,c,d=0;
clrscr();
printf("A program to reverse a numbern");
printf("Enter a number:");
scanf("%d",&n);
num=n;
while (n>0)
{
c=n;
n=n/10;
d=(d*10)+c;
}
printf("The reverse of the number %d is %dn",n,d);
if (d==num)
{
printf("The number %d is palindrome.",num);
}
else
{
printf("The number %d is not palindrome.",num);
}
getch();
return 0;
}
7. A Program to print Perfect Numbers upto N.
#include<stdio.h>
#include<conio.h>
int main(void)
{
int num,i,j,sum=0;
clrscr();
printf("Program to print perfect numbers from 1 to Nn");
printf("Enter the number upto which perfect numbers are to be printed:");
scanf("%d",&num);
for (j=1;j<=num;j++)
{
for (i=1;i<j;i++)
{
if (j%i==0)
{
sum=sum+i;
}
}
if (sum==j)
{
printf("%dn",j);
}
sum=0;
}
printf("press any key to exit");
getch();
return 0;
}
8. A Program to check whether a Number is Perfect or Not.
#include<stdio.h>
#include<conio.h>
int main(void)
{
int num,i,sum=0;
clrscr();
printf("Program to determine whether a given number is perfect or notn");
printf("Enter the number:");
scanf("%d",&num);
for (i=1;i<num;i++)
{
if (num%i==0)
{
sum=sum+i;
}
}
if (sum==num)
{
printf("The number is perfect.n");
}
else
{
printf("the number is not perfect.n");
}
printf("press any key to exit");
getch();
return 0;
}
9. A Program to print Prime Numbers upto N.
#include<stdio.h>
#include<conio.h>
int main(void)
{
int num,i,j,flag=0;
clrscr();
printf("Program to print prime numbers upto N termsn");
printf("Enter the number upto which prime numbers are to be printed:");
scanf("%d",&num);
printf("Prime Numbersn");
for (i=1;i<=num;i++)
{
if (i==1)
{
printf("%dn",i);
}
else
{
for (j=2;j<=(i-1);j++)
{
if (i%j==0)
{
flag=1;
break;
}
}
if (flag==0)
{
printf("%dn",i);
}
flag=0;
}
}
printf("press any key to exit");
getch();
return 0;
}
10. A Program to print Prime Numbers between an Interval.
#include<stdio.h>
#include<conio.h>
int main(void)
{
int num1,num2,i,j,flag=0;
clrscr();
printf("Enter the starting number from which prime numbers are to be printed:");
scanf("%d",&num1);
printf("Enter the ending number upto which prime numbers are to be printed:");
scanf("%d",&num2);
printf("Prime Numbers between %d and %dn",num1,num2);
for (i=num1;i<=num2;i++)
{
if (i==1)
{
printf("%dn",i);
}
else
{
for (j=2;j<=(i-1);j++)
{
if (i%j==0)
{
flag=1;
break;
}
}
if (flag==0)
{
printf("%dn",i);
}
flag=0;
}
}
printf("press any key to exit");
getch();
return 0;
}
11. A Program to check whether a Number is Prime or Not.
#include<stdio.h>
#include<conio.h>
int main(void)
{
int num,i,flag=0;
clrscr();
printf("Program to find whether the given number is prime or notn");
printf("Enter a number:");
scanf("%d",&num);
if (num==1)
{
printf("The number is neither prime nor composite.n");
}
else if (num>=2)
{
for (i=2;i<=(num-1);i++)
{
if (num%i==0)
{
flag=1;
break;
}
}
if (flag==0)
{
printf("The number is prime.n");
}
else
{
printf("The number is composite.n");
}
}
else
{
printf("Negative numbern");
}
printf("press any key to exit");
getch();
return 0;
}
12. A program to sort user entered array of numbers in ascending order.
#include<stdio.h>
int main()
{
int num,i,j,c;
printf("How many numbers to enter:");
scanf("%d",&num);
int a[num];
for (i=0;i<num;i++)
{
printf("Enter the Number %d:",i+1);
scanf("%d",&a[i]);
}
for (i=0;i<num-1;i++)
{
for (j=i+1;j<num;j++)
{
if (a[j]<a[i])
{
c=a[i];
a[i]=a[j];
a[j]=c;
}
}
}
printf("the
ascending order isn");
for (i=0;i<num;i++)
{
printf("%dn",a[i]);
}
return 0;
}
13.A program for the Matrix Multiplication of any oredered two matrices.
#include<stdio.h>
#include<conio.h>
int main()
{
int r1,c1,r2,c2,i,j,k;
printf("Matrix An");
printf("Enter No of Rows:");
scanf("%d",r1);
printf("Enter No of Columns:");
scanf("%d",c1);
printf("Matrix Bn");
printf("Enter No of Rows:");
scanf("%d",r2);
printf("Enter No of Columns:");
scanf("%d",c2);
int a[r1][c1], b[r2][c2], c[r1][c2];
clrscr();
if (c1!=r2)
{
printf("Matrix
Multiplication A*B not Possible.n");
}
else
{
printf("Enter Elements for Matrix An");
for (i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("Element[A%d%d]:",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("n");
}
printf("Enter Elements for Matrix Bn");
for (i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("Element[B%d%d]:",i+1,j+1);
scanf("%d",&b[i][j]);
}
printf("n");
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<r2;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("Matrix A*Bn");
for (i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%6d",c[i][j]);
}
printf("n");
}
}
getch();
return 0;
}
14.A program to find transpose of any square matrix
#include<stdio.h>
int main()
{
int n;
printf("give the dimension for square matrix:");
scanf("%d",&n);
int a[n][n],t[n][n],i,j;
printf("enter the elements of the matrixn");
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
printf("n");
}
printf("the entered matrix isn");
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
t[j][i]=a[i][j];
printf("%6d",a[i][j]);
}
printf("n");
}
printf("the transpose matrix isn");
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
printf("%6d",t[i][j]);
}
printf("n");
}
return 0;
}
15.A program to find Determinant of any 3*3 square matrix
#include<stdio.h>
#include<conio.h>
int main()
{
int A[3][3];
int i,j,det;
clrscr();
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
scanf("%d",&A[i][j]);
}
}
i = 0;
det = 0;
for ( j = 0 ; j < 3 ; j++)
{
switch (j)
{
case 2:
det += A[i][j] * A[i+1][0] * A[i+2][1];
break;
case 1:
det += A[i][j] * A[i+1][j+1] * A[i+2][0];
break;
default:
det += A[i][j] * A[i+1][j+1] * A[i+2][j+2];
break;
}
}
i = 2;
for ( j = 0 ; j < 3 ; j++)
{
switch (j)
{
case 2:
det -= A[i][j] * A[i-1][0] * A[i-2][1];
break;
case 1:
det -= A[i][j] * A[i-1][j+1] * A[i-2][0];
break;
default:
det -= A[i][j] * A[i-1][j+1] * A[i-2][j+2];
break;
}
}
printf("The determinant is %dn",det);
getch();
return 0;
}
16.A program to enter details of 5 students and print their details alphabetically respectively using STRUCTURE
#include<stdio.h>
#include<string.h>
#include<conio.h>
struct student
{
char name[20];
int classs;
int Roll_no;
int mark[3];
};
int main()
{
char temp[20];
int i,j,k,tempdata;
struct student s[5];
clrscr();
for (i=0;i<5;i++)
{
printf("Name:");
scanf("%s",s[i].name);
printf("Class:");
scanf("%d",&s[i].classs);
printf("Roll no:");
scanf("%d",&s[i].Roll_no);
for(j=0;j<3;j++)
{
printf("Mark in Subject %d:",j+1);
scanf("%d",&s[i].mark[j]);
}
printf("n");
}
for(i=0;i<5-1;i++)
{
for(j=i+1;j<5;j++)
{
if(strcmp(s[i].name,s[j].name)>0)
{
strcpy(temp,s[i].name);
strcpy(s[i].name,s[j].name);
strcpy(s[j].name,temp);
tempdata=s[i].classs;
s[i].classs=s[j].classs;
s[j].classs=tempdata;
tempdata=s[i].Roll_no;
s[i].Roll_no=s[j].Roll_no;
s[j].Roll_no=tempdata;
for(k=0;k<3;k++)
{
tempdata=s[i].mark[k];
s[i].mark[k]=s[j].mark[k];
s[j].mark[k]=tempdata;
}
}
}
}
printf("Datas sorrted Alphabeticallyn");
for(i=0;i<5;i++)
{
printf("Name:%sn",s[i].name);
printf("Class:%dn",s[i].classs);
printf("Roll no:%dn",s[i].Roll_no);
for (j=0;j<3;j++)
{
printf("Mark in Subject %d:",j+1);
printf("%dn",s[i].mark[j]);
}
printf("n");
}
getch();
return 0;
}
17.A program to find whether the given Quadratic Equation result Real or Imaginary Solution
#include<stdio.h>
#include<conio.h>
int quad(int x,int y,int z)
{
int ans;
ans=((y*y)-(4*x*z));
if (ans>=0)
return 1;
else
return 0;
}
int main()
{
int a,b,c,answer;
clrscr();
printf("Enter the Value of the Variable a:");
scanf("%d",&a);
printf("Enter the Value of the Variable b:");
scanf("%d",&b);
printf("Enter the Value of the Variable c:");
scanf("%d",&c);
answer=quad(a,b,c);
if (answer==1)
printf("The Given Quadratic Equation has Real Solution");
else
printf("The Given Quadratic Equation has Imaginary Solution");
getch();
return 0;
}
18.A program to solve Matrix Multiplication by POINTER
#include<stdio.h>
#include<conio.h>
int main()
{
int r1,c1,r2,c2,i,j,k;
int a[100][100],b[100][100],c[100][100];
int *matrixc,*matrixa,*matrixb;
clrscr();
printf("Matrix An");
printf("Enter No of Rows:");
scanf("%d",&r1);
printf("Enter No of Columns:");
scanf("%d",&c1);
printf("Matrix Bn");
printf("Enter No of Rows:");
scanf("%d",&r2);
printf("Enter No of Columns:");
scanf("%d",&c2);
if (c1==r2)
{
printf("Enter Elements for Matrix An");
for (i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
matrixa=&a[i][j];
printf("Element[A%d%d]:",i+1,j+1);
scanf("%d",&*matrixa);
}
printf("n");
}
printf("Enter Elements for Matrix Bn");
for (i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
matrixb=&b[i][j];
printf("Element[B%d%d]:",i+1,j+1);
scanf("%d",&*matrixb);
}
printf("n");
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
matrixc=&c[i][j];
*matrixc=0;
for(k=0;k<r2;k++)
{
matrixa=&a[i][k];
matrixb=&b[k][j];
*matrixc=*matrixc + (*matrixa * *matrixb);
}
}
}
printf("Matrix A*Bn");
for (i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
matrixc=&c[i][j];
printf("%6d",*matrixc);
}
printf("n");
}
}
else
{
printf("Matrix Multiplication A*B not Possible.n");
}
getch();
return 0;
}
19.A program to find greatest number in user entered 10 numbers using FUNCTION
#include<stdio.h>
#include<conio.h>
int larg(n1,n2)
{
int max;
max=(n1>n2)?n1:n2;
return max;
}
int main()
{
int x,y,i,answer;
clrscr();
printf("enter a number:");
scanf("%d",&x);
for (i=2;i<=10;i++)
{
printf("enter next number:");
scanf("%d",&y);
answer=larg(x,y);
x=answer;
}
printf("the largset number is %d",answer);
getch();
return 0;
}
20.A program to find whether user entered number is within the given array or not using FUNCTION
#include<stdio.h>
#define size 10
int search(int a[],int num)
{
int i,flag=0;
for (i=0;i<size;i++)
{
if (num==a[i])
{
flag=1;
return 1;
break;
}
}
if (flag==0) return 0;
}
int main()
{
int data[size]={3,9,1,0,5,7,10,6,4,2};
int key,answer;
printf("Enter a Number:");
scanf("%d",&key);
answer=search(data, key);
if (answer==1)
printf("found");
else
printf("not found");
return 0;
}
21.A program to find HCF/GCD of two numbers using RECURSION
#include<stdio.h>
int gcd(int x,int y,int z)
{
int ans1;
if (x%z==0 && y%z==0) return z;
else
{
z--;
ans1=gcd(x,y,z);
return ans1;
}
}
int main()
{
int num1,num2,min,answer;
printf("Enter the first number:");
scanf("%d",&num1);
printf("Enter the second number:");
scanf("%d",&num2);
min=(num1>num2)?num2:num1;
answer=gcd(num1,num2,min);
printf("gcd=%d",answer);
return 0;
}
22.A program to find Factorial of a number using RECURSION
#include<stdio.h>
int fact(int num)
{
if (num==0) return 1;
else
return(num*fact(num-1));
}
int main()
{
int num,answer;
printf("Enter a number:");
scanf("%d",&num);
answer=fact(num);
printf("factorial of %d is %d",num,answer);
return 0;
}
22.A program to print Fibonnaci Series as:0 1 1 2 3 5 8 13 21..................n terms
#include<stdio.h>
int main()
{
int n,i,a,b;
printf("Enter number upto which series to be printed:");
scanf("%d",&n);
a=0;
b=1;
printf("%dn",a);
for (i=1;i<=n;i++)
{
a=a+b;
b=a-b;
printf("%dn",a);
}
return 0;
}
23.A program to find factorial of a number
#include<stdio.h>
int main()
{
int a,i,mul=1;
printf("enter a number:");
scanf("%d",&a);
for (i=1;i<=a;i++)
{
mul=mul*i;
}
printf("factorial:%d",mul);
return 0;
}
24.A program to find sum of a given array using POINTER and FUNCTION
#include<stdio.h>
#define SIZE 5
int getsum(int *px)
{
int i;
int total=0;
for(i=0;i<SIZE;i++)
{
total=total + *px;
px=px+1;
}
return total;
}
int main()
{
int data[SIZE]={1,2,3,4,5};
int sum=0;
sum=getsum(data);
printf("Sum=%dn",sum);
return 0;
}
25.A program to swap two values using FUNCTION and POINTER
#include<stdio.h>
void swap(int *a, int *b)
{
int *c;
c=&a;
*a=*b;
*b=*c;
}
int main()
{
int x=5;
int y=7;
printf("Initial Values of x and y are %d and %d.n",x,y);
swap(&x, &y);
printf("After swappingn");
printf("x= %dn",x);
printf("y= %dn",y);
return 0;
}
26.A program to find nth fibonnaci number using RECURSION
#include<stdio.h>
int sum(int num)
{
if (num==1 || num==2) return 1;
else
return(sum(num-1)+sum(num-2));
}
int main()
{
int num,answer;
printf("Enter a number:");
scanf("%d",&num);
answer=sum(num);
printf("%d th fibonnaccci number is %d",num,answer);
return 0;
}
27.A program to find sum of n natural numbers using RECURSION
#include<stdio.h>
int sum(int num)
{
if (num==0) return 0;
else
return(num+sum(num-1));
}
int main()
{
int num,answer;
printf("Enter a number:");
scanf("%d",&num);
answer=sum(num);
printf("sum upto natural integer %d is %d",num,answer);
return 0;
}
28.A program to print following output
1
12
123
1234
12345
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int a=1,b=2,i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
putchar(' ');
}
printf("%dn",a);
a=(a*10)+b;
b++;
}
getch();
return 0;
}
29.A program to convert a user entered sentence into uppercase.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define eol 'n'
int main()
{
char sentence[255];
int count=0,flag;
clrscr();
printf("Enter a sentence:n");
while((sentence[count]=getchar())!=eol)
{
count++;
}
flag=count;
count=0;
while(count<flag)
{
putchar(toupper(sentence[count]));
count++;
}
getch();
return 0;
}
30.A program to print following output.
*
***
*****
*******
*********
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
int a=1,i,j,k;
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
putchar(' ');
}
for(k=1;k<=a;k++)
{
putchar('*');
}
a=a+2;
printf("n");
}
getch();
return 0;
}
31.A program to print following output.
1
121
12321
1234321
123454321
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
long int a=1,i,j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
putchar(' ');
}
printf("%ldn",a*a);
a=(a*10)+1;
}
getch();
return 0;
}
32.A program to print following output.
5
54
543
5432
54321
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
long int a=0,i;
clrscr();
for(i=5;i>=1;i--)
{
a=(a*10)+i;
printf("%ldn",a);
}
getch();
return 0;
}
33.A program to print following output.
1
21
321
4321
54321
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
long int a=1,i,j,sum=0;
clrscr();
for(i=1;i<=5;i++)
{
for(a=1,j=1;j<i;j++)
{
a=a*10;
}
a=a*i;
sum=sum+a;
printf("%ldn",sum);
}
getch();
return 0;
}
34.A program to print following output.
1
212
32123
4321234
543212345
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
long int a=1,z,b,c,i,j,sum=0,sum1,sum2,count=5;
clrscr();
for(i=1;i<=5;i++)
{
/*generating the front part of structure including 1*/
for(a=1,j=1;j<i;j++)
{
a=a*10;
}
z=a;
a=a*i;
sum=sum+a;
/*reversing the front part of structure excluding 1*/
sum1=sum/10;
c=0;
while(sum1>0)
{
b=sum1%10;
c=(c*10)+b;
sum1=sum1/10;
}
/*combining front part of structure including 1 and its reverse part excluding 1*/
sum2=(sum*z)+c;
/*printing spaces*/
for(j=1;j<count;j++)
{
putchar(' ');
}
count--;
/*printing final output*/
printf("%ldn",sum2);
}
getch();
return 0;
}
35.A program to print following output.
12345
1234
123
12
1
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
long int a=12345;
int i;
clrscr();
for(i=5;i>=1;i--)
{
printf("%ldn",a);
a=(a-i)/10;
}
getch();
return 0;
}
36.A program to print following output.
12345
1234
123
12
1
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
long int a=12345;
int i,j,count=0;
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<=count;j++)
{
putchar(' ');
}
count++;
printf("%ldn",a);
a=(a-i)/10;
}
getch();
return 0;
}
37.A program to print following output.
1
12
123
1234
12345
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
long int a=0;
int i;
clrscr();
for(i=1;i<=5;i++)
{
a=(a*10)+i;
printf("%ldn",a);
}
getch();
return 0;
}
38.A program to print following output.
5
54
543
5432
54321
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
long int a=0,i,j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
putchar(' ');
}
a=(a*10)+i;
printf("%ldn",a);
}
getch();
return 0;
}
39.A program to print following output.
54321
5432
543
54
5
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
long int a=54321,i;
clrscr();
for(i=1;i<=5;i++)
{
printf("%ldn",a);
a=(a-i)/10;
}
getch();
return 0;
}
40.A program to print following output.
54321
5432
543
54
5
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
long int a=54321,i,j,count=0;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=count;j++)
{
putchar(' ');
}
++count;
printf("%ldn",a);
a=(a-i)/10;
}
getch();
return 0;
}
41.A program to print following output.
54321
4321
321
21
1
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
long int a=54321,i,b=10000;
clrscr();
for(i=1;i<=5;i++)
{
printf("%ldn",a);
a=a%b;
b=b/10;
}
getch();
return 0;
}
42.A program to print following output.
54321
4321
321
21
1
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
long int a=54321,i,b=10000,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
putchar(' ');
}
printf("%ldn",a);
a=a%b;
b=b/10;
}
getch();
return 0;
}
43.A program to print following output.(see another way above)
1
21
321
4321
54321
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
long int a=54321,b=10,i;
clrscr();
for(i=1;i<=5;i++)
{
printf("%ldn",a%b);
b=b*10;
}
getch();
return 0;
}
44.A program to print following output.(see another way above)
1
212
32123
4321234
543212345
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
long int a=54321,z=1,b,c,i,j,sum=0,sum1,sum2,count=5,s=10;
clrscr();
for(i=1;i<=5;i++)
{
/*generating the front part of structure including 1*/
sum=a%s;
s=s*10;
/*reversing the front part of structure excluding 1*/
sum1=sum/10;
c=0;
while(sum1>0)
{
b=sum1%10;
c=(c*10)+b;
sum1=sum1/10;
}
/*combining front part of structure including 1 and its reverse part excluding 1*/
sum2=(sum*z)+c;
z=z*10;
/*printing spaces*/
for(j=1;j<count;j++)
{
putchar(' ');
}
count--;
/*printing final output*/
printf("%ldn",sum2);
}
getch();
return 0;
}
45.A program to find whether the input number is armstrong or not.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<math.h>
int main()
{
long int num,save,sum=0;
clrscr();
printf("Enter a Number:");
scanf("%ld",&num);
save=num;
while(num>0)
{
sum=sum+pow((num%10),3);
num=num/10;
}
if (sum==save)
printf("The Number is Armstrong.n");
else
printf("The Number is not Armstrong.n");
getch();
return 0;
}
46.A program to convert user entered binary number into decimal.
#include<stdio.h>
#include<conio.h>
long int pow(long int d)
{
long int m=1,q;
if (d==0)
{
return 1;
}
else
{
for(q=1;q<=d;q++)
{
m=m*2;
}
return m;
}
}
long int deci(long int b)
{
long int c=0,p=0;
while(b>=1)
{
c=c+ ((b%10)*pow(p));
b=b/10;
p=p+1;
}
return c;
}
long int main()
{
long int a,r;
clrscr();
printf("Binary Number:");
scanf("%ld",&a);
r=deci(a);
printf("decimal equivalent: %ld",r);
getch();
return 0;
}
47.A program to print following output. A better, efficient, logically easier way with a new treatment to string behavior...........
1
212
32123
4321234
543212345
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,j,m=5,k;
clrscr();
for(i=1;i<=5;i++)
{
/*printing front spaces*/
for (j=1;j<=m;j++)
{
printf(" ");
}
m--;
/*generating and printing the series structure*/
for(k=-i;k<=i;k++)
{
if (k==0 || k==-1)
{
}
else
{
printf("%d",abs(k));
}
}
printf("n");
}
getch();
return 0;
}
48.A program to print following output.
54321
4321
321
21
1
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,j;
clrscr();
for(i=5;i>=1;i--)
{
for (j=i;j>=1;j--)
{
printf("%d",j);
}
printf("n");
}
getch();
return 0;
}
49.A program that takes an integer, calculates it in reverse order and adds it to original number(eg. if the number is 12345, calculate 12345+54321)
#include<stdio.h>
#include<conio.h>
int main()
{
long int num,num_org,b=0;
clrscr();
printf("Enter a number:");
scanf("%ld",&num);
num_org=num;
while(num>=1)
{
b=(b*10)+(num%10);
num=num/10;
}
printf("the reverse of the number %ld is %ld and their sum is %ld.n",num_org,b,num_org+b);
getch();
return 0;
}
50.A program that takes an integer, calculates the average of its digits( eg. if the number is 12345, calculate (1+2+3+4+5)/5 )
#include<stdio.h>
#include<conio.h>
int main()
{
long int num,b=0;
long float c;
float count=0;
clrscr();
printf("Enter a number:");
scanf("%ld",&num);
while(num>=1)
{
b=b+(num%10);
num=num/10;
count++;
}
c=b/count;
printf("the sum of digits: %ldn",b);
printf("the average of digits: %lf",c);
getch();
return 0;
}
51.A program read a character and display whether it is an uppercase alphabet or lowercase alphabet or number or a special symbol.
#include<stdio.h>
#include<conio.h>
int main()
{
int letter;
clrscr();
printf("Enter a character:");
letter=getchar();
if (letter>=65 && letter<=90)
{
printf("The character is an UPPERCASE CHARACTER.n");
}
else if (letter>=97 && letter <=122)
{
printf("The character is a lowercase character.n");
}
else if (letter>=48 && letter<=89)
{
printf("The character is a Number.n");
}
else
{
printf("The character is a special character.n");
}
getch();
return 0;
}
OR
#include<stdio.h>
#include<conio.h>
int main()
{
char letter[1];
clrscr();
printf("Enter a character:");
scanf("%[^n]",letter);
if ((letter[0] >= 'a') && (letter[0] <= 'z'))
{
printf("The character is a lowercase alphabet.");
}
else if ((letter[0] >= 'A') && (letter[0] <= 'Z'))
{
printf("The character is an UPPERCASE ALPHABET.");
}
else if ((letter[0] >= '0') && (letter[0] <= '9'))
{
printf("The character is a number.");
}
else
{
printf("The symbol is a special symbol.");
}
getch();
return 0;
}
52.A program that reads a character and converts it into uppercase if it is in lowercase and viceversa.
#include<stdio.h>
#include<conio.h>
int main()
{
int letter;
clrscr();
printf("Enter a character:");
letter=getchar();
if (letter>=65 && letter<=90)
{
putchar(tolower(letter));
}
else if (letter>=97 && letter <=122)
{
putchar(toupper(letter));
}
else
{
printf("The character is Illegal.n");
}
getch();
return 0;
}
53.A program to display all Armstrong Numbers between a Range.
or
Write a program to display all the three digit Armstrong numbers. ( Armstrong number is the one which is equal to the sum of the cubes of the individual digits. )
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
long int num1,num2,save,save1,sum=0,i;
clrscr();
printf("Enter lower limit of range:");
scanf("%ld",&num1);
printf("Enter upper limit of range:");
scanf("%ld",&num2);
if(num1<=num2)
{
for(i=num1;i<=num2;i++)
{
save=i;
save1=i;
while(save>0)
{
sum=sum+pow((save%10),3);
save=save/10;
}
if (save1==sum)
{
printf("%dn",save1);
}
sum=0;
}
}
else
{
printf("lower limit is greater than upper limit.n");
}
printf("n");
printf("Press any key to continue.n");
getch();
return 0;
}
54.A program to read a number and print its multiplication table up to 10.
#include<stdio.h>
#include<conio.h>
int main()
{
long int num;
int i;
clrscr();
printf("Enter the number whose multiplication table is to be printed:");
scanf("%ld",&num);
for(i=1;i<=10;i++)
printf("%ld t * t %d = %ld n",num,i,num*i);
getch();
return 0;
}
55.A program to generate the series below:
1*2+2*3+3*4+..............upto N terms
#include<stdio.h>
#include<conio.h>
int main()
{
long int num,i;
clrscr();
printf("Enter the number upto which series is to be printed: ");
scanf("%ld",&num);
for(i=1;i<=num;i++)
{
if (i!=num)
{
printf("%ld*%ld+",i,i+1);
}
else
{
printf("%ld*%ld",i,i+1);
}
}
getch();
return 0;
}
Note: Better not to give treatment to the problems like in Example 47 & 48. As far as Possible give a mathematical treatment to the problems relating series printing since it is very much helpful in logic and series recognizing capability.