0% found this document useful (0 votes)
169 views

C Programs: Program To Demostrate Arithmetic Operators

The document contains programs written in C programming language to demonstrate various concepts like arithmetic operators, data types, switch-case, functions, loops, pointers, unions, and more. Some key programs include: 1) A program that demonstrates arithmetic operators like addition, subtraction, multiplication, division etc on an integer variable. 2) Programs to find the size of different data types and print them. 3) Programs using concepts like switch-case, ternary operator, functions, loops, arrays etc. 4) Programs to calculate area and circumference of a circle, surface area and volume of a cube, simple and compound interest, roots of quadratic equations and more.

Uploaded by

Aishwary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
169 views

C Programs: Program To Demostrate Arithmetic Operators

The document contains programs written in C programming language to demonstrate various concepts like arithmetic operators, data types, switch-case, functions, loops, pointers, unions, and more. Some key programs include: 1) A program that demonstrates arithmetic operators like addition, subtraction, multiplication, division etc on an integer variable. 2) Programs to find the size of different data types and print them. 3) Programs using concepts like switch-case, ternary operator, functions, loops, arrays etc. 4) Programs to calculate area and circumference of a circle, surface area and volume of a cube, simple and compound interest, roots of quadratic equations and more.

Uploaded by

Aishwary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56

C PROGRAMS

PROGRAM TO DEMOSTRATE ARITHMETIC OPERATORS

# include <stdio.h>
int main()
{
int i;
i = 5;
printf("The Value of i is : %d", i);
i = i + 5;
printf("\nThe Value of i is : %d", i);
i = i - 2;
printf("\nThe Value of i is : %d", i);
i = i * 2;
printf("\nThe Value of i is : %d", i);
i = i / 4;
printf("\nThe Value of i is : %d", i);
i++;
printf("\nThe Value of i is : %d", i);
i++;
printf("\nThe Value of i is : %d", i);
i --;
printf("\nThe Value of i is : %d", i);
}

PROGRAM TO FIND SIZE OF DATA TYPE

#include <stdio.h>
main()
{
printf(" short int is %2d bytes \n", sizeof(short int));
printf(" int is %2d bytes \n", sizeof(int));
printf(" int * is %2d bytes \n", sizeof(int *));
printf(" long int is %2d bytes \n", sizeof(long int));
printf(" long int * is %2d bytes \n", sizeof(long int *));
printf(" signed int is %2d bytes \n", sizeof(signed int));
printf(" unsigned int is %2d bytes \n", sizeof(unsigned int));
printf("\n");
printf(" float is %2d bytes \n", sizeof(float));
printf(" float * is %2d bytes \n", sizeof(float *));
printf(" double is %2d bytes \n", sizeof(double));
printf(" double * is %2d bytes \n", sizeof(double *));
printf(" long double is %2d bytes \n", sizeof(long double));
printf("\n");
printf(" signed char is %2d bytes \n", sizeof(signed char));
printf(" char is %2d bytes \n", sizeof(char));
printf(" char * is %2d bytes \n", sizeof(char *));
printf("unsigned char is %2d bytes \n", sizeof(unsigned char));
}

PROGRAM TO DEMOSTRATE SWITCH-CASE

#include<stdio.h>
int main()
{
int num1,num2,opt;
printf("Enter the first Integer:\n");
scanf("%d",&num1);
printf("Enter the second Integer:\n");
scanf("%d",&num2);
for(;;)
{
printf("\n\n\n\nEnter the your option:\n");
printf("1-Addition.\n2-Substraction.\n3-Multiplication.\n4-Division.\n5-Exit.\n");
scanf("%d",&opt);
switch(opt)
{
case 1:printf("\nAddition of %d and %d is: %d",num1,num2,num1+num2);
break;
case 2:printf("\nSubstraction of %d and %d is: %d",num1,num2,num1-num2);
break;
case 3:printf("\nMultiplication of %d and %d is: %d",num1,num2,num1*num2);
break;
case 4:
if(num2==0)
{
printf("OOps Devide by zero\n");
}
else{
printf("\n Division of %d and %d is: %d",num1,num2,num1/num2);
}
break;
case 5: return 0;
break;
default:printf("\n Enter correct option\n");
break;
}
}
}
PROGRAM TO DEMOSTRATE TERNARY OPERATOR

#include<stdio.h>
int main()
{
int min,x,y;
printf("Enter two numbers\n");
scanf("%d%d",&x,&y);
min=x <= y ? x : y;
printf("\n Small number is: %d",min);
return 0;
}

PROGRAM TO DEMOSTRATE FUNCTION WITH ARGUMENTS

#include<stdio.h>
int add( int, int); /* Function declaration */
main()
{
int i=1;
printf("i starts out life as %d.", i);
i = add(1, 1); /* Function call */
printf(" And becomes %d after function is executed.\n", i);
}

int add( int a, int b) /* Function definition */


{
int c;
c = a + b;
return c;
}

PROGRAM TO PRINT TABLE

#include <stdio.h>
int main() {
int num, i = 1;
printf("\n Enter any Number:");
scanf("%d", &num);
printf("Multiplication table of %d: \n", num);
while (i <= 10) {
printf("\n %d x %d = %d", num, i, num * i);
i++;
}
return 0;
}
PROGRAM TO FIND THE SUM OF ALL DIGITS OF AN INTEGER NUMBER

#include <stdio.h>
void main()
{
long num, temp, digit, sum = 0;
printf("Enter the number\n");
scanf("%ld", &num);
temp = num;
while(num > 0)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}
printf("Given number =%ld\n", temp);
printf("Sum of the digits %ld =%ld\n", temp, sum);
}

PROGRAM TO COMPUTE THE SURFACE AREA AND VOLUME

#include <stdio.h>
#include <math.h>
void main()
{
float side, surfArea, volume;
printf("Enter the length of a side\n");
scanf("%f", &side);
surfArea = 6.0 * side * side;
volume = pow (side, 3);
printf("Surface area = %6.2f and Volume = %6.2f\n", surfArea, volume);
}

PROGRAM TO PRINT PYRAMID

#include<stdio.h>
int main()
{
int r, c,t=5;
for ( r = 1 ; r <= 5 ; r++ )
{
for ( c = 1 ; c < t ; c++ )
printf(" ");
t--;
for ( c = 1 ; c <= 2*r - 1 ; c++ )
printf("*");
printf("\n");
}
}

PROGRAM TO FIND THE ROOTS OF QUADRATIC EQUATION

#include<stdio.h>
#include<math.h>
main()
{
int a,b,c;
float disc,r1,r2;
for(;;)
{
printf("\nEnter the non Zero co-efficients a, b,c\n");
scanf("%d%d%d",&a,&b,&c);
if((a==0) || (b==0) || (c==0))
{
printf("\nPlease enter non zero co-efficients \n");
}else{
disc=((b*b)-(4*a*c));
if(disc>0)
{
printf("Roots are Real:\n");
r1=(-b-(sqrt(disc)))/(2.0*a);
r2=(-b+(sqrt(disc)))/(2.0*a);
printf("Roots are:%f and %f \n", r1,r2);
}
else if(disc<0)
{
printf("Roots are Imaginary:\n");
r1=-b/(2.0*a);
printf("First root: %lf +%fi \n", r1,sqrt(-disc)/(2.0*a));
printf("Second root:%lf -%lfi\n", r1,sqrt(-disc)/(2.0*a));
}
else
{
printf("Roots are Equal\n");
r1= -b/(2.0*a);
printf("Equal; roots are:%f and %f \n", r1,r1);
}
return 0;
}
}
}

