Programs in C
Programs in C
2
Program #21: Use of Camparison Operators
Program #22: Comparison of two numbers using ternary operator
Program #23: Find the largest number of four numbers
Program #24: Conversion of days into years, months, weeks, days
Program #25: Calculation of roots of quadratic equation
Program #26: calculation of expressions
Program #27: Checking whether number is even or odd
Program #28: Swapping without use of third variable
Program #29: Get the largest number of five numbers
Program #30: Write a program in C, which can print every odd number and
their sum up to N.
Program #31: Write a program in C, which can calculate the sum of the
following series: a) 1 + 2 + 3 + ... + N, N = 10.
b) X + 2X + 3X + ... +NX, N = 15.
c) 1 + X + X^2 + ... + X^N , N = 5.
d) 1 + 1 / X + 1/ X^2 + ...+ 1 / X^N, N = 10.
e) 1 + 1 / X + 1/ X^2 + ... for 10 terms.
f) 1 + 1 / X + 1/ X^2 + ... which is correct up to two decimal
places.
g) 1 + 1 / X + 1/ X^2 + ... which is correct up to three decimal
places.
Program #32: Write a program in C, which can count all the numbers
using do-while statement and then print that value.
Program #33: Find out the prime number less then a given number
Program #34: To find the divisor of a number
Program #35: Write a menu-based program in C, which can take two
integer numbers Num1 and Num2 from the user and then do all the
arithmetic operations Between them.
Program #36: Write down the Header Files of each of the following
functions: - getch(), printf(), clrscr(), sqrt(), fflush()
Program #37: What will be the output of each of the following
executable statements a)
printf(" \\\\\\\\ ");
b)
printf("""""""""""");
c)
i = 5; printf(" %%d%%",i++);
d)
printf("%d%d", 5>=5, i >= I);
e)
I = 5; j = I++*++I; printf(" I = %d and j = %d ",
I, j);
Program #38: Write a program in C, which can print N Real Numbers and
return their sum to main()..
Program #39: Write a program in C to find sum and average of square of
4
Program #63: Merging and Sorting of Array
Program #64: From a given matrix print all the diagonal elements on the
screen
Program #65: From a given matrix print all the upper diagonal elements
on the screen
Program #66: Arithmetic operations with matrices
Program #67: Demonstration of pointers
Program #68: Write a program to assign the value of one pointer
variable *ptr to another pointer *ptr1 and display the contents of both
of these pointer variables.
Program #69: A program is having three variables int I, char c, and
float f. use void pointer to display the content of all these variables.
Program #70: Write a program to display the memory address of a
variable using pointer before incrementation and after incrementation.
Program #71: A program to display the contents of the pointer variables
using arithmetic operations.
Program
# 1
Program to print Hello
/*Header file section*/
#include<stdio.h>
#include<conio.h>
/*Starting of the function main*/
main()
{
/*Function To Clear Screen*/
clrscr();
/*Application Part*/
printf("\n\n Hello);
getch();
}
/*Ending of the program*/
Program
# 2
Program to use Escape Sequences like \n, \t, \a, \b, \v
/*Header File Section*/
#include<stdio.h>
#include<conio.h>
/*Starting of the function main()*/
main()
{
/*Function To Clear Screen*/
clrscr();
/*Application Part*/
printf("\n The use of
printf("\n The use of
printf("\n The use of
printf("\n The use of
printf("\n The use of
printf("\n The use of
\\n
\\v
\\t
\\r
\\a
\\b
is
is
is
is
is
is
A
A
A
A
A
A
\n
\v
\t
\r
\a
\b
B
B
B
B
B
B
");
");
");
");
");
");
getch();
}
/*Ending of the function main()*/
use of \n is A
use of \v is A
use
use
use
use
of
of
of
of
\t
\r
\a
\b
is
is
is
is
A
A
A B
A B
Program
# 3
Program that accept a character in UPPER CASE and then convert it into
LOWER CASE character using the ASCII value
/*Header File Declaration*/
#include<stdio.h>
#include<conio.h>
/*Starting of the function main*/
main()
{
/*Local Variable Declaration*/
char ch;
/*Function to clear the Screen */
clrscr();
/*Application Part */
printf("Enter the character in UPPER CASE :\t");
scanf("%c",&ch);
printf("\nThe LOWER CASE of %c \t is \t%c",ch, ch+32);
getch();
}
/*Ending of the function main()*/
is
Program
# 4
Program that accept a character in LOWER CASE and then convert it into
UPPER CASE character using the ASCII value
/*Header File Declration*/
#include<stdio.h>
#include<conio.h>
/*Starting of the function main*/
main()
{
/*Local Variable Declaration*/
char ch;
/*Function To Clear Screen*/
clrscr();
/*Application Part*/
32);
getch();
}
/*Ending of the function main()*/
character s
is
s
S
:\t");
is \t%c",ch,ch-
Program
# 5
Program that accept a character in UPPER CASE and then convert it into
LOWER CASE character by using tolower()
/*Header File Declaration*/
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
/*Starting of the function main*/
main()
{
/*Local Variable Declaration*/
char ch;
/*Function To Clear Screen*/
clrscr();
/*Application Part*/
printf("Enter any UPPER CASE character : \t");
scanf("%c",&ch);
printf("\n\nThe LOWER CASE of character %c is\t
%c",ch,tolower(ch));
getch();
}
/*Ending of the function main()*/
W
w
10
Program
# 6
Program that accept a character in LOWER CASE and then convert into
UPPER CASE by using toupper()
/*Header File Declaration */
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
/*Starting of the function main*/
main()
{
/*Local Variable Declaration*/
char ch;
/*Function To Clear Screen*/
clrscr();
/*Application Part*/
printf("Enter Any LOWER CASE character : \t");
scanf("%c",&ch);
printf("\n\nthe UPPER CASE of
%c\t is \t%c",ch,toupper(ch));
getch();
}
/*Ending of the function main()*/
is
a
A
11
Program # 7
Program to get a number from user in integer& print the SQUARE OF NUMBER
The SQUARE of 3
is
Program # 8
Program to get a number from user in float and print the SQUARE OF
NUMBER
/*Header File Declaration */
#include<stdio.h>
#include<conio.h>
/*Starting of the function main*/
main()
{
/*Local variable Declaration*/
float num;
/*Function To Clear Screen*/
clrscr();
/*Application Part*/
printf("Enter the number
: \t");
scanf("%f",&num);
printf("\n\nThe SQUARE of %f is \t%f",num,num*num);
getch();
3.5
12.250000
12
Program # 9
Program to Perform Arithmetic Operation on Integer Quantities
13
5
-1
Program # 10
Program to Perform Arithmetic Operation on One Integer And One Float
Quantities
/*Header file section*/
#include <stdio.h>
#include<conio.h>
/*Starting of the function main*/
main()
{
/*Local Variable Declaration*/
int num1;
float num2;
/*Function To Clear Screen*/
clrscr();
/*Application Part*/
printf("Enter an Integer
scanf("%d", &num1);
printf("\nEnter a float
scanf("%f", &num2);
printf("\n num1 + num2 =
printf("\n num1 - num2 =
printf("\n num1 * num2 =
printf("\n num1 / num2 =
getch();
number
num1 : ");
}
/*Ending of the function main()*/
+
*
/
num2
num2
num2
num2
num1 : 12
14
Program # 11
Write a program in C, which can take an integer number Inch and then
convert then find out its equivalent in Meters, and in Centimeters.
/*Header file section*/
#include<stdio.h>
#include<conio.h>
/*Starting of the function main()*/
main()
{
getch();
36
15
Program # 12
Program To Print ASCII equvilant of Given Character
/*Header File Section*/
#include<stdio.h>
#include<ctype.h>
/*Starting of the function main*/
main()
{
/*Local Variable Declaration*/
char ch;
/*Function to Clear Screen*/
clrscr();
/*Application Part*/
printf("Enter the character :
");
scanf("%c",&ch);
printf("\n\n\nThe ASCII equvilant of %c
%d",ch,toascii(ch));
getch();
}
/*Ending of the function main()*/
w
is
119
is\t
16
Program # 13
Write a program in C, which can take an integer number num whose
valueis 300 and then find the value of (num * num ) / num.
Program taking integer from the user & calculating (num*num)/num
/*Header file Section*/
#include<stdio.h>
/*Starting of the function main*/
main()
{
/*Local Variable Declaration*/
int num;
/*Function To Clear Screen*/
clrscr();
/*Application Part*/
num = 300;
printf("\n %d * %d / %d
%d",num,num,num,(num*num)/num);
getch();
}
/*Ending of the function main()*/
81
17
18
Program # 14
Write a program in C, which can take an integer number greater than 127
and then print it into character form using %c program taking integer
>127 &printing equivalent character
/*Header file section*/
#include<stdio.h>
#include<ctype.h>
/*Starting of the function main*/
main()
{
/*Local Varaible Declaration*/
int num;
/*Function To Clear Screen*/
clrscr();
/*Application Part*/
printf(" Enter the number > 127 : ");
scanf("%d",&num);
printf("\n The character equvilant of %d is
getch();
}
/*Ending of the function main()*/
%c",num,num);
19
Program # 15
Write a program in C, which can take radius of a circle and then
calculate the area of the circle. Using the same radius also calculate
the volume of sphere [ take PI = 3.14].
Program taking radius & calculating area
Program # 16
Write a program in C, which can take an integer number num from the
user and Calculate sin(num), cos(num) and log(num). Program taking
integer number & calculating sin(num),cos(num),log(num)
/*Header file section*/
#include<stdio.h>
#include<math.h>
/*Starting of the function main*/
main()
{
/*Local Variable Declaration*/
float num;
/*Application Part*/
clrscr();
printf("Enter the number : ");
scanf("%f",&num);
printf("\n\nThe sin(%f) is\t%f",num,sin(num));
printf("\n\nThe cos(%f) is\t%f",num,cos(num));
printf("\n\nThe log(%f) is\t%f",num,log(num));
getch();
}
/*Ending of the function main()*/
1.23
The sin(1.230000) is
0.942489
The cos(1.230000) is
0.334238
The log(1.230000) is
0.207014
20
21
Program
# 17
Conversion of paisa into rupees
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
/* Starting of the Function main */
main()
{
/* Local Variable Declaration */
int paisa;
float rupees;
clrscr();
/* Application Part */
printf("\nEnter the paisa : ");
scanf("%d",&paisa);
rupees = (float)paisa / 100;
/* (float) converts the any type of data into float type data */
printf("\n%d paisa = %.2f rupees",paisa,rupees);
getch();
}
/* Ending of the Function main */
22
Program # 18
Sum of Series
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
/* Starting of the Function main */
main()
{
/* Local Variable Declaration */
float sum;
clrscr();
/* Application Part */
sum = 1 + 1/2 + 1/3 + 1/4 + 1/5 +1/6 + 1/7;
printf("\n1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 = %f",sum);
printf("\n\n....Using Integer Calculation");
getch();
}
/* Ending of the Function main */
Program
# 19
Calculation of square , cube & fourth power
23
/* Application Part */
printf("\n\nEnter any number : ");
scanf("%f",&num);
result = num * num;
printf("\nSquare
: %f",result);
result=result * num;
printf("\n\nCube
: %f",result);
result=result * num;
printf("\n\nForth power
: %f",result);
getch();
the screen */
}
/* End of function main */
: 4.000000
Cube
: -8.000000
Forth power
: 16.000000
24
Program
# 20
Type casting demo
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
/* Starting of the Function main */
main()
{
/* Local Variable Declaration */
int i;
float f;
char c;
double d;
clrscr();
/* Application Part */
/* Assigning the value to variables */
i = 15;
f = 13.0;
d = 11.0;
c = 'a';
/* printing the values of varibales before interchange */
printf("\nThe value of integer is : %d",i);
printf("\n\nThe value of character is : %c",c);
printf("\n\nThe value of float is : %f",f);
printf("\n\nThe value of double is : %lf",d);
/* Interchanging the values of varibales */
c = i;
i = f;
f = d;
value
value
value
value
of
of
of
of
integer is : %d",i);
character is : %c",c);
float is : %f",f);
double is : %lf",d);
}
/* Ending of the Function main */
/*Output of the program*/
The value of integer is : 15
The value of character is : a
The value of float is : 13.000000
The value of double is : 11.000000
The value of integer is : 13
25
The value of character is :
The value of float is : 11.000000
The value of double is : 11.000000
26
Program
# 21
Use of Camparison Operators
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
/* Starting of the Function main */
main()
{
/* Local Variable Declaration */
int num1,num2,num3;
clrscr();
/* Application Part */
printf("\nEnter the value of num1 : ");
scanf("%d",&num1);
printf("\nEnter the value of num2 : ");
scanf("%d",&num2);
printf("\nEnter the value of num3 : ");
scanf("%d",&num3);
printf("\n\nnum1
< num3);
printf("\n\nnum1
> num3);
printf("\n\nnum1
num1 > num3);
printf("\n\nnum1
num2 < 0 || num1 >0);
getch();
< num2 && num1 < num3 = %d",num1 < num2 && num1
< num2 || num1 > num3 = %d",num1 < num2 || num1
== num2 || num1 > num3 = %d",num1 == num2 ||
> 15 || num2 < 0 || num1 > 0 = %d",num1 > 15 ||
}
/* Ending of the Function main */
/*Output of the program*/
Enter the value of num1 : 23
Enter the value of num2 : 34
Enter the value of num3 : 23
num1 < num2 && num1 < num3 = 0
num1 < num2 || num1 > num3 = 1
num1 == num2 || num1 > num3 = 0
num1 > 15 || num2 < 0 || num1 > 0 = 1
Program
# 22
Comparison of two numbers using ternary operator
27
/* Application Part */
printf("\nEnter the value of num1 : ");
scanf("%d",&num1);
printf("\nEnter the value of num2 : ");
scanf("%d",&num2);
great = num1 > num2 ? num1 : num2;
printf("\n\nThe greater of %d and %d is : %d",num1,num2,great);
getch();
screen */
}
/* Ending of the Function main */
28
Program
# 23
Find the largest number of four numbers
/* Application Part */
printf("\n\nEnter the first number : ");
scanf("%d",&num1);
printf("\nEnter the second number : ");
scanf("%d",&num2);
printf("\nEnter the third number : ");
scanf("%d",&num3);
printf("\nEnter the fourth number : ");
scanf("%d",&num4);
/* Finding the greatest of four numbers */
result = num1 > num2 ? (num1 > num3 ? (num1 > num4 ? num1 : num4):
(num3 > num4 ? num3 : num4)):
(num2 > num4 ? (num2 > num3 ? num2 : num3) : (num4 >
num3 ?
num4 : num3));
printf("\n\nGreatest of four numbers is %d", result);
getch();
the screen */
}
/* Ending of the Function main */
/*Output of the program*/
Enter the first number : 1
Enter the second number : -34
Enter the third number : 0
Enter the fourth number : 324
Greatest of four numbers is 324
Program
# 24
Conversion of days into years, months, weeks, days
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
/* Starting of the Function main */
main()
{
/* Local Variable Declaration */
int days;
clrscr();
/* Application Part */
printf("\nEnter the days : ");
scanf("%d",&days);
printf("\n\n");
if(days >= 365)
{
printf("%d year(s) ",days/365);
days = days%365;
}
if(days >= 30)
{
printf("%d month(s) ",days/30);
days = days%30;
}
if(days >= 7)
{
printf("%d week(s) ",days/7);
days = days%7;
}
if(days >= 1)
{
printf("%d day(s)",days);
}
getch();
/* Function to accept the input
used here to hold the screen */
}
/* Ending of the Function main */
/*Output of the program*/
Enter the days : 1234
3 year(s) 4 month(s) 2 week(s) 5 day(s)
29
Program
30
# 25
/* Application Part */
printf("\nEnter the value of a : ");
scanf("%d",&a);
printf("\nEnter the value of b : ");
scanf("%d",&b);
printf("\nEnter the value of c : ");
scanf("%d",&c);
root1 = (-b+sqrt(b*b-4.0*a*c))/(2.0*a);
root2 = (-b-sqrt(b*b-4.0*a*c))/(2.0*a);
printf("The roots of the equation are : %f and %f",root1,root2);
getch();
}
/* Ending of the Function main */
/*Output of the program*/
Enter the value of a : 12
Enter the value of b : -65
Enter the value of c : 9
The roots of the equation are : 5.274472 and 0.142194
*/
31
Program
# 26
calculation of expressions
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
/* Starting of the Function main */
main()
{
/* Local Variable Declaration */
int a,k,d,adc=15,b=3;
float g;
clrscr();
/* Application Part */
/* Expression Evaluation */
a
k
d
g
=
=
=
=
5
3
2
b
*
/
*
/
6
2
3
2
/
*
/
+
3
4
4
b
+
+
+
*
2
3
4
4
*
/
/
/
6
8
4
3
/
+
+
-
4 - 2;
3;
8 - 2 + 5 / 8;
b + adc / 3;
value
value
value
value
of
of
of
of
a
k
d
b
is
is
is
is
:
:
:
:
%d",a);
%d",k);
%d",d);
%d",b);
}
/* Ending of the Function main */
/* Output of the program*/
The value of a is : 11
The value of k is : 7
The value of d is : 8
The value of b is : 3
Program
32
# 27
/* Application Part */
printf("\nEnter the value of num : ");
scanf("%d",&num);
if(num%2==0)
printf("\n%d is even",num);
else
printf("\n%d is odd",num);
getch();
}
/* Ending of the Function main */
Output of the program
Enter the value of num : 23
23 is odd
Enter the value of num : 34
34 is even
Program
33
# 28
/* Application Part */
printf("\nEnter the value of num1 : ");
scanf("%d",&num1);
printf("\nEnter the value of num2 : ");
scanf("%d",&num2);
printf("\n\nBEFORE SWAPPING");
printf("\n\nNum1 = %d , Num2 = %d",num1,num2);
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
printf("\n\nAFTER SWAPPING");
printf("\n\nNum1 = %d , Num2 = %d",num1,num2);
getch();
}
/* Ending of the Function main */
/*Output of the program*/
Enter the value of num1 : 12
Enter the value of num2 : 33
BEFORE SWAPPING
Num1 = 12 , Num2 = 33
AFTER SWAPPING
Num1 = 33 , Num2 = 12
*/
Program
34
# 29
/* Application Part */
printf("\n\nEnter first number : ");
scanf("%d",&a);
printf("\nEnter second number : ");
scanf("%d",&b);
printf("\nEnter third number : ");
scanf("%d",&c);
printf("\nEnter fourth number : ");
scanf("%d",&d);
printf("\nEnter fifth number : ");
scanf("%d",&e);
/* Starting comparison of five numbers using if-else */
if(a>b)
{
if(a>c)
{
if(a>d)
{
if(a>e)
printf("\n%d is greatest number",a);
else
printf("\n%d is greatest number",e);
}
else
{
if(d>e)
printf("\n%d is greatest number",d);
else
printf("\n%d is greatest number",e);
}
}
else
{
if(c>d)
{
if(c>e)
printf("\n%d is greatest number",c);
else
printf("\n%d is greatest number",e);
}
}
}
else
{
else
{
if(d>e)
printf("\n%d is greatest number",d);
else
printf("\n%d is greatest number",e);
}
if(b>c)
{
if(b>d)
{
if(b>e)
printf("\n%d is greatest number",b);
else
printf("\n%d is greatest number",e);
}
else
{
if(d>e)
printf("\n%d is greatest number",d);
else
printf("\n%d is greatest number",e);
}
}
else
{
if(c>d)
{
if(c>e)
printf("%d is greatest number",c);
else
printf("%d is greatest number",e);
}
else
{
if(d>e)
printf("%d is greatest number",d);
else
printf("%d is greatest number",e);
}
}
}
getch();
}
/* Ending of the Function main */
Output of the program
Enter first number : 23
Enter second number : 34
35
36
37
Program # 30
Write a program in C, which can print every odd number and their sum up
to N.
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
/* Starting of the Function main */
main()
{
int i=1, sum=0, N;
clrscr();
printf("Enter the value of N \t:\t");
scanf("%d",&N);
printf("\n\n\nThe odd numbers are ");
for(i = 1; i <= N; i += 2)
{
printf(" %d,",i);
sum += i;
}
printf("\n\n\nThe sum of the odd numbers is %d",sum);
getch();
}
10
1, 3, 5, 7, 9,
38
Program # 31
Write a program in C, which can calculate the sum of the following
series: a) 1 + 2 + 3 + ... + N, N = 10.
b) X + 2X + 3X + ... +NX, N = 15.
c) 1 + X + X^2 + ... + X^N , N = 5.
d) 1 + 1 / X + 1/ X^2 + ...+ 1 / X^N, N = 10.
e) 1 + 1 / X + 1/ X^2 + ... for 10 terms.
f) 1 + 1 / X + 1/ X^2 + ... which is correct up to two decimal
places.
g) 1
+ 1 / X + 1/ X^2 + ... which is correct up to three
decimal places.
#include <stdio.h>
#include <math.h>
#include <conio.h>
main()
{
int i = 1, sum = 0, x;
float sum1 = 1.0, term, X;
clrscr();
printf("The sum of the series 1 + 2 + 3 + ... + N
for(i = 1; i<= 10; i++)
sum+=i;
printf(":\t%d",sum);
printf("\n\nEnter the x\t:\t");
scanf("%d",&x);
printf("\nThe sum of the series
for(i = 1; i <= 15; i++)
sum += i * x;
printf(":\t%d",sum);
sum = 1;
printf("\n\nEnter the x\t:\t");
scanf("%d",&x);
printf("\nThe sum of the series
is");
for(i = 0; i <= 5; i++)
sum += sum * pow(x,i);
printf(":\t%d",sum);
1x+2x+3x+...+Nx
sum1 = 1.0;
printf("\n\nEnter the x\t:\t");
scanf("%d",&x);
printf("\nThe sum of the series 1+1/X+1/X^2+...+1/X^N
is");
for(i = 0; i <= 10; i++)
sum1 += 1.0 / pow(x,i);
printf(":\t%f",sum1);
where N = 5
for N = 10
sum1 = 1.0;
printf("\nEnter the value of x \t:\t");
scanf("%f",&X);
printf("\nThe sum of the series 1+1/X+1/X^2 +...\nwhich is correct");
printf(" up to 2 decimal places is");
do
39
term = 1 / X;
sum1 += term;
}while(term < .01);
printf(" %.2f",sum1);
sum1 = 1.0;
printf("\nEnter the value of x \t:\t");
scanf("%f",&X);
printf("\nThe sum of the series 1+1/X+1/X^2 +...\nwhich is correct");
printf(" up to 3 decimal places is");
do
{
term=1 / X;
sum+=term;
}while(term < .001);
printf(" %.3f ",sum1);
getch();
}
1x+2x+3x+...+Nx
Enter the x
Enter the x
55
for N = 10 is:
2.999023
Program # 32
Write a program in C, which can count all the numbers using do-while
statement and then print that value.
40
Program # 33
Find out the prime number less then a given number
#include<stdio.h>
#include<math.h>
main()
{
int n,prime,i,j,x;
clrscr();
printf("ENTER AN INTEGER :");
scanf("%d",&n);
printf("\nPRIME NO'S.ARE");
for(j=2;j<=n;j++)
{
i=2;
prime=1;
x=sqrt(j);
while(i<=x)
{
if(j%i==0)
{
prime=0;
break;
}
i++;
}
if(prime)
printf(" %d ",j);
}
getch();
11
41
Program # 34
To find the divisor of a number
#include<stdio.h>
#include<conio.h>
main()
{
int num1,num2;
clrscr();
printf("\nEnter the numbers : ");
scanf("%d",&num1);
printf("\nEnter the numbers : ");
scanf("%d",&num2);
if((num1%num2)==0)
printf("\n%d is divisible by %d",num1,num2);
else
printf("\n%d is not divisible by %d",num1,num2);
getch();
}
42
Program # 35
Write a menu-based program in C, which can take two integer numbers
Num1 and Num2 from the user and then do all the arithmetic operations
Between them.
#include<stdio.h>
#include<math.h>
main()
{
float num1, num2,j;
int choice;
clrscr();
printf("\n
YOU ARE HAVING FOLLOWING CHOICES\n ");
printf("**************************************");
printf("\n1. TO ADD TWO NUMBERS");
printf("\n2. TO SUBTRACT TWO NUMBERS");
printf("\n3. TO MULTIPLY TWO NUMBERS");
printf("\n4. TO DEVIDE TWO NUMBERS");
printf("\n5. TO FIND SOME POWER OF A NUMBER");
printf("\n6. EXIT");
printf("\n\nENTER YOUR CHOICE : ");
scanf("%d",&choice);
printf("\n\nENTER TWO NUMBERS \n");
printf("Number 1 = ");
scanf("%f",&num1);
printf("Number 2 = ");
scanf("%f",&num2);
switch(choice)
{
case 1: printf ("\nSUM = %f",num1 + num2); break;
case 2: printf ("\nDIFFERENCE = %f",num1 - num2); break;
case 3: printf ("\nPRODUCT = %f",num1 * num2); break;
case 4: printf ("\nQUOTIENT = %f",num1/num2); break;
num2,j);
case 5: j = pow(num1,num2);
printf ("\n%f RAISED TO POWER %f = %f",num1,
break;
case 6: exit();
}
getch();
/*Output of the program */
43
YOUR CHOICE : 1
44
program # 36
Write down the Header Files of each of the following functions: getch(), printf(), clrscr(), sqrt(), fflush()
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{
45
46
Program # 37
What will be the output of each of the following executable statements
a)
printf(" \\\\\\\\ ");
b)
printf("""""""""""");
c)
i = 5; printf(" %%d%%",i++);
d)
printf("%d%d", 5>=5, i >= I);
e)
I = 5; j = I++*++I; printf(" I = %d and j = %d ",
I, j);
#include <stdio.h>
#include <conio.h>
main()
{
int i, j;
clrscr();
printf("\nThe outout of \"printf(\" \\\\\\\\ \");\" is");
printf(" \\\\\\\\ ");
printf("\n\nThe outout of \"printf("""""""""""");\" is");
printf("""""""""""");
i = 5;
printf("\n\nThe outout of \"printf(\" %%d%%\",i++);\" is");
printf(" %%d%%",i++);
printf("\n\nThe outout of \"printf(\"%d%d\", 5>=5, i >= I);;\"
is");
printf("%d%d", 5>=5, i >= j);
i = 5;
j = i++*++i;
printf("\n\ni = %d
getch();
}
and j = 36
47
Program # 38
Write a program in C, which can print N Real Numbers and return their
sum to main()..
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
/* Function Declaration */
void sum(int N);
/* Starting of the Function main */
main()
{
/* Local Variable Declaration */
int N;
clrscr();
/* Application Part */
printf("\nEnter the value of N : ");
scanf("%d",&N);
/* Calling of a Function */
sum (N);
getch();
}
/* Ending of the Function main */
/* Other Function Definition */
/* Starting of Function sum(int) */
void sum(int N)
{
int count;
float sum=0,num;
for(count = 1;count <= N; count++)
{
printf("\nEnter the number [%d] : ",count);
scanf("%f",&num);
sum = sum + num;
}
printf("\nThe sum of the numbers entered is : %f",sum);
}
/* Ending of Function sum(int) */
48
Program no.39
Write a program in C to find sum and average of square of
first 100 natural numbers.
# include<stdio.h>
# include<conio.h>
void main( )
{
int i, n=100,sum=0, p;
float avg;
for (i=1;i<=n;i++)
{
p=i*i ;
sum=sum+p;
}
avg =sum/(i-1);
printf ("\n The sum is %d", sum);
printf ("\n The average is %f", avg);
OUTPUT:
The sum is 10670
The average is 106.000000
49
50
progam NO.40
/*PROGRAM TO FIND PRODUCT OF 1ST 'N' NATURAL NUMBERS*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
long int prod;
clrscr();
printf("\nEnter the value of n:\t");
scanf("%d",&n);
i=1,prod=1;
a5:
i=i+1;
prod=prod*i;
if(i<n)
goto a5;
printf("\nProduct of 1st %d natural
numbers:\t%ld",n,prod);
getch();
}
Output:
Enter the value of n:
51
Program no. 41
Write a program in C to calculate simple and compound interest.
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
float p,r,t,si,ci,a,v;
clrscr();
printf ("\n Principal amount p=");
scanf ("%f",&p);
printf("\n rate of interest r= ");
scanf("%f",&r);
printf("\n Time period t=");
scanf("%f",&t);
si=(p*r*t)/100;
v=1+(r/100);
a=p*pow(v,t);
ci=a-p;
printf("\n Simple Interest = %f",si);
printf("\n Compound Interest = %f",ci);
getch();
}
OUTPUT:
Principal amount p =1000
Rate of interest r =10
Time period t =5
Simple Interest = 500.000000
Compound Interest = 610.51013
52
Program no 42
/* FIBBONACCI SERIES */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i;
double x1,x2,x;
x1=1;
x2=1;
printf("%12.0f\t%12.0f",x1,x2);
for(i=3;i<=25;i++)
{
x=x1+x2;
x1=x2;
x2=x;
printf("%12.0f",x);
}
getch();
}
OUTPUT:
1
13
21
53
Program no.43
/*GENERATING PRIME NO. BETWEEN 1 AND 100*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,count=0;
clrscr();
printf("\nprime nos. between 1 and 100\n");
for(i=1;i<=100;i++)
{
if(i==2)
i++;
j=2;
while(j<=(i/2))
{
if(i%j==0)
break;
j++;
}
if(j>(i/2))
{
count++;
printf("\t%d",i);
}
if(count%5==0)
printf("\n");
}
getch();
}
output:
prime nos. between 1 and 100
2
3
5
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
54
Program no.44
Write a program in C to generate prime nos. between 1 and 100 except
those divisible by 5.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n1=1,n2=100,j,i,temp,k=0,flag=0;
for (i=n1;i<=n2;i++)
{
temp=i;
if (temp==2)
{
flag=1;
}
if (temp%5==0)
{
flag=0;
continue;
}
for (j=2;j<=temp/2;j++)
{
if (temp%j!=0)
{
flag=1;
}
else
{
flag=0;
break;
}
}
if (flag==1)
{
printf ("\t %d",temp);
}
}
OUTPUT:
31
73
2
3 7
37 41 43
79 83 89
11 13 17 19 23 29
47 53 59 61 67 71
97
55
Program no. 45
/* to sort a list of 5 numbers in ascending order*/
#include<stdio.h>
#include<conio.h>
void main()
{ int i,j,t;
int n[5]; clrscr();
//to enter the data
printf("\n enter the 5 numbers to be sorted\n");
for(i=0;i<5;i++)
scanf("%d",&n[i]);
//the sorting operation
for(j=0;j<5;j++)
for(i=0;i<4-j;i++)
if(n[i]>n[i+1])
{t=n[i];
n[i]=n[i+1];
n[i+1]=t;
}
//to print the sorted list
printf("output:");
for(i=0;i<5;i++)
printf("\n%d ",n[i]);
getch();
OUTPUT:
enter the
12
13
5
34
76
output:
5
12
13
34
76
5 numbers to be sorted
56
Program no 46
/*CALCULATION OF THE VALUE OF nCr*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,y;
clrscr();
printf("\nCALCULATION OF nCr");
printf("\nenter the value of n ,r");
scanf("%d%d",&n,&r);
y=fact(n)/(fact(r)*fact(n-r));
printf("\n%dC%d = %d",n,r,y);
getch();
}
int fact (int a)
{
int f=1,j;
if((a==0)&&(a==1))
return(1);
else
{
for(j=1;j<=a;j++)
f=f*j;
return(f);
}
}
OUTPUT:
CALCULATION OF nCr
enter the value of n ,r6 2
6C2 = 15
57
Program no. 47
/*SUM OF DIGITS OF A GIVEN NUMBER*/
#include<stdio.h>
#include<conio.h>
void main()
{
int num,p,sum=0;
clrscr();
printf("\nenter any number==");
scanf("%d",&num);
while(num!=0)
{
p=num%10;
sum=sum+p;
num=num/10;
}
printf("\nsum of digit of given number==");
printf("%d",sum);
getch();
}
OUTPUT:
enter any number==4672
sum of digit given number==19
58
Program no. 48
/*CALCULATION OF TAXABLE INCOME*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float income,tax;
clrscr();
printf("\nenter taxable income:");
scanf("%f",&income);
if(income<=22000)
tax=0.0;
else if(income<=30000)
tax=0.1*(income-22000);
else if(income<=50000)
tax=800+0.2*(income-30000);
else if(income<=100000)
tax=4800+0.3*(income-50000);
else
tax=19800+0.4*(income-100000);
printf("\namount of tax on Rs%f equals to Rs%f",income,tax);
getch();
}
output:
enter taxable income:132000
amount of tax on Rs132000.000000 equals to Rs32600.000
59
program no. 49
/* CALCULATION OF SINE SERIES */
#include<stdio.h>
#include<math.c>
#include<conio.h>
int fact (int a);
void main()
{
double x,sum1=0.0,sum2=0.0,sum=0.0;
int y,i;
clrscr();
printf("\nenter the value for x:");
scanf("%lf",&x);
for(i=1;i<=30;i=i+4)
sum1=sum1+((pow(x,i))/fact(i));
for(i=3;i<=30;i=i+4)
sum2=sum2+((pow(x,i))/fact(i));
}
/*
sum=sum1-sum2;
printf("sum of cosine series cos(%lf)==%lf",x,sum);
getch();
//END OF MAIN
FUNCTION SUB PROGRAM
*/
60
Program no 50
/*CALCULATION OF COSINE SERIES*/
#include<stdio.h>
#include<math.h>
#include<conio.h>
long fact (int a);
void main()
{
double x,sum1=0.0,sum2=0.0,sum=0.0;
int y,i;
clrscr();
printf("\nenter the value for x:");
scanf("%lf",&x);
for(i=0;i<=30;i=i+4)
sum1=sum1+((pow(x,i))/fact(i));
for(i=2;i<=30;i=i+4)
sum2=sum2+((pow(x,i))/fact(i));
sum=sum1-sum2;
printf("sum of cosine series cos(%lf)==%lf",x,sum);
getch();
}
long fact (int a)
{
int j;
long f=1;
if(a==0)
return(1.0);
else
{
for(j=1;j<=a;j++)
f=f*j;
return(f);
}
}
OUTPUT:
enter the value for x:2
sum of cosine series cos(2.000000)==-1.350759
61
Program no.51
Write a program in C to find out the average marks of students and
print the marks of the topper.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int stu[20][20],i,j,m,n,roll=0;
float avg[20],sum=0.0,max=0.0;
clrscr();
printf("\n Enter the number of students =");
scanf("%d",&n);
printf ("\n Enter the number of subjects =");
scanf ("%d",&m);
for(i=1;i<=n;i++)
{
printf("\n Enter marks for %d student =",i);
for(j=1;j<=m;j++)
{
printf("\n Subject %d",j);
scanf("%d",&stu[i][j]);
}
}
for(i=1;i<=n;i++)
{
printf("\n The average of the %d student =\n",i);
sum=0.0;
avg[i]=0.0;
for(j=1;j<=m;j++)
{
sum=sum+stu[i][j];
}
avg[i]=(sum/m-1);
printf("%f\n",avg[i]);
if(avg[i]>max)
{
max=avg[i];
roll=i;
}
}
printf("\n The topper of the class is student %d with average
%f",roll,max);
getch();
}
62
OUTPUT:
Enter the number of students =3
Enter the number of subjects =5
Enter marks for 1 student
Subject 1=69
Subject 2=58
Subject 3=45
Subject 4=10
Subject 5=35
Enter marks for 2 student
Subject 1=47
Subject 2=25
Subject 3=16
Subject 4=97
Subject 5=46
Enter marks for 3 student
Subject 1=30
Subject 2=90
Subject 3=76
Subject 4=58
Subject 5=47
The
The
The
The
63
Program no.52
Write a program in C to reverse the digits of a number and find the sum
of its digits.
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2=0,rem,sum=0;
printf ("\n Enter the number to be reversed=");
scanf ("%d",&n1);
while (n1>0)
{r
rem=n1%10;
n1=n1/10;
n2=n2*10+rem;
}
printf ("\n The reversed number is %d",n2);
while (n2>0)
{
rem=n2%10;
n2=n2/10;
sum=sum+rem;
}
printf ("\n The sum of digits of reversed number is %d",sum);
}
OUTPUT:
Enter the number to be reversed =4567
The reversed number is 7654
The sum of digits of reversed number is 22
64
Programme no. 53
Write a program in C to find the sum, mean standard deviation and
variance of any numbers.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int n;
float x[20],sum;
float am,var,sd;
int i;
printf("\n enter the no. of terms=");
scanf("%d",&n);
printf("\n enter %d values \n",n);
for(i=0;i<n;i++)
scanf("%f",&x[i]);
sum=0.0;
for(i=0;i<n;i++)
sum = sum+x[i];
am = sum/n;
sum = 0.0;
for(i=0;i<n;i++)
sum = sum+((x[i]-am)*(x[i]-am));
var = sum/n;
sd = sqrt(var);
printf("\n ARITHMETIC MEAN, STD. DEVIATION AND VARIANCE =");
printf("%f\t%f\t%f",am,var,sd);
getch();
}
OUTPUT:
Enter the no. of terms= 5
Enter 5 values
4
9
8
6
12
Arithmetic MEAN, STD. DEVIATION and VARIANCE =7.800000 7.360000 2.712932
65
Programme no. 54
Write a program in C to convert binary number to decimal number.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num[10];
int i,x=2,j,y=0,n;
printf ("\n Enter the number of elements of binary number=");
scanf ("%d",&n);
j=n-1;
for (i=0;i<n;i++)
{
printf ("\n Enter the number[%d]=",i);
scanf ("%d",&num[i]);
}
while (j>=0)
{
for (i=0;i<n;i++)
{
y=y+num[i]*(pow(x,j));
j--;
}
}
printf (" The decimal number is %d",y);
}
OUTPUT:
Enter the number of elements of binary number= 5
Enter the number[0]= 1
Enter the number[1]= 0
Enter the number[2]= 1
Enter the number[3]= 1
Enter the number[4]= 1
The decimal number is 23
66
Programme no.55
Write a program in C to convert decimal number to binary number.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,j,n;
int rem[10];
printf ("\n Enter the number=");
scanf ("%d",&n);
do
{
rem[i]=n%2;
i=i+1;
n=n/2;
}
while (n>0);
for (j=i-1;j>=0;j--)
{
printf ("The binary number is %d",rem[j]);
}
}
OUTPUT:
Enter the number= 123
The binary number is 1111011
67
program no.56
Write a program in C to add two rectangular matrices.
#include<stdio.h>
#include<conio.h>
void main()
{
int mat1[10][10],mat2[10][10],mat3[10][10];
int i,j,m,n;
clrscr();
printf ("\n The number of rows are=");
scanf ("%d",&m);
printf ("\n The number of columns are=");
scanf ("%d",&n);
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf ("\n The matrix 1 is [%d][%d]=",i,j);
scanf ("%d",& mat1[i][j]);
}
}
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf ("\n The matrix 2is [%d][%d]=",i,j);
scanf ("%d",& mat2[i][j]);
}
}
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
mat3[i][j]= mat1[i][j]+mat2[i][j];
}
}
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf ("\n The resultant matrix is %d\n",mat3[i][j]);
}
printf ("\n");
}
}
OUTPUT:
The
The
The
The
The
The
number
number
matrix
matrix
matrix
matrix
of rows are=2
of columns are=2
1 is [0][0]= 1
1 is [0][1]= 2
1 is [1][0]= 3
1 is [1][1]= 4
68
The
The
The
The
matrix
matrix
matrix
matrix
2
2
2
2
is
is
is
is
[0][0]=
[0][1]=
[1][0]=
[1][1]=
5
6
7
8
69
Program no. 57
/*MULTIPLICATION OF MATRIX*/
#include<stdio.h>
#include<conio.h>
void main()
{int m,n,p,q,i,j,k;
int a[10][10],b[10][10],c[10][10];
clrscr();
printf("\nenter no. of row and col of matrix a:");
scanf("%d%d",&m,&n);
printf("\nenter no. of row and col of matrix b:");
scanf("%d%d",&p,&q);
if(n!=p)
{ printf("\nmatrix can't be multiplied\n");
goto end;
}
printf("\nenter matrix a\n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("\nenter matrix b\n");
for(i=0;i<p;++i)
{ for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{ c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("the product ofmatrix is:");
for(i=0;i<m;i++)
{printf("\n");
for(j=0;j<q;j++)
printf("%3d",c[i][j]);
}
end:
getch();
}
70
output:
enter no. of row and col of matrix a:3 3
enter no. of row and col of matrix b:3 2
enter matrix a
0 1 2
1 2 3
2 3 4
enter matrix b
1 -2
-1 0
2 -1
the product ofmatrix is:
3 -2
5 -5
7 -8
71
Program no. 58
/* To find out the binary, octal and hexadecimal equivalent of a number
*/
#include<stdio.h>
#include<math.h>
void bin(int);
void oct(int);
void hex(int);
void menu(int);
main()
{ int num;
clrscr();
printf("please enter a number: ");
scanf("%d", &num);
menu(num);
getch();
}
void menu(int n)
{int flag;
printf("plz enter 1 for binary equivalent, 2 for octal equivalent, 3
for hexadecimal equivalent");
scanf("%d", &flag);
switch(flag)
{ case 1 : bin(n); break;
case 2 : oct(n); break;
case 3 : hex(n); break;
default: printf("\n U have entered wrong choice : ");
break;
}
}
void bin(int n)
{ int i,j=0;
int rem;
long int bin=0;
for(i=n;i>0;)
{ rem=i%2;
i=i/2;
bin=bin+pow(10,j)*rem;
j++;
}
printf("The binary equivalent is : %d",bin);
}
void oct(int n)
{ int i,j=0; int rem;
long int oct=0;
for(i=n;i>0;)
{ rem=i%8;
i=i/8;
oct=oct+pow(10,j)*rem;
j++;
}
printf("The octal equivalent is %d",oct);
return oct;
}
void hex(int n)
{ int i,rem;
72
printf("The hexadecimal equivalent of %d is : ",n);
for(i=n;i>0;)
{ rem=i%16;
if(rem<10)
{ printf("%d",rem);
}
else
{ switch(rem)
{ case 10 : printf("A"); break;
case 11 : printf("B"); break;
case 12 : printf("C"); break;
case 13 : printf("D"); break;
case 14 : printf("E"); break;
case 15 : printf("F"); break;
}
}
i=i/16;
}
}
/****************************************
output:
please enter a number: 28
plz enter 1 for binary equivalent, 2 for octal equivalent, 3 for
hexadecimal equ
ivalent2
The octal equivalent is 34
****************************************/
73
Program no. 59
/* To find gcd and lcm of given two numbers */
#include <stdio.h>
#include <conio.h>
main()
{
int a,b,r,gcd,x,y,lcm;
clrscr();
printf("ENTER TWO NUMBERS :");
scanf("%d",&a);
printf("ENTER TWO NUMBERS :");
scanf("%d",&b);
x=a;
y=b;
do
{
r=a%b;
if (r==0)
{
gcd=b;
}
else
{
a=b;
b=r;
}
} while(r!=0);
lcm=x*y/gcd;
printf("\nGCD OF %d AND %d IS %d\n",x,y,gcd);
printf("\nLCM OF %d AND %d IS %d",x,y,lcm);
getch();
}
/* Output of the program */
ENTER TWO NUMBERS :12
ENTER TWO NUMBERS :24
GCD OF 12 AND 24 IS 12
LCM OF 12 AND 24 IS 24
74
Program no. 60
/*CALCULATION OF THE VALUE OF nPr*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,y;
clrscr();
printf("\nCALCULATION OF nPr");
printf("\nenter the value of n ,r");
scanf("%d%d",&n,&r);
y=fact(n)/(fact(n-r));
printf("\n%dP%d = %d",n,r,y);
getch();
}
int fact (int a)
{
int f=1,j;
if((a==0)&&(a==1))
return(1);
else
{
for(j=1;j<=a;j++)
f=f*j;
return(f);
}
}
OUTPUT:
CALCULATION OF nPr
enter the value of n ,r6 2
6P2 = 30
75
Program # 61
Write a menu-based program in C, which can perform all the arithmetic
operations, each by using different functions.
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
/* Function Declaration */
void accept();
void display();
int add(int num1,int num2);
int subtract(int num1,int num2);
int multiply(int num1,int num2);
int divide(int num1, int num2);
/* Global Variable Declaration */
int num1 , num2;
/* Starting of the Function main */
main()
{
/* Local Variable Declaration */
int option;
clrscr();
/* Application Part */
/* Menu Design */
gotoxy(30,10);
printf("1)
Add");
gotoxy(30,12);
printf("2)
Subtract");
gotoxy(30,14);
printf("3)
Multiply");
gotoxy(30,16);
printf("4)
Divide");
gotoxy(30,20);
printf("5)
Quit");
gotoxy(30,25);
printf("Enter your choice : ");
scanf("%d",&option);
switch(option)
{
case 1 :
clrscr();
/* Clear the Screen */
accept();
/* Accept the input */
printf("\n\nThe sum of the numbers is :
%d",add(num1,num2));
break;
case 2 :
76
clrscr();
accept();
printf("\n\nThe subtraction of two numbers is :
%d",subtract(num1,num2));
break;
case 3 :
clrscr();
accept();
printf("\n\nThe multiplication of two numbers is :
%d",multiply(num1,num2));
break;
case 4 :
clrscr();
accept();
printf("\n\nThe division of two numbers is :
%d",divide(num1,num2));
break;
case 5 :
exit();
}
getch();
}
/* Ending of the Function main */
/* Other Function Definition */
/* Starting of Function <<accept>> */
void accept()
{
printf("Enter the value of num1 : ");
scanf("%d",&num1);
printf("\nEnter the value of num2 : ");
scanf("%d",&num2);
}
/* End of Function <<accept>> */
77
Add
2)
Subtract
3)
Multiply
4)
Divide
5)
Quit
1)
Add
2)
Subtract
3)
Multiply
4)
Divide
5)
Quit
78
Enter the value of num2 : 3
The multiplication of two numbers is : 36
*/
79
Program # 62
A program contains two functions one for checking out that whether a
given number is prime or not, and second is for finding out the prime
factors of a given number, A person wants to check that whether a given
number is prime or not. If the number is not a prime number, then he
wants to find out its prime factors. Write a program in C, which can
solve his problem..
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
#include <math.h> /* Contains the declaration of sqrt() */
/* Function Declaration */
int prime(int num);
void factor(int num);
/* Starting of the Function main */
main()
{
/* Local Variable Declaration */
int num;
clrscr();
/* Application Part */
printf("\nEnter the value of num : ");
scanf("%d",&num);
/* Calling of a Function */
if(prime(num))
printf("\n\nThe number is prime");
else
{
printf("\n\nThe factors are : ");
factor(num);
}
getch();
}
/* Ending of the Function main */
/* Other Function Definition */
/* Starting of Function <<prime>> */
int prime(int num)
{
int i,sqr,primeflag;
sqr = sqrt(num);
primeflag = 1;
for (i=2;i<=sqr;i++)
80
{
if(num%i==0)
{
primeflag = 0;
break;
}
}
}
return (primeflag);
11
81
Program # 63
Merging and Sorting of Array
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
/* Starting of the Function main */
main()
{
/* Local Variable Declaration */
int a[5] = {2,4,6,2,7};
int b[8] = {2,24,6,20,18,7,14,12};
int c[13],i,j,count=0;
char flag = 'y';
clrscr();
/* Application Part */
for(i=0;i<8;i++)
{
flag = 'y';
for(j=0;j<=count;j++)
{
if(a[i] == c[j])
{
flag = 'y';
break;
}
}
if(flag == 'n')
{
count++;
c[count] = a[i];
}
}
for(i=0;i<8;i++)
{
flag = 'n';
for(j=0;j<=count;j++)
{
if(b[i] == c[j])
{
flag = 'y';
break;
}
}
if(flag == 'n')
{
82
count++;
c[count] = b[i];
}
for(i=0;i<=count;i++)
printf(" %d ",c[i]);
getch();
}
/* Ending of the Function main */
1824
24
20
18
14
12
83
Program # 64
From a given matrix print all the diagonal elements on the screen
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
/* Starting of the Function main */
main()
{
/* Local Variable Declaration */
int mat[3][3],i,j;
clrscr();
/* Application Part */
/* Accept the elements of matrix */
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the value at [%d][%d] : ",i,j);
scanf("%d",&mat[i][j]);
}
}
/* Printing of diagonal elements */
printf("\nThe diagonal elements of the matrix are .......\n");
for(i=0;i<3;i++)
{
printf("\n[%d][%d] : %d",i,i,mat[i][i]);
}
getch();
}
/* Ending of the Function main */
/*Output of the program*/
/*
Enter
Enter
Enter
Enter
Enter
Enter
Enter
the
the
the
the
the
the
the
value
value
value
value
value
value
value
at
at
at
at
at
at
at
[0][0]
[0][1]
[0][2]
[1][0]
[1][1]
[1][2]
[2][0]
:
:
:
:
:
:
:
1
2
3
2
3
4
5
84
Enter the value at [2][1] : 4
Enter the value at [2][2] : 3
The diagonal elements of the matrix are .......
[0][0] : 1
[1][1] : 3
[2][2] : 3
*/
85
Program # 65
/* From a given matrix print all the upper diagonal elements on the
screen */
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
/* Starting of the Function main */
main()
{
/* Local Variable Declaration */
int mat[3][3],i,j;
clrscr();
/* Application Part */
/* Accept the elements of matrix */
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the value at [%d][%d] : ",i,j);
scanf("%d",&mat[i][j]);
}
}
/* Printing of diagonal elements */
printf("\nThe upper diagonal elements of the matrix are
.......\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i<=j)
printf("\n[%d][%d] : %d",i,j,mat[i][j]);
}
}
getch();
}
/* Ending of the Function main */
86
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
the
the
the
the
the
the
the
the
the
value
value
value
value
value
value
value
value
value
at
at
at
at
at
at
at
at
at
[0][0]
[0][1]
[0][2]
[1][0]
[1][1]
[1][2]
[2][0]
[2][1]
[2][2]
:
:
:
:
:
:
:
:
:
1
2
4
5
54
45
6
56
54
:
:
:
:
:
:
1
2
4
54
45
54
87
Program # 66
/* Arithmetic operations with matrices */
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,k;
int mat1[3][3],mat2[3][3],mat3[3][3];
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter [%d][%d] value for Matrix 1 : ",i,j);
scanf("%d",&mat1[i][j]);
}
}
/*
clrscr();
*/
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter [%d][%d] value for Matrix 2 : ",i,j);
scanf("%d",&mat2[i][j]);
}
}
/*clrscr();*/
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t%d",mat1[i][j]);
}
printf("\t\t");
for(j=0;j<3;j++)
{
printf("\t%d",mat2[i][j]);
}
printf("\n");
}
printf("\n\t\tThe addition and the difference of the matrices
is \n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t%d",mat1[i][j]+mat2[i][j]);
}
88
printf("\t\t");
for(j=0;j<3;j++)
{
printf("\t%d",mat1[i][j] - mat2[i][j]);
}
printf("\n");
}
printf("\n\t\tThe transpose of the matrix A\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(j>i)
{
int temp;
temp = mat1[i][j];
mat1[i][j] = mat1[j][i];
mat1[j][i] = temp;
}
}
}
for(i=0;i<3;i++)
{
printf("\t\t");
for(j=0;j<3;j++)
{
printf("\t%d",mat1[i][j]);
}
printf("\n");
}
printf("\n\t\tThe Multiplication of the matrices is \n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
mat3[i][j] = 0;
for(k=0;k<3;k++)
{
mat3[i][j] = mat3[i][j] + (mat1[i][k] *
mat2[k][j]);
}
}
}
for(i=0;i<3;i++)
{
printf("\t\t");
for(j=0;j<3;j++)
{
printf("\t%d",mat3[i][j]);
}
printf("\n");
}
}
89
/*Output of the program*/
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
[0][0]
[0][1]
[0][2]
[1][0]
[1][1]
[1][2]
[2][0]
[2][1]
[2][2]
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
[0][0]
[0][1]
[0][2]
[1][0]
[1][1]
[1][2]
[2][0]
[2][1]
[2][2]
value
value
value
value
value
value
value
value
value
for
for
for
for
for
for
for
for
for
Matrix
Matrix
Matrix
Matrix
Matrix
Matrix
Matrix
Matrix
Matrix
1
1
1
1
1
1
1
1
1
:
:
:
:
:
:
:
:
:
12
12
23
34
34
45
44
55
45
value
value
value
value
value
value
value
value
value
12
34
44
for
for
for
for
for
for
for
for
for
12
34
55
Matrix 2
Matrix 2
Matrix 2
Matrix 2
Matrix 2
Matrix 2
Matrix 2
Matrix 2
Matrix 2
23
45
45
:
:
:
:
:
:
:
:
:
4
44
45
67
56
55
45
45
44
4
67
45
44
56
45
45
55
44
56
90
100
68
100
89
8
-33
-1
-32
-22
10
34
34
45
44
55
45
4412
4907
5557
4346
4830
5490
-22
-10
1
90
Program # 67
Demonstration of pointers
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
/* Starting of the Function main */
main()
{
/* Local Variable Declaration */
int i=10,*iptr;
char c='A' ,*cptr;
clrscr();
/* Application Part */
iptr = &i;
cptr = &c;
/* Display of variable contents and address */
printf("\nValue
printf("\nValue
printf("\nValue
printf("\nValue
getch();
of
of
of
of
i
i
c
c
:
:
:
:
%d
%d
%c
%c
,
,
,
,
Address
Address
Address
Address
of
of
of
of
i
i
c
c
:
:
:
:
%u",i,&i);
%u",*iptr,iptr);
%u",c,&c);
%u",*cptr,cptr);
}
/* Ending of the Function main */
/*Output of the program */
Value
Value
Value
Value
of
of
of
of
i
i
c
c
:
:
:
:
10 , Address of i
10 , Address of i
A , Address of c
A , Address of c
:
:
:
:
65524
65524
65523
65523
91
Program # 68
/*Write a program to assign the value of one pointer variable *ptr to
another pointer *ptr1 and display the contents of both of these pointer
variables.*/
/* Application Part */
i = 5;
iptr = &i;
iptr2 = iptr;
printf("\nThe contents of i using assigned pointers : %d",*iptr2);
getch();
}
/* Ending of the Function main */
/*Output of the program */
The contents of i using assigned pointers : 5
92
Program # 69
A program is having three variables int I, char c, and float f. use
void pointer to display the content of all these variables.
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
/* Starting of the Function main */
main()
{
/* Local Variable Declaration */
int i;
char c;
float f;
void *ptr;
clrscr();
/* Application Part */
i = 5;
c = 'A';
f = 15.36;
/* Display of int value using void pointer */
ptr = &i;
printf("\nThe value of i using void pointer : %d",*((int *)ptr));
/* Display of char value using void pointer */
ptr = &c;
printf("\nThe value of c using void pointer : %c",*((char *)ptr));
/* Display of float value using void pointer */
ptr = &f;
printf("\nThe value of f using void pointer : %f",*((float
*)ptr));
getch();
}
/* Ending of the Function main */
/*Output of the program */
The value of i using void pointer : 5
The value of c using void pointer : A
The value of f using void pointer : 15.360000
93
Program # 70
/*Write a program to display the memory address of a variable using
pointer before incrementation and after incrementation.*/
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
/* Starting of the Function main */
main()
{
/* Local Variable Declaration */
int i,*iptr;
clrscr();
/* Application Part */
iptr = &i;
printf("\nThe address in iptr before incrementation is :
%u",iptr);
iptr++;
/* Pointer incrementation */
}
/* Ending of the Function main */
/*Output*/
The address in iptr before incrementation is : 65524
The address in iptr after incrementation is : 65526
94
Program # 71
/*A program to display the contents of the pointer variables using
arithmetic operations.*/
/* Header File Inclusion */
#include <stdio.h>
#include <conio.h>
/* Starting of the Function main */
main()
{
/* Local Variable Declaration */
int i,*ptr;
clrscr();
/* Application Part */
/* display of address stored in ptr */
printf("\nThe address in ptr : %u",ptr);
/* Addition of scalar with pointer */
ptr = ptr + 3;
printf("\n\nThe address in ptr : %u",ptr);
/* Subtraction of scalar with pointer */
ptr = ptr - 2;
printf("\n\nThe address in ptr : %u",ptr);
getch();
}
/* Ending of the Function main */
/*Output of the program */
The address in ptr : 916
The address in ptr : 922
The address in ptr : 918