PROGRAM TO INPUT REAL NUMBERS AND FIND THE MEAN, VARIANCE AND
STANDARD DEVIATION

#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAXSIZE 10
void main()
{
float x[MAXSIZE];
int i, n;
float avrg, var, SD, sum=0, sum1=0;
clrscr();
printf("Enter the value of N\n");
scanf("%d", &n);
printf("Enter %d real numbers\n",n);
for(i=0; i<n; i++)
{
scanf("%f", &x[i]);
}
/* Compute the sum of all elements */
for(i=0; i<n; i++)
{
sum = sum + x[i];
}
avrg = sum /(float) n;
/* Compute varaience and standard deviation */
for(i=0; i<n; i++)
{
sum1 = sum1 + pow((x[i] - avrg),2);
}
var = sum1 / (float) n;
SD = sqrt(var);
printf("Average of all elements = %.2f\n", avrg);
printf("Varience of all elements = %.2f\n", var);
printf("Standard deviation = %.2f\n", SD);
}

PROGRAM TO FIND SIMPLE INTEREST

#include <stdio.h>
#include <conio.h>

void main()
{
float p, r, si;
int t;
clrscr();
printf("Enter the values of p,r and t\n");
scanf ("%f %f %d", &p, &r, &t);
si = (p * r * t)/ 100.0;
printf ("Amount = Rs. %5.2f\n", p);
printf ("Rate = Rs. %5.2f%\n", r);
printf ("Time = %d years\n", t);
printf ("Simple interest = %5.2f\n", si);
}

PROGRAM TO FIND AREA OF CIRCLE

#include <stdio.h>
#include <conio.h>
#include <math.h>
#define PI 3.142
void main()
{
float radius, area;
clrscr();
printf("Enter the radius of a circle\n");
scanf ("%f", &radius);
area = PI * pow (radius,2);
printf ("Area of a circle = %5.2f\n", area);
}

PROGRAM TO FIND THE TYPE OF TRIANGLE

#include<stdio.h>
#include<math.h>
int main()
{
int a, b, c;
float s, area;
printf("Enter the values of the sides of the triangle: \n");
scanf("%d %d %d", &a, &b, &c);
if ((a + b > c && a + c > b && b + c > a) && (a > 0 && b > 0 && c > 0)) {
s = (a + b + c) / 2.0;
area = sqrt((s * (s - a) * (s - b) * (s - c)));
if (a == b && b == c) {
printf("Equilateral Triangle. \n");
printf("Area of Equilateral Triangle is: %f", area);
}
else if (a == b || b == c || a == c) {
printf("Isosceles Triangle. \n");
printf("Area of an Isosceles Triangle: %f", area);
}
else {
printf("Scalene Triangle. \n");
printf("Area of Scalene Triangle: %f", area);
}
}
else {
printf("Triangle formation not possible");
}
return 0;
}

WRITE A PROGRAM IN C TO CHECK WHETHER ENTERED NUMBER IS PRIME


OR NOT

#include <stdio.h>
#include <conio.h>
main()
{
int n, i, m,flag=0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
getch();
}

PROGRAM TO FIND THE BIGGEST OF THREE NUMBERS

#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter the value for a:\n");
scanf("%d", &a);
printf("Enter the value for b:\n");
scanf("%d", &b);
printf("Enter the value for c:\n");
scanf("%d", &c);
if ((a > b) && (a > c)) {
printf("\n The big one is a= %d", a);
} else if (b > c) {
printf("\n The big one is b= %d", b);
} else {
printf("\n The big one is c= %d", c);
}
return 0;
}

PROGRAM TO DEMONSTRATE UNION

#include <stdio.h>
#include <conio.h>

void main()
{
union number
{
int n1;
float n2;
};
union number x;
clrscr() ;
printf("Enter the value of n1: ");
scanf("%d", &x.n1);
printf("Value of n1 =%d", x.n1);
printf("\nEnter the value of n2: ");
scanf("%d", &x.n2);
printf("Value of n2 = %d\n",x.n2);
}

PROGRAM TO CONVERT DAYS TO YEARS, WEEKS AND DAYS

#include <stdio.h>
#define DAYSINWEEK 7
void main()
{
int ndays, year, week, days;
printf("Enter the number of days\n");
scanf("%d",&ndays);
year = ndays/365;
week = (ndays % 365)/DAYSINWEEK;
days = (ndays%365) % DAYSINWEEK;
printf ("%d is equivalent to %d years, %d weeks and %d days\n",
ndays, year, week, days);
}

PROGRAM TO CALCULATE AREA OF CIRCLE USING POINTER


#include<stdio.h>
void areaperi ( int r, float *a, float *p )
{
*a = 3.14 * r * r ;
*p = 2 * 3.14 * r ;
}
void main( )
{
int radius ;
float area, perimeter ;
printf ( "nEnter radius of a circle " ) ;
scanf ( "%d", &radius ) ;
areaperi ( radius, &area, &perimeter ) ;
printf ( "Area = %f", area ) ;
printf ( "nPerimeter = %f", perimeter ) ;
}

PROGRAM TO CALCULATE GROSS SALARY OF EMPLOYEE


#include<stdio.h>
#include<conio.h>
void main()
{
int gross_salary,basic,da,ta;
clrscr();
printf("Enter basic salary : ");
scanf("%d",&basic);
da = ( 10 * basic ) / 100;
ta = ( 12 * basic ) / 100;
gross_salary = basic + da + ta;
printf("Gross salary : %d",gross_salary);
getch();
}

PERFECT NUMBER: Perfect number is a positive number which sum of all positive divisors
excluding that number is equal to that number. For example 6 is perfect number since divisor of
6 are 1, 2 and 3. Sum of its divisor is
1 + 2+ 3 =6

Note: 6 is the smallest perfect number

PROGRAM TO CHECK THE ENTERED NUMBER IS PERFECT NUMBER OR NOT

#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;
}
PROGRAM TO FIND PERFECT NUMBERS

#include<stdio.h>
int main()
{
int n,i,sum;
int min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Perfect numbers in given range is: ");
for(n=min;n<=max;n++){
i=1;
sum = 0;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d ",n);
}
return 0;
}

PROGRAM TO CHECK GIVEN NUMBER IS ARMSTRONG NUMBER OR NOT.

#include<stdio.h>
int main()
{
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}

PROGRAM TO CHECK GIVEN NUMBER IS PRIME NUMBER OR NOT.

PROGRAM TO FIND THE REVERSE OF USER GIVEN NUMBER

#include<stdio.h>
#include<conio.h>
void main()
{
int num,rem,rev=0;
clrscr();
printf("nEnter any no to be reversed : ");
scanf("%d",&num);
while(num>=1)
{
rem = num % 10;
rev = rev * 10 + rem;
num = num / 10;
}
printf("nReversed Number : %d",rev);
getch();
}

PROGRAM TO CHECK GIVEN NUMBER IS STRONG NUMBER OR NOT

#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;
}
if(sum==temp)
printf("%d is a strong number",temp);
else
printf("%d is not a strong number",temp);
return 0;
}

PROGRAM TO CONVERT TEMPERATURE FROM DEGREE CENTIGRADE TO


FAHRENHEIT

#include<stdio.h>
#include<conio.h>
void main()
{
float celsius,fahrenheit;
clrscr();
printf("nEnter temp in Celsius : ");
scanf("%f",&celsius);
fahrenheit = (1.8 * celsius) + 32;
printf("nTemperature in Fahrenheit : %f ",fahrenheit);
getch();
}

PROGRAM TO CHECK GIVEN NUMBER IS PALINDROME NUMBER OR NOT.

#include<stdio.h>
#include<string.h>
int main()
{
int num,i,count=0;
char str1[10],str2[10];
printf("nEnter a number:");
scanf("%d",&num);
sprintf(str1,"%d",num); // Convert Number to String
strcpy(str2,str1); // Copy String into Other
strrev(str2); // Reverse 2nd Number
count = strcmp(str1,str2);
if(count==0)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}
PROGRAM WHICH PASSES ONE DIMENSION ARRAY TO FUNCTION.

#include <stdio.h>
#define N 5
void fstore1D(int a[], int a_size);
void fretrieve1D(int a[], int a_size);
void fedit1D(int a[], int a_size);
int main()
{
int a[N];
printf("Input data into the matrix:\n");
fstore1D(a, N);
fretrieve1D(a, N);
fedit1D(a, N);
fretrieve1D(a, N);
return 0;
}
void fstore1D(int a[], int n)
{
int i;
for ( i = 0; i < n; ++i )
scanf("%d", &a[i]);
}
void fretrieve1D(int a[], int n)
{
int i;
for ( i = 0; i < n; ++i )
printf("%6d ", a[i]);
printf("\n");
}
void fedit1D(int a[], int n)
{
int i, q;
for ( i = 0; i < n; ++i ){
printf("Prev. data: %d\nEnter 1 to edit 0 to skip.", a[i]);
scanf("%d", &q);
if ( q == 1 ){
printf("Enter new value: ");
scanf("%d", &a[i]);
}
}
}

PROGRAM TO FIND FACTORIAL OF NUMBER USING RECURSION


#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int x,n;
printf("nEnter the value of n :");
scanf("%d",&n);
x=fact(n);
printf("n%d",x);
getch();
}
int fact(int n)
{
if(n==0)
return(1);
return(n*fact(n-1));
}

PROGRAM TO FIND FACTORIAL OF NUMBER WITHOUT USING FUNCTION


#include<stdio.h>
#include<conio.h>
void main()
{
int i,number,factorial;
printf("nEnter the number : ");
scanf("%d",&n);
factorial = 1;
for(i=1;i<=n;i++)
factorial = factorial * i;
printf("nFactorial of %d is %d",n,factorial );
getch();
}

PROGRAM TO DELETE DUPLICATE ELEMENTS IN AN ARRAY

#include<stdio.h>
#include<conio.h>
main()
{
int a[20],i,j,k,n;
clrscr();
printf("nEnter array size : ");
scanf("%d",&n);
printf("nAccept Numbers : ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
clrscr();
printf("nOriginal array is : ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
printf("nUpdated array is : ");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;)
{
if(a[j]==a[i])
{
for(k=j;k<n;k++)
a[k]=a[k+1];
n--;
}
else
j++;
}
}

for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}

PROGRAM TO FIND FIBONACCI SERIES

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

PROGRAM TO CHECKING LEAP YEAR

Definition of leap year:

Rule 1: A year is called leap year if it is divisible by 400.


For example: 1600, 2000 etc leap year while 1500, 1700 are not leap year.
Rule 2: If year is not divisible by 400 as well as 100 but it is divisible by 4 then that year are also
leap year.
For example: 2004, 2008, 1012 are leap year.
#include<stdio.h>
int main()
{
int year;
printf("Enter any year: ");
scanf("%d",&year);
if(((year%4==0)&&(year%100!=0))||(year%400==0))
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);

return 0;
}

PROGRAM FOR ADDITION OF ALL ELEMENTS OF THE ARRAY

#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[50],sum,n;
printf("nEnter no of elements :");
scanf("%d",&n);
/* Reading values into Array */
printf("nEnter the values :");
for(i=0;i < n;i++)
scanf("%d",&a[i]);
/* computation of total */
sum=0;
for(i=0;i < n;i++)
sum=sum+a[i];
/* printing of all elements of array */
for(i=0;i < n;i++)
printf("na[%d]=%d",i,a[i]);
/* printing of total */
printf("nSum=%d",sum);
}

PROGRAM TO FIND SMALLEST ELEMENT IN ARRAY

#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],i,n,smallest;
printf("n Enter no of elements :");
scanf("%d",&n);
/* read n elements in an array */
for(i=0 ; i < n ; i++)
scanf("%d",&a[i]);
smallest = a[0];
for(i = 0 ;i < n ; i++)
{
if ( a[i] < smallest )
smallest = a[i];
}
/* Print out the Result */
printf("nSmallest Element : %d",smallest);
getch();
}

PROGRAM TO FIND PRIME FACTORS OF A NUMBER

#include<stdio.h>
int main()
{
int num,i=1,j,k;
printf("\nEnter a number:");
scanf("%d",&num);
while(i<=num){
k=0;
if(num%i==0){
j=1;
while(j<=i){
if(i%j==0)
k++;
j++;
}
if(k==2)
printf("\n%d is a prime factor",i);
}
i++;
}
return 0;
}

PROGRAM TO FIND LARGEST ELEMENT IN ARRAY

#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],i,n,largest;
printf("n Enter no of elements :");
scanf("%d",&n);
/* read n elements in an array */
for(i=0 ; i < n ; i++)
scanf("%d",&a[i]);
largest = a[0];
for(i = 0;i<n;i++)
{
if(a[i] > largest )
largest = a[i];
}
/* Print out the Result */
printf("nLargest Element : %d",largest);
getch();
}

PROGRAM TO PRINTING ASCII VALUE

#include<stdio.h>
int main()
{
int i;
for(i=0;i<=255;i++)
printf("ASCII value of character %c: %d\n",i,i);
return 0;
}

PROGRAM TO REVERSING AN ARRAY ELEMENTS


#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],i,j,n,temp;
printf("n Enter no of elements :");
scanf("%d",&n);
/* read n elements in an array */
for(i=0 ; i < n ; i++)
scanf("%d",&a[i]);
j = i-1; // j will Point to last Element
i = 0; // i will be pointing to first element
while(i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++; // increment i and decrement j
j--;
}
/* Print out the Result of Insertion */
for(i = 0 ;i< n ;i++)
printf("n %d",a[i]);
getch();
}

PROGRAM TO MERGE TWO ARRAYS


#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],b[30],c[30],i,j,k,n1,n2;
printf("n Enter no of elements in 1'st array :");
scanf("%d",&n1);
for(i=0;i 〈 n1;i++)
scanf("%d",&a[i]);
printf("n Enter no of elements in 2'nd array :");
scanf("%d",&n2);
for(i=0;i 〈 n2;i++)
scanf("%d",&b[i]);
i=0;j=0;k=0; /* merging starts */
while(i 〈 n1 && j 〈 n2)
{
if(a[i] 〈= b[j])
{
c[k]=a[i];
i++;k++;
}
else
{
c[k]=b[j];
k++;j++;
}
}
/* Some elements in array 'a' are still remaining where as the array 'b' is exhausted */
while(i 〈 n1)
{
c[k]=a[i];
i++;k++;
}
/* some elements in array b are still remaining whereas the array 'a' is exhausted */
while(j 〈 n2)
{
c[k]=b[j];
k++;j++;
}
/* Displaying elements of array 'c' */
printf("nMerged array is :");
for(i=0;i 〈 n1+n2;i++)
printf("n %d",c[i]);
getch();
}

PROGRAM TO SPLIT NUMBER INTO DIGITS

#include<stdio.h>
int main()
{
int num,temp,factor=1;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(temp){
temp=temp/10;
factor = factor*10;
}
printf("Each digits of given number are: ");
while(factor>1){
factor = factor/10;
printf("%d ",num/factor);
num = num % factor;
}
return 0;
}

PROGRAM TO SEARCH AN ELEMENT IN ARRAY


#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],x,n,i;
/*
a - for storing of data
x - element to be searched
n - no of elements in the array
i - scanning of the array
*/
printf("nEnter no of elements :");
scanf("%d",&n);
/* Reading values into Array */
printf("nEnter the values :");
for(i=0;i < n;i++)
scanf("%d",&a[i]);
/* read the element to be searched */
printf("nEnter the elements to be searched");
scanf("%d",&x);
/* search the element */
i=0; /* search starts from the zeroth location */
while(i < n && x!=a[i])
i++;
/* search until the element is not found i.e. x!=a[i]
search until the element could still be found i.e. i 〈 n */
if(i < n) /* Element is found */
printf("found at the location =%d",i+1);
else
printf("n not found");
getch();
}

PROGRAM TO CHECK ODD OR EVEN NUMBER

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

PROGRAM TO COPY ALL ELEMENTS OF AN ARRAY INTO ANOTHER ARRAY


#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],b[30],i,n;
// Element of an array 'a' will be copied into array 'b'
printf("n Enter no of elements :");
scanf("%d",&n);
/* Accepting values into Array a*/
printf("n Enter the values :");
for(i = 0 ; i < n ; i++)
scanf("%d",&a[i]);
/* Copying data from array 'a' to array 'b */
for(i = 0 ;i < n ; i++)
b[i]=a[i];
/* printing of all elements of array */
printf("the copied array is :");
for(i=0;i < n;i++)
printf("nb[%d]=%d",i,b[i]);
getch();
}

PROGRAM TO FIND OUT NCR FACTOR OF GIVEN NUMBER

#include<stdio.h>
int main()
{
int n,r,ncr;
printf("Enter any two numbers->");
scanf("%d %d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("The NCR factor of %d and %d is %d",n,r,ncr);
return 0;
}
int fact(int n)
{
int i=1;
while(n!=0)
{
i=i*n;
n--;
}
return i;
}

PROGRAM TO DELETE AN ELEMENT FROM THE SPECIFIED LOCATION FROM


ARRAY
#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],n,i,j;
printf("n Enter no of elements :");
scanf("%d",&n);
/* read n elements in an array */
printf("n Enter %d elements :",n);
for(i=0;i 〈 n;i++)
scanf("%d",&a[i]);
/* read the location of the element to be deleted */
printf("n location of the element to be deleted :");
scanf("%d",&j);
/* loop for the deletion */
while(j 〈 n)
{
a[j-1]=a[j];
j++;
}
n--; /* no of elements reduced by 1 */
/* loop for printing */
for(i=0;i 〈 n;i++)
printf("n %d",a[i]);
getch();
}

PROGRAM TO PRINT PASCAL TRIANGLE.

#include<stdio.h>
long fact(int);
int main()
{
int line,i,j;
printf("Enter the no. of lines: ");
scanf("%d",&line);
for(i=0;i<line;i++){
for(j=0;j<line-i-1;j++)
printf(" ");
for(j=0;j<=i;j++)
printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
printf("\n");
}
return 0;
}
long fact(int num)
{
long f=1;
int i=1;
while(i<=num){
f=f*i;
i++;
}
return f;
}

PROGRAM TO CHECK WHETHER MATRIX IS MAGIC SQUARE OR NOT ?

What is Magic Square :

1. A magic square is a simple mathematical game developed during the 1500.


2. Square is divided into equal number of rows and columns.
3. Start filling each square with the number from 1 to num ( where num = No of Rows X
No of Columns )
4. You can only use a number once.
5. Fill each square so that the sum of each row is the same as the sum of each column.
6. In the example shown here, the sum of each row is 15, and the sum of each column is
also 15.
7. In this Example : The numbers from 1 through 9 is used only once. This is called a magic
square.
#include<stdio.h>
#include<conio.h>
void main()
{
int size,a[4][4];
int i,j=0;
int sum,sum1,sum2;
int flag=0;
clrscr();
printf("Enter matrix : ");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
scanf("%d",&a[i][j]);
}
printf("Entered matrix is : nn");
for(i=0;i<4;i++)
{
printf("n");
for(j=0;j<4;j++)
{
printf("t%d",a[i][j]);
}
}
//------for diagondal elements---------
sum=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(i==j)
sum=sum+a[i][j];
}
//-------------for rows--------------
for(i=0;i<4;i++)
{
sum1=0;
{
for(j=0;j<4;j++)
sum1=sum1+a[i][j];
}
if(sum==sum1)
flag=1;
else
{
flag=0;
break;
}
}
//-------------for colomns----------------
for(i=0;i<4;i++)
{
sum2=0;
for(j=0;j<4;j++)
{
sum2=sum2+a[j][i];
}
if(sum==sum2)
flag=1;
else
{
flag=0;
break;
}
}
//----------------------------------------
if(flag==1)
printf("nntMagic square");
else
printf("nntNo magic square");
//-----------------------------------------
getch();
}

PROGRAM TO CHECK GIVEN STRING IS PALINDROME NUMBER OR

#include<string.h>
#include<stdio.h>
int main()
{
char *str,*rev;
int i,j;
printf("\nEnter a string:");
scanf("%s",str);
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
if(strcmp(rev,str))
printf("\nThe string is not a palindrome");
else
printf("\nThe string is a palindrome");
return 0;
}

PROGRAM TO FIND TRANSPOSE OF GIVEN SQUARE MATRIX


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],m,i,j,temp;
/* actual size of matrix is m*n
i,j - for scanning of array
temp - for interchanging of a[i][j] and a[j][i] */
printf("n Enter the size of matrix :");
scanf("%d",&m);
/* Reading elements of matrix */
printf("n Enter the values a:");
for(i=0;i<m;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
//to print original square matrix
printf("nGiven square matrix is");
for(i=0;i<m;i++)
{
printf("n");
for(j=0 ; j 〈 m ; j++)
printf("%dt",a[i][j]);
}
/* Find transpose */
for(i=1;i<m;i++)
for(j=0;j<i;j++)
{
temp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp;
}
/* printing of all elements of final matrix */
printf("nTranspose matrix is :");
for(i=0;i 〈 m;i++)
{
printf("n");
for(j=0;j<m;j++)
printf("%dt",a[i][j]);
}
getch();
}
PROGRAM TO CONVERT STRING TO INT WITHOUT USING LIBRARY
FUNCTIONS

#include<stdio.h>
int stringToInt(char[] );
int main()
{
char str[10];
int intValue;
printf("Enter any integer as a string: ");
scanf("%s",str);
intValue = stringToInt(str);
printf("Equivalent integer value: %d",intValue);
return 0;
}
int stringToInt(char str[])
{
int i=0,sum=0;
while(str[i]!='\0'){
if(str[i]< 48 || str[i] > 57){
printf("Unable to convert it into integer.\n");
return 0;
}
else{
sum = sum*10 + (str[i] - 48);
i++;
}
}
return sum;
}

PROGRAM TO CONVERT BINARY TO DECIMAL NUMBER


#include<stdio.h>
#include<conio.h>
#include<math.h>
void bin_dec(long int num) // Function Definition
{
long int rem,sum=0,power=0;
while(num>0)
{
rem = num%10;
num = num/10;
sum = sum + rem * pow(2,power);
power++;
}
printf("Decimal number : %d",sum);
}
//-------------------------------------
void main()
{
long int num;
clrscr();
printf("Enter the Binary number (0 and 1): ");
scanf("%ld",&num);
bin_dec(num);
getch();
}

PROGRAM TO FIND OUT L.C.M. OF TWO NUMBERS.

#include<stdio.h>
int main()
{
int n1,n2,x,y;
printf("\nEnter two numbers:");
scanf("%d %d",&n1,&n2);
x=n1,y=n2;
while(n1!=n2)
{
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
printf("L.C.M=%d",x*y/n1);
return 0;
}

PROGRAM TO CONVERT DECIMAL NUMBER INTO BINARY : NUMBER SYSTEM


#include<stdio.h>
#include<conio.h>
#include<math.h>
void dec_bin(long int num) // Function Definition
{
long int rem[50],i=0,length=0;
while(num>0)
{
rem[i]=num%2;
num=num/2;
i++;
length++;
}
printf("nBinary number : ");
for(i=length-1;i>=0;i--)
printf("%ld",rem[i]);
}
//================================================
void main()
{
long int num;
clrscr();
printf("Enter the decimal number : ");
scanf("%ld",&num);
dec_bin(num); // Calling function
getch();
}

PROGRAM TO COMPUTE SUM OF THE ARRAY ELEMENTS USING POINTERS


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int i,sum=0;
int *ptr;
printf("Enter 10 elements:n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
ptr = a; /* a=&a[0] */
for(i=0;i<10;i++)
{
sum = sum + *ptr; //*p=content pointed by 'ptr'
ptr++;
}
printf("The sum of array elements is %d",sum);
}

PROGRAM TO FIND G.C.D OF TWO NUMBER

Definition of HCF (Highest common factor):

HFC is also called greatest common divisor (gcd). HCF of two numbers is a largest positive
numbers which can divide both numbers without any remainder. For example HCF of two
numbers 4 and 8 is 2 since 2 is the largest positive number which can dived 4 as well as 8
without a remainder.
#include<stdio.h>
int main(){
int x,y,m,i;
printf("Insert any two number: ");
scanf("%d%d",&x,&y);
if(x>y)
m=y;
else
m=x;
for(i=m;i>=1;i--){
if(x%i==0&&y%i==0){
printf("\nHCF of two number is : %d",i) ;
break;
}
}
return 0;
}

PROGRAM TO COUNT NUMBER OF WORDS,DIGITS,VOWELS USING POINTERS


include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<conio.h>
/*low implies that position of pointer is within a word*/
#define low 1
/*high implies that position of pointer is out of word.*/
#define high 0
void main()
{
int nob,now,nod,nov,nos,pos=high;
char *s;
nob=now=nod=nov=nos=0;
clrscr();
printf("Enter any string:");
gets(s);
while(*s!='')
{
if(*s==' ') /* counting number of blank spaces. */
{
pos=high;
++nob;
}
else if(pos==high) /* counting number of words. */
{
pos=low;
++now;
}
if(isdigit(*s)) /* counting number of digits. */
++nod;
if(isalpha(*s)) /* counting number of vowels */
switch(*s)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
++nov;
break;
}
/* counting number of special characters */
if(!isdigit(*s)&&!isalpha(*s))
++nos;
s++;
}
printf("nNumber of words %d",now);
printf("nNumber of spaces %d",nob);
printf("nNumber of vowels %d",nov);
printf("nNumber of digits %d",nod);
printf("nNumber of special characters %d",nos);
getch();
}

PROGRAM TO FIND G.C.D OF TWO NUMBER

Definition of HCF (Highest common factor):

HFC is also called greatest common divisor (gcd). HCF of two numbers is a largest positive
numbers which can divide both numbers without any remainder. For example HCF of two
numbers 4 and 8 is 2 since 2 is the largest positive number which can dived 4 as well as 8
without a remainder.

Logic of HCF or GCD of any two numbers:

In HCF we try to find any largest number which can divide both the number.
For example: HCF or GCD of 20 and 30
Both number 20 and 30 are divisible by 1, 2,5,10.
HCF=max (1, 2, 3, 4, 10) =10

#include<stdio.h>
int main(){
int x,y,m,i;
printf("Insert any two number: ");
scanf("%d%d",&x,&y);
if(x>y)
m=y;
else
m=x;
for(i=m;i>=1;i--){
if(x%i==0&&y%i==0){
printf("\nHCF of two number is : %d",i) ;
break;
}
}
return 0;
}

PROGRAM TO SWAP TWO VARIABLES WITHOUT USING THIRD VARIABLE

#include<stdio.h>
int main(){
int a=5,b=10;
//process one
a=b+a;
b=a-b;
a=a-b;
printf("a= %d b= %d",a,b);
//process two
a=5;b=10;
a=a+b-(b=a);
printf("\na= %d b= %d",a,b);
//process three
a=5;b=10;
a=a^b;
b=a^b;
a=b^a;
printf("\na= %d b= %d",a,b);
//process four
a=5;b=10;
a=b-~a-1;
b=a+~b+1;
a=a+~b+1;
printf("\na= %d b= %d",a,b);
//process five
a=5,b=10;
a=b+a,b=a-b,a=a-b;
printf("\na= %d b= %d",a,b);
return 0;
}

PROGRAM TO PRINT MIRROR OF RIGHT ANGLED TRIANGLE


#include<stdio.h>
int main()
{
char ch = '*';
int i, j, no_of_spaces = 4, space_count;

for (i=1; i<=5; i++)


{
for(space_count = no_of_spaces; space_count >= 1; space_count--)
{
printf(""); //2spaces
}

for(j=1; j<=i; j++)


{
printf("%2c",ch);
}

printf("n");
no_of_spaces--;
}
return 0;
}

PROGRAM FOR SWAPPING OF TWO ARRAYS

#include<stdio.h>
int main(){
int a[10],b[10],c[10],i;
printf("Enter First array->");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\nEnter Second array->");
for(i=0;i<10;i++)
scanf("%d",&b[i]);
printf("Arrays before swapping");
printf("\nFirst array->");
for(i=0;i<10;i++){
printf("%d",a[i]);
}
printf("\nSecond array->");
for(i=0;i<10;i++){
printf("%d",b[i]);
}
for(i=0;i<10;i++){
//write any swapping technique
c[i]=a[i];
a[i]=b[i];
b[i]=c[i];
}
printf("\nArrays after swapping");
printf("\nFirst array->");
for(i=0;i<10;i++){
printf("%d",a[i]);
}
printf("\nSecond array->");
for(i=0;i<10;i++){
printf("%d",b[i]);
}
return 0;
}

PROGRAM TO PRINT NUMBER PYRAMID PATTERN


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,j,x=30,y=10;
clrscr();
printf("Enter n (between 2 & 9) : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
gotoxy(x,y);
for(j=1;j<=i;j++)
printf("%d ",i);
x=x-1;
y++;
}
getch();
}

PROGRAM TO SWAP OF STRINGS


#include<stdio.h>
int main(){
int i=0,j=0,k=0;
char str1[20],str2[20],temp[20];
puts("Enter first string");
gets(str1);
puts("Enter second string");
gets(str2);
printf("Before swaping the strings are\n");
puts(str1);
puts(str2);
while(str1[i]!='\0'){
temp[j++]=str1[i++];
}
temp[j]='\0';
i=0,j=0;
while(str2[i]!='\0'){
str1[j++]=str2[i++];
}
str1[j]='\0';
i=0,j=0;
while(temp[i]!='\0'){
str2[j++]=temp[i++];
}
str2[j]='\0';
printf("After swaping the strings are\n");
puts(str1);
puts(str2);
return 0;
}

PROGRAM TO PRINT EVEN NUMBER PYRAMID


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,num=2;
clrscr();
for(i=0;i<4;i++)
{
num=2;
for(j=0;j<=i;j++)
{
printf("%dt",num);
num = num+2;
}
printf("n");
}
getch();
}

PROGRAM TO CONVERT DIGITS TO WORDS

#include<stdio.h>
int main(){
int number,i=0,j,digit;
char * word[1000];
printf("Enter any integer: ");
scanf("%d",&number);
while(number){
digit = number %10;
number = number /10;
switch(digit){
case 0: word[i++] = "zero"; break;
case 1: word[i++] = "one"; break;
case 2: word[i++] = "two"; break;
case 3: word[i++] = "three"; break;
case 4: word[i++] = "four"; break;
case 5: word[i++] = "five"; break;
case 6: word[i++] = "six"; break;
case 7: word[i++] = "seven"; break;
case 8: word[i++] = "eight"; break;
case 9: word[i++] = "nine"; break;
}
}
for(j=i-1;j>=0;j--){
printf("%s ",word[j]);
}
return 0;
}

PROGRAM TO CONVERT DECIMAL TO ROMAN NUMBER

#include<stdio.h>
void predigits(char c1,char c2);
void postdigits(char c,int n);
char roman_Number[1000];
int i=0;
int main()
{
int j;
long int number;
printf("Enter any natural number: ");
scanf("%d",&number);
if(number <= 0){
printf("Invalid number");
return 0;
}
while(number != 0){
if(number >= 1000){
postdigits('M',number/1000);
number = number - (number/1000) * 1000;
}
else if(number >=500){
if(number < (500 + 4 * 100)){
postdigits('D',number/500);
number = number - (number/500) * 500;
}
else{
predigits('C','M');
number = number - (1000-100);
}
}
else if(number >=100){
if(number < (100 + 3 * 100)){
postdigits('C',number/100);
number = number - (number/100) * 100;
}
else{
predigits('L','D');
number = number - (500-100);
}
}
else if(number >=50){
if(number < (50 + 4 * 10)){
postdigits('L',number/50);
number = number - (number/50) * 50;
}
else{
predigits('X','C');
number = number - (100-10);
}
}
else if(number >=10){
if(number < (10 + 3 * 10)){
postdigits('X',number/10);
number = number - (number/10) * 10;
}
else{
predigits('X','L');
number = number - (50-10);
}
}
else if(number >=5){
if(number < (5 + 4 * 1)){
postdigits('V',number/5);
number = number - (number/5) * 5;
}
else{
predigits('I','X');
number = number - (10-1);
}
}
else if(number >=1){
if(number < 4){
postdigits('I',number/1);
number = number - (number/1) * 1;
}
else{
predigits('I','V');
number = number - (5-1);
}
}
}
printf("Roman number will be: ");
for(j=0;j<i;j++)
printf("%c",roman_Number[j]);
return 0;
}
void predigits(char c1,char c2){
roman_Number[i++] = c1;
roman_Number[i++] = c2;
}
void postdigits(char c,int n)
{
int j;
for(j=0;j<n;j++)
roman_Number[i++] = c;
}

PROGRAM TO PRINT PRIME NUMBER PYRAMID


#include<stdio.h>
#include<conio.h>
int prime(int num);
void main()
{
int i,j;
int num=2;
clrscr();
for(i=0;i<5;i++)
{
printf("n");
for(j=0;j<=i;j++)
{
while(!prime(num))
num++;
printf("%dt",num++);
}
}
getch();
}
//--------------------------------------------
int prime(int num)
{
int i,flag;
for(i=2;i<num;i++)
{
if(num%i!=0)
flag=1;
else
{
flag=0;
break;
}
}
if(flag == 1 || num == 2)
return(1);
else
return(0);
}

PROGRAM TO CONVERT ROMAN NUMBER TO DECIMAL NUMBER

#include<stdio.h>
#include<string.h>
int digitValue(char);
int main()
{
char roman_Number[1000];
int i=0;
long int number =0;
printf("Enter any roman number (Valid digits are I, V, X, L, C, D, M): \n");
scanf("%s",roman_Number);
while(roman_Number[i])
{
if(digitValue(roman_Number[i]) < 0){
printf("Invalid roman digit : %c",roman_Number[i]);
return 0;
}
if((strlen(roman_Number) -i) > 2)
{
if(digitValue(roman_Number[i]) < digitValue(roman_Number[i+2])){
printf("Invalid roman number");
return 0;
}
}

if(digitValue(roman_Number[i]) >= digitValue(roman_Number[i+1]))


number = number + digitValue(roman_Number[i]);
else{
number = number + (digitValue(roman_Number[i+1]) - digitValue(roman_Number[i]));
i++;
}
i++;
}
printf("Its decimal value is : %ld",number);
return 0;
}
int digitValue(char c)
{
int value=0;
switch(c)
{
case 'I': value = 1; break;
case 'V': value = 5; break;
case 'X': value = 10; break;
case 'L': value = 50; break;
case 'C': value = 100; break;
case 'D': value = 500; break;
case 'M': value = 1000; break;
case '\0': value = 0; break;
default: value = -1;
}
return value;
}
PROGRAM TO PRINT THE DOUBLE PYRAMID PATTERN
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
int blank=0;
int lines = 6;
char symbol='A';
int temp ;
int diff[7]= {0,1,3,5,7,9,11};
clrscr();
k=0;
//Step 0
for(i=lines;i>=0;i--)
{
printf("n");
symbol = 'A';
for(j=i;j>=0;j--) //step 1
printf("%c ",symbol++);
blank = diff[k++]; //step 2
for(j=0;j<blank;j++)
printf(" "); //step 3
symbol = 'F' - (blank/2);
if (blank== 0)
temp = i-1;
else
temp = i;
for(j=0;j<=temp;j++) //step 4
printf("%c ",symbol--);
}
getch();
}

PROGRAM TO CONVERT CURRENCY INTO WORDS

#include<stdio.h>
#include<string.h>
void toWord(int,int);
char * getPositionValue(int);
char * digitToWord(int);
char word[100][30];
int i =0;
int main()
{
int j,k,subnumer;
unsigned long int number;
printf("Enter any postive number: ");
scanf("%lu",&number);
if(number ==0)
{
printf("Zero");
return 0;
}
while(number)
{
if(i==1)
{
toWord(number %10,i);
number = number/10;
}
else
{
toWord(number %100,i);
number = number/100;
}
i++;
}
printf("Number in word: ");
*word[i-1] = *word[i-1] - 32;
for(j=i-1;j>=0;j--)
{
printf("%s",word[j]);
}
return 0;
}
void toWord(int number,int position)
{
char numberToword[100]={" "};
if(number ==0)
{
}
else if (number < 20 ||number %10==0){
strcpy(numberToword,digitToWord(number));
}
else
{
strcpy(numberToword,digitToWord((number/10)*10));
strcat(numberToword,digitToWord(number%10));
}
strcat(numberToword,getPositionValue(position));
strcpy(word[i],numberToword);
}
char * getPositionValue(int postion){
static char positionValue[10]=" ";
switch(postion)
{
case 1: strcpy(positionValue,"hundreds "); break;
case 2: strcpy(positionValue,"thousand "); break;
case 3: strcpy(positionValue,"lakh "); break;
case 4: strcpy(positionValue,"crore "); break;
case 5: strcpy(positionValue,"arab "); break;
case 6: strcpy(positionValue,"kharab "); break;
case 7: strcpy(positionValue,"neel "); break;
case 8: strcpy(positionValue,"padam "); break;
}
return positionValue;
}
char * digitToWord(int digit){
static char digitInWord[10]=" ";
switch(digit)
{
case 1: strcpy(digitInWord , "one "); break;
case 2: strcpy(digitInWord , "two "); break;
case 3: strcpy(digitInWord , "three "); break;
case 4: strcpy(digitInWord , "four "); break;
case 5: strcpy(digitInWord , "five "); break;
case 6: strcpy(digitInWord , "six "); break;
case 7: strcpy(digitInWord , "seven "); break;
case 8: strcpy(digitInWord , "eight "); break;
case 9: strcpy(digitInWord , "nine ");break;
case 10: strcpy(digitInWord , "ten "); break;
case 11: strcpy(digitInWord , "eleven "); break;
case 12: strcpy(digitInWord , "twelve "); break;
case 13: strcpy(digitInWord , "thirteen "); break;
case 14: strcpy(digitInWord , "fourteen "); break;
case 15: strcpy(digitInWord , "fifteen "); break;
case 16: strcpy(digitInWord , "sixteen "); break;
case 17: strcpy(digitInWord , "seventeen "); break;
case 18: strcpy(digitInWord , "eighteen "); break;
case 19: strcpy(digitInWord , "nineteen "); break;
case 20: strcpy(digitInWord , "twenty "); break;
case 30: strcpy(digitInWord , "thirty "); break;
case 40: strcpy(digitInWord , "fourty "); break;
case 50: strcpy(digitInWord , "fifty "); break;
case 60: strcpy(digitInWord , "sixty "); break;
case 70: strcpy(digitInWord , "seventy "); break;
case 80: strcpy(digitInWord , "eighty "); break;
case 90: strcpy(digitInWord,"ninety "); break;
}
return digitInWord;
}

PROGRAM FOR UNIT CONVERSION

#include<stdio.h>
#include<math.h>
int main()
{
char fromUnit,toUnit;
char *fUnit,*tUnit;
long double fromValue,meterValue,toValue;
int power =0;
printf("To convert the 12 Inch to Foot\n");
printf("enter the the unit in the format : dc12\n");
printf("Ell: a\n");
printf("Femi: b\n");
printf("Foot: c\n");
printf("Inch: d\n");
printf("Light year: e\n");
printf("Metre: f\n");
printf("Mile: g\n");
printf("Nano meter: h\n");
printf("Pace: i\n");
printf("Point: j\n");
printf("Yard: k\n");
printf("Mili meter: l\n");
printf("Centi meter: m\n");
printf("Deci meter: n\n");
printf("Deca meter: o\n");
printf("Hecto meter: p\n");
printf("Kilo meter: q\n");
scanf("%c%c%Lf",&fromUnit,&toUnit,&fromValue);
switch(fromUnit)
{
case 'a': meterValue = fromValue * 1.143; fUnit="ell"; break;
case 'b': meterValue = fromValue ; power = -15; fUnit="fm"; break;
case 'c': meterValue = fromValue * 0.3048; fUnit="ft"; break;
case 'd': meterValue = fromValue * 0.0254; fUnit="in"; break;
case 'e': meterValue = fromValue * 9.4607304725808; power =15; fUnit="ly"; break;
case 'f': meterValue = fromValue; fUnit="m"; break;
case 'g': meterValue = fromValue * 1609.344; fUnit="mi"; break;
case 'h': meterValue = fromValue; fUnit="nm"; power = -9; break;
case 'i': meterValue = fromValue * 0.762 ; fUnit="pace"; break;
case 'j': meterValue = fromValue * 0.000351450; fUnit="pt"; break;
case 'k': meterValue = fromValue * 0.9144; fUnit="yd"; break;
case 'l': meterValue = fromValue * 0.001; fUnit="mm"; break;
case 'm': meterValue = fromValue * 0.01; fUnit="cm"; break;
case 'n': meterValue = fromValue * 0.1; fUnit="deci meter"; break;
case 'o': meterValue = fromValue * 10; fUnit="deca meter"; break;
case 'p': meterValue = fromValue * 100; fUnit="hm"; break;
case 'q': meterValue = fromValue * 1000; fUnit="km"; break;
default:
printf("Invalid input"); exit(0);
}
switch(toUnit)
{
case 'a': toValue = meterValue/1.143; tUnit="ell"; break;
case 'b': toValue = meterValue; tUnit="fm"; break;
case 'c': toValue = meterValue/0.3048; tUnit="ft"; break;
case 'd': toValue = meterValue/0.0254; tUnit="in"; break;
case 'e': toValue = meterValue/9.4607304725808; tUnit="ly"; break;
case 'f': toValue = meterValue; tUnit="m";break;
case 'g': toValue = meterValue/1609.344; tUnit="mi"; break;
case 'h': toValue = meterValue; tUnit="nm"; break;
case 'i': toValue = meterValue/0.762; tUnit="pace"; break;
case 'j': toValue = meterValue/0.000351450; tUnit="pt"; break;
case 'k': toValue = meterValue/0.9144; tUnit="yd"; break;
case 'l': toValue = meterValue/0.001; tUnit="mm"; break;
case 'm': toValue = meterValue/0.01; tUnit="cm"; break;
case 'n': toValue = meterValue/0.1; tUnit="deci meter"; break;
case 'o': toValue = meterValue/10; tUnit="deca meter"; break;
case 'p': toValue = meterValue/100; tUnit="hm"; break;
case 'q': toValue = meterValue/1000; tUnit="km"; break;
default:
printf("Invalid input"); exit(0);
}
if(power==0)
printf("%.4Lf %s = %.4Lf %s",fromValue,fUnit,toValue,tUnit);
else{
while(tovalue > 10
printf("%.4Lf %s = %.4Lf*10^%d %s",fromValue,fUnit,toValue,power,tUnit);
}
return 0;
}

PROGRAM TO PRINT 1-10 NUMBERS IN PYRAMID FASHION


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int count=1;
for(i=0;i<=4;i++)
{
printf("n");
for(j=0;j<i;j++)
{
printf("%dt",count);
count++;
}
}
getch();
}

PROGRAM FOR ATM TRANSACTIONS

#include<stdio.h>
int totalThousand =1000;
int totalFiveFundred =1000;
int totalOneHundred =1000;

int main()
{
unsigned long withdrawAmount;
unsigned long totalMoney;
int thousand=0,fiveHundred=0,oneHundred=0;
printf("Enter the amount in multiple of 100: ");
scanf("%lu",&withdrawAmount);
if(withdrawAmount %100 != 0){
printf("Invalid amount;");
return 0;
}
totalMoney = totalThousand * 1000 + totalFiveFundred* 500 + totalOneHundred*100;
if(withdrawAmount > totalMoney){
printf("Sorry,Insufficient money");
return 0;
}
thousand = withdrawAmount / 1000;
if(thousand > totalThousand)
thousand = totalThousand;
withdrawAmount = withdrawAmount - thousand * 1000;
if (withdrawAmount > 0){
fiveHundred = withdrawAmount / 500;
if(fiveHundred > totalFiveFundred)
fiveHundred = totalFiveFundred;
withdrawAmount = withdrawAmount - fiveHundred * 500;
}
if (withdrawAmount > 0)
oneHundred = withdrawAmount / 100;
printf("Total 1000 note: %d\n",thousand);
printf("Total 500 note: %d\n",fiveHundred);
printf("Total 100 note: %d\n",oneHundred);
return 0;
}

PROGRAM WITHOUT MAIN FUNCTION

Can you write a c program without using main function?

We can write a c program without using main function. We can compile it but we cannot execute
a program without a main function. Since in C execution of any program start from main
function. Examples of c program without a main is all the c library functions. Function printf is
an example of library function which has been written and complied without using main
function. To use or run such functions we write other program using main function and call those
functions.

PROGRAM TO PRINT INVERTED PYRAMID


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
char ch = '*';
clrscr();
for(i=4;i>=0;i--)
{
printf("n");
for(j=0;j<i;j++)
printf("%c",ch);
}
getch();
}

PROGRAM TO CONVERSION FROM UPPERCASE TO LOWER CASE

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

PROGRAM TO PRINT BINARY NUMBERS PYRAMID PATTERN


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int count = 1;
clrscr();
for(i=1;i<=4;i++)
{
printf("n");
for(j=1;j<=i;j++)
{
printf("%d",count%2);
count++;
}
if( i % 2 == 0)
count=1;
else
count=0;
}
getch();
}

PROGRAM TO CONVERT THE STRING FROM LOWER CASE TO UPPER CASE

#include<stdio.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]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\nThe string in lowercase is->%s",str);
return 0;
}

PROGRAM TO PRINT RIGHT ANGLED PYRAMID


#include<stdio.h>
#include<conio.h>
main()
{
int i,j,lines;
char ch = '*';
clrscr();
printf("Enter number of lines : ");
scanf("%d",&lines);
for(i=0;i <=lines;i++)
{
printf("n");
for (j=0;j < i;j++)
printf("%c",ch);
}
getch();
}

PROGRAM TO COUNT DIFFERENT CHARACTERS IN A STRING

#include <stdio.h>
int isvowel(char chk);
int main(){
char text[1000], chk;
int count;
count = 0;
while((text[count] = getchar()) != '\n')
count++;
text[count] = '\0';
count = 0;
while ((chk = text[count]) != '\0'){
if (isvowel(chk)){
if((chk = text[++count]) && isvowel(chk)){
putchar(text[count -1]);
putchar(text[count]);
putchar('\n');
}
}
else
++count;
}
return 0;
}
int isvowel(char chk){
if(chk == 'a' || chk == 'e' || chk == 'i' || chk == 'o' || chk == 'u')
return 1;
return 0;
}

PROGRAM TO PRINT TOWER OF HANOI USING RECURSION


#include<stdio.h>
#include<conio.h>
void TOH(int n,char x,char y,char z);
void main()
{
int n;
printf("nEnter number of plates:");
scanf("%d",&n);
TOH(n-1,'A','B','C');
getch();
}
void TOH(int n,char x,char y,char z)
{
if(n>0)
{
TOH(n-1,x,z,y);
printf("n%c -> %c",x,y);
TOH(n-1,z,y,x);
}
}

PROGRAM TO SORT THE STRING

#include<stdio.h>
int main(){
int i,j,n;
char str[20][20],temp[20];
puts("Enter the no. of string to be sorted");
scanf("%d",&n);
for(i=0;i<=n;i++)
gets(str[i]);
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++){
if(strcmp(str[i],str[j])>0){
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("The sorted string\n");
for(i=0;i<=n;i++)
puts(str[i]);
return 0;
}

WRITE A PROGRAM IN C TO PRINT THE MULTIPLICATION OF TWO MATRICES

#include<stdio.h>
#include<conio.h>

main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
clrscr();
printf("\nEnter the row and column of first matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the row and column of second matrix");
scanf("%d %d",&o,&p);
if(n!=o)
{
printf("Matrix mutiplication is not possible");
printf("\nColumn of first matrix must be same as row of second matrix");
}
else
{
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the Second matrix->");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);
printf("\nThe First matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("\nThe Second matrix is\n");
for(i=0;i<o;i++)
{
printf("\n");
for(j=0;j<p;j++)
{
printf("%d\t",b[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf("\nThe multiplication of two matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<p;j++)
{
printf("%d\t",c[i][j]);
}
}
getch();
}

PROGRAM TO RETURN MULTIPLE VALUES FROM A FUNCTION

Method 1 : Using Array

 If more than two variables of same type is to be returned then we can use array .
 Store each and every value to be returned in an array and return base address of that
array.

Method 2 : Using Pointer and One Return Statement

 Pointer Variable can updated directly using Value at ['*'] Operator.


 Usually Function can return single value.
 If we need to return more than two variables then update 1 variable directly using pointer
and return second variable using ‘return Statement‘. [ Call by Value + Call by
Reference ]

Method 3 : Using Structure

 Construct a Structure containing values to be returned and then return the base address of
the structure to the calling function.

You might also like