Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
24 views72 pages

Pps Progs Manual

The document contains multiple C programming examples demonstrating various operators and concepts, including arithmetic, logical, and assignment operators, as well as type conversion, conditional statements, and loops. It includes programs for calculating interest, finding maximum and minimum values, generating multiplication tables, and checking for prime numbers. Each program is accompanied by input and output examples to illustrate its functionality.

Uploaded by

Faizan Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views72 pages

Pps Progs Manual

The document contains multiple C programming examples demonstrating various operators and concepts, including arithmetic, logical, and assignment operators, as well as type conversion, conditional statements, and loops. It includes programs for calculating interest, finding maximum and minimum values, generating multiplication tables, and checking for prime numbers. Each program is accompanied by input and output examples to illustrate its functionality.

Uploaded by

Faizan Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

1.

Write a simple program that prints the results of all the operators available in C (including
pre/post increment, bitwise and /or/not, etc.).Read required operand values from standard
input.

// C Program to demonstrate the working of arithmetic operators


#include <stdio.h>
int main()
{
int a = 9,b = 4, c;

c = a+b;
printf("a+b = %d \n",c);

c = a-b;
printf("a-b = %d \n",c);

c = a*b;
printf("a*b = %d \n",c);

c=a/b;
printf("a/b = %d \n",c);

c=a%b;
printf("Remainder when a divided by b = %d \n",c);

return 0;
}

Output

a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1

// C Program to demonstrate the working of increment and decrement operators


#include <stdio.h>
int main()
{
int a = 10, b = 100;

1 |Page
float c = 10.5, d = 100.5;

printf("++a = %d \n", ++a);

printf("--b = %d \n", --b);

printf("++c = %f \n", ++c);

printf("--d = %f \n", --d);

return 0;
}

Output

++a = 11
--b = 99
++c = 11.500000
++d = 99.50000

// C Program to demonstrate the working of assignment operators


#include <stdio.h>
int main()
{
int a = 5, c;

c = a;
printf("c = %d \n", c);

c += a; // c = c+a
printf("c = %d \n", c);

c -= a; // c = c-a
printf("c = %d \n", c);

c *= a; // c = c*a
printf("c = %d \n", c);

c /= a; // c = c/a
printf("c = %d \n", c);

2 |Page
c %= a; // c = c%a
printf("c = %d \n", c);

return 0;
}

Output

c=5
c = 10
c=5
c = 25
c=5
c=0

// C Program to demonstrate the working of arithmetic operators


#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

printf("%d == %d = %d \n", a, b, a == b); // true


printf("%d == %d = %d \n", a, c, a == c); // false

printf("%d > %d = %d \n", a, b, a > b); //false


printf("%d > %d = %d \n", a, c, a > c); //false

printf("%d < %d = %d \n", a, b, a < b); //false


printf("%d < %d = %d \n", a, c, a < c); //true

printf("%d != %d = %d \n", a, b, a != b); //false


printf("%d != %d = %d \n", a, c, a != c); //true

printf("%d >= %d = %d \n", a, b, a >= b); //true


printf("%d >= %d = %d \n", a, c, a >= c); //false

printf("%d <= %d = %d \n", a, b, a <= b); //true


printf("%d <= %d = %d \n", a, c, a <= c); //true

3 |Page
return 0;

}
Output

5 == 5 = 1
5 == 10 = 0
5>5=0
5 > 10 = 0
5<5=0
5 < 10 = 1
5 != 5 = 0
5 != 10 = 1
5 >= 5 = 1
5 >= 10 = 0
5 <= 5 = 1
5 <= 10 = 1

// C Program to demonstrate the working of logical operators

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) equals to %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) equals to %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) equals to %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) equals to %d \n", result);

result = !(a != b);


printf("!(a == b) equals to %d \n", result);

4 |Page
result = !(a == b);
printf("!(a == b) equals to %d \n", result);

return 0;
}

Output

(a == b) && (c > b) equals to 1


(a == b) && (c < b) equals to 0
(a == b) || (c < b) equals to 1
(a != b) || (c < b) equals to 0
!(a != b) equals to 1
!(a == b) equals to 0

sizeof Operator

#include <stdio.h>
int main()
{
int a, e[10];
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
printf("Size of integer type array having 10 elements = %lu bytes\n", sizeof(e));
return 0;
}

Output

Size of int = 4 bytes


Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte
Size of integer type array having 1

5 |Page
C conditional Operator

#include <stdio.h>
int main(){
char February;
int days;
printf("If this year is leap year, enter 1. If not enter any integer: ");
scanf("%c",&February);

// If test condition (February == 'l') is true, days equal to 29.


// If test condition (February =='l') is false, days equal to 28.
days = (February == '1') ? 29 : 28;

printf("Number of days in February = %d",days);


return 0;
}

Output

If this year is leap year, enter 1. If not enter any integer: 1


Number of days in February = 29

2. Write a simple program that converts one given data type to another using auto conversion
and casting. Take the values from standard input.

// An example of implicit conversion


#include<stdio.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character c

// y implicitly converted to int. ASCII


// value of 'a' is 97
x = x + y;

// x is implicitly converted to float


float z = x + 1.0;

printf("x = %d, z = %f", x, z);


return 0;

6 |Page
}
Output:

x = 107, z = 108.000000

// C program to demonstrate explicit type casting


#include<stdio.h>

int main()
{
double x = 1.2;

// Explicit conversion from double to int


int sum = (int)x + 1;

printf("sum = %d", sum);

return 0;
}

Output:

sum = 2

3. Write a program for find the max and min from the three numbers.

#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter 3 numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("Mximum number is a = %d",a);
else if(b>a && b>c)
printf("Mximum number is b = %d",b);
else
printf("Mximum number is c = %d",c);
if(a<b && a<c)
printf("Minimum number is a = %d",a);
else if(b<a && b<c)
printf("Minimum number is b = %d",b);

7 |Page
else
printf("Minimum number is c = %d",c);
}

4. Write a program for the simple compound interest.

#include<stdio.h>

#include<math.h>

int main()

int p,t;

float r,si,amount,ci;

printf("Please enter principal,time and rate of interest");

scanf("%d%d%f",&p,&t,&r);

si=p*t*r/100;

//Simple Interest formula is p*t*r

printf("\nSimple interest = %.3f",si);

//Compound Interest formula is below

amount=p*pow((1 +r/100),t);

ci=amount-p;

printf("\nCompound interest = %.3f",ci);

Input
Enter principle (amount): 1200
Enter time: 2
Enter rate: 5.4

Output

Compound Interest = 133.099197

8 |Page
5. Write program that declares class awarded for a given percentage of marks, where
mark<40%=failed, 40% to <60%= second class, 60% to <70%=First
class, >=70%=Distinction, read percentage from standard input.

#include<stdio.h>
void main()
{
int m1,m2,m3,total;
float per;
clrscr();
printf("Enter 3 Nos.");
scanf("%D%D%D",&m1,&m2,&m3);
total=m1+m2+m3;
per=total*100/300;
if(per>=60&&per<=100)
printf("You are 1st :");
else if(per>=50&&per<=60)
printf("You are 2nd");
else if(per>=40&&per<=50)
printf("You are 3rd");
else
printf("You are Fail");
getch();
}

6. Write program that prints multiplication table for a given number and the number of rows
in the table. for example, for a number 5 and rows=3,the output should be:
5x1=5
5x2=10
5x3=15

#include <stdio.h>

int main()

int n, i;

printf("Enter an integer: ");

scanf("%d",&n);

9 |Page
for(i=1; i<=10; ++i)

printf("%d * %d = %d \n", n, i, n*i);

return 0;

Output

Enter an integer: 5

5 * 1 = 5

5 * 2 = 10

5 * 3 = 15

7. Write a program that shows the binary equivalent of a given positive number
Between 0 to 255.

#include <stdio.h>

int main()
{
long long decimal, tempDecimal, binary;
int rem, place = 1;

binary = 0;

/* Input decimal number from user */


printf("Enter any decimal number: ");
scanf("%lld", &decimal);
tempDecimal = decimal;

10 | P a g e
/* Decimal to binary conversion */
while(tempDecimal > 0)
{
rem = tempDecimal % 2;

binary = (rem * place) + binary;

tempDecimal /= 2;

place *= 10;
}

printf("Decimal number = %lld\n", decimal);


printf("Binary number = %lld", binary);

return 0;

} of decimal = 0111000

Enter a decimal integer


134
Input number is = 134
Its binary equivalent is = 10000110
No.of 1's in the binary number is = 3

8. A building has 10 floor heights of 3 meters each. A ball is dropped from the top of the
building. Find the time taken by the ball to reach each floor.(use formula s=ut+(1/2)at^2
where u and a are the initial velocity in m/sec(=0) and acceleration in m/sec^2(=9.8 m/s^2)).

#include<stdio.h>
#include<conio.h>
void main()
{
float u,t,a,S;
clrscr();
printf(“enter values u,t,a”);
scanf(“%f %f %f”, &u,&t,&a);
S=(u*t)+(0.5*a*t*t);
printf(“\n S = %f”, S);

11 | P a g e
}

Input:- enter values u,t,a


U=10,t=4,a=4.9
Output:- S =79.200

9. Write a program which takes two integer operands and one operator from the user, performs
the operation and then prints the result.(consider the operators +,-,*,/,% and use switch
statement).

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b;
printf("\n Enter two numbers:");
scanf("%d%d",&a,&b);
int choice;
printf("\n Operation to perform :");
printf("\n1.Addition \n2.Subtraction \n3.multiplication
\n4.division \n5.modulus ");
scanf("\n%d",&choice);
switch(choice)
{
case 1: printf("\n Addition = %d",a+b);
break;
case 2: printf("\n Subtraction = %d",a-b);
break;
case 3: printf("\n Multiplication = %d",a*b);
break;
case 4: printf("\n Division = %d",a/b);
break;
case 5: printf("\n Modulus = %d",a%b);
break;
default:printf("\n Invalid choice");
}
getch();
}
OUTPUT:
1.Addition

12 | P a g e
2.Subtraction
3.Multiplication
4.Division
Enter the values of a & b: 20 15
Enter your Choice : 1
Sum of 20 and 15 is : 35

10. Write a program that find if a given number is a prime number.

#include <stdio.h>

int main()

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d",&n);

for(i=2; i<=n/2; ++i)

// condition for nonprime number

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);

13 | P a g e
return 0;

Output

Enter a positive integer: 29

29 is a prime number.

11. Write a C program to find the sum of individual digits of positive integer and test given
number is palindrome.

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main ()

int number = 0, digit = 0, sumOfDigits = 0;

clrscr();

printf("Enter any number\n ");

scanf("%d", &number);

while (number != 0)

digit = number % 10;

sumOfDigits = sumOfDigits + digit;

number = number / 10;

printf ("Sum of individual digits of a given number is %d", sumOfDigits);

getch();

14 | P a g e
Input & Output:

Enter any number

1234

Sum of individual digits of a given number is 10

12. A Fibonacci sequence is defined as follows: the first and second terms in the sequence are
0 and 1.subsequent terms are found by adding the preceding two terms in the sequence.
Write a C program to generate the first n terms of the sequence.

#include<stdio.h>

#include<conio.h>

void main()

int a = 0, b = 1, lengthOfSeries = 0, counter, sum = 0;

clrscr();

printf("Enter the length of series \n ");

scanf("%d", &lengthOfSeries);

printf("Fibonacci series\n");

printf("%d %d", a, b);

for(counter = 2; counter < lengthOfSeries; counter++)

sum = a + b;

printf(" %d",sum);

15 | P a g e
a = b;

b = sum;

getch();

INPUT & OUTPUT:

Enter the length of series

15

Fibonacci series

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

13. Write a C program to generate all the prime numbers between 1 and n, where n is a
value supplied by the user.

#include<stdio.h>

#include<conio.h>

void main()

int n, i, j, count;

clrscr();

printf("Prime no.series\n");

16 | P a g e
printf("Enter any number\n");

scanf("%d", &n);

printf("The prime numbers between 1 to %d\n",n);

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

count = 0;

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

if(i % j == 0)

count++;

if(count == 2)

printf("%d\t", i);

getch();

INPUT & OUTPUT:

Prime no. series

17 | P a g e
Enter any number

10

The prime numbers between 1 to 10

2 3 5 7

14. Write a C program to find the roots of a quadratic equation.

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

float a, b, c, d, root1, root2;

clrscr();

printf("Enter the values of a, b, c\n");

scanf("%f%f%f", &a, &b, &c);

if(a == 0 || b == 0 || c == 0)

printf("Error: Roots can't be determined");

else

d = (b * b) - (4.0 * a * c);

if(d > 0.00)

printf("Roots are real and distinct \n");

root1 = -b + sqrt(d) / (2.0 * a);

18 | P a g e
root2 = -b - sqrt(d) / (2.0 * a);

printf("Root1 = %f \nRoot2 = %f", root1, root2);

else if (d < 0.00)

printf("Roots are imaginary");

root1 = -b / (2.0 * a) ;

root2 = sqrt(abs(d)) / (2.0 * a);

printf("Root1 = %f +i %f\n", root1, root2);

printf("Root2 = %f -i %f\n", root1, root2);

else if (d == 0.00)

printf("Roots are real and equal\n");

root1 = -b / (2.0 * a);

root2 = root1;

printf("Root1 = %f\n", root1);

printf("Root2 = %f\n", root2);

getch();

Input & Output:

Enter the values of a, b, c

1 2 3

Roots are imaginary,Root1 = -1.000 + i,Root2 = -1.000 - i

15. Write a C program to calculate the following, where x is a fractional value.


1-x/2+x^2/4-x^3/6

#include <stdio.h>

19 | P a g e
void main()

float x,sum,t,d;

int i,n;

printf("Input the Value of x :");

scanf("%f",&x);

printf("Input the number of terms : ");

scanf("%d",&n);

sum =1; t = 1;

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

d = (2*i)*(2*i-1);

t = -t*x*x/d;

sum =sum+ t;

printf("\nthe sum = %f\nNumber of terms = %d\nvalue of x =


%f\n",sum,n,x);

Input the Value of x :2


Input the number of terms : 5

the sum = -0.415873


Number of terms = 5
value of x = 2.000000

16. Write a C program to read in two numbers, x and n, and


then compute the sum of this geometric progression:
1+x+x^2+x^3+…………+x^n. for example: if n is 3 and x is 5,
then the program computes 1+5+25+125.

20 | P a g e
#include <stdio.h>

#include <conio.h>

#include <math.h>

void main()

int n, x, i, sum = 0;

clrscr();

printf("Enter the limit\n");

scanf("%d", &n);

printf("Enter the value of x\n");

scanf("%d", &x);

if(x < 0 || n < 0)

printf("illegal value");

else

for(i = 0; i <= n; i++)

sum=sum + pow(x, i);

21 | P a g e
printf("sum=%d", sum);

getch();

INPUT & OUTPUT:

Enter the limit

Enter the value of x

sum=31

17. Write a C program to find the minimum, maximum and


average is an array of integers.

#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

int main()
{
int arr[MAX_SIZE];
int i, max, min, size;

/* Input size of the array */


printf("Enter size of the array: ");
scanf("%d", &size);

/* Input array elements */


printf("Enter elements in the array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

22 | P a g e
/* Assume first element as maximum and minimum */
max = arr[0];
min = arr[0];

/*
* Find maximum and minimum in all array elements.
*/
for(i=1; i<size; i++)
{
/* If current element is greater than max */
if(arr[i] > max)
{
max = arr[i];
}

/* If current element is smaller than min */


if(arr[i] < min)
{
min = arr[i];
}
}

/* Print maximum and minimum element */


printf("Maximum element = %d\n", max);
printf("Minimum element = %d", min);

return 0;
}

// C program to find the average is an array of integers.

#include <stdio.h>

int main()

int n, i;

float num[100], sum = 0.0, average;

23 | P a g e
printf("Enter the numbers of elements: ");

scanf("%d", &n);

while (n > 100 || n <= 0)

printf("Error! number should in range of (1 to 100).\n");

printf("Enter the number again: ");

scanf("%d", &n);

for(i = 0; i < n; ++i)

printf("%d. Enter number: ", i+1);

scanf("%f", &num[i]);

sum += num[i];

average = sum / n;

printf("Average = %.2f", average);

return 0;

Output

Enter the numbers of elements: 6

1. Enter number: 45.3

2. Enter number: 67.5

3. Enter number: -45.6

24 | P a g e
4. Enter number: 20.34

5. Enter number: 33

6. Enter number: 45.6

Average = 27.69

18. Write a functions to compute mean, variance, Standard Deviation, sorting of n elements
in a single dimension array.

#include <stdio.h>
#include <math.h>
#define MAXSIZE 10

void main()
{
float x[MAXSIZE];
int i, n;
float average, variance, std_deviation, sum = 0, sum1 = 0;

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];
}
average = sum / (float)n;
/* Compute variance and standard deviation */
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow((x[i] - average), 2);
}

25 | P a g e
variance = sum1 / (float)n;
std_deviation = sqrt(variance);
printf("Average of all elements = %.2f\n", average);
printf("variance of all elements = %.2f\n", variance);
printf("Standard deviation = %.2f\n", std_deviation);
}

output
Enter the value of N
5
Enter 5 real numbers
34
88
32
12
10
Average of all elements = 35.20
variance of all elements = 794.56
Standard deviation = 28.19

19. Write a C program that uses functions to perform the following:


i Addition of two matrices
ii Multiplication of two matrices
iii Transpose of a matrix with memory dynamically allocated for the new matrix as row and column
counts may not be same.

i Addition of two matrices


#include <stdio.h>

#include <conio.h>

void main()

int a[3][3], b[3][3], c[3][3], i, j;

clrscr();

26 | P a g e
printf("Enter the elements of 3*3 matrix a \n");

for(i = 0; i < 3; i++)

for(j = 0; j < 3; j++)

scanf("%d", &a[i][j]);

printf("Enter the elements of 3*3 matrix b \n");

for(i = 0; i < 3; i++)

for(j = 0; j < 3; j++)

scanf("%d", &b[i][j]);

for(i = 0; i < 3; i++)

for(j = 0; j < 3; j++)

27 | P a g e
c[i][j] = a[i][j] + b[i][j];

printf("The resultant 3*3 matrix c is \n");

for(i = 0; i < 3; i++)

for(j = 0; j < 3; j++)

printf("%d\t", c[i][j]);

printf("\n");

getch();

INPUT & OUTPUT:

Enter the elements of 3*3 matrix a

1 2 3 4 5 6 7 8 9

Enter the elements of 3*3 matrix b

1 2 3 4 5 6 7 8 9

The resultant 3*3 matrix c is

28 | P a g e
2 4 6

8 10 12

14 16 18

ii Multiplication of two matrices


#include<stdio.h>

#include<conio.h>

void main()

int a[3][3], b[3][3], c[3][3], i, j, k;

clrscr();

printf("Enter the elements of 3*3 matrix a \n");

for(i = 0; i < 3; i++)

for(j = 0; j < 3; j++)

scanf("%d", &a[i][j]);

printf("Enter the elements of 3*3 matrix b \n");

for(i = 0; i < 3; i++)

29 | P a g e
for(j = 0; j < 3; j++)

scanf("%d", &b[i][j]);

for(i = 0; i < 3; i++)

for(j = 0; j < 3; j++)

c[i][j] = 0

for(k = 0; k < 3; k++)

c[i][j] = c[i][j] + (a[i][k] * b[k][j])

printf("The resultant 3*3 matrix c is \n");

for(i = 0; i < 3; i++)

for(j = 0; j < 3; j++)

30 | P a g e
{

printf("%d\t", c[i][j]);

printf("\n");

getch();

INPUT & OUTPUT:

Enter the elements of 3*3 matrix a

1 2 3 4 5 6 7 8 9

Enter the elements of 3*3 matrix b

1 2 3 4 5 6 7 8 9

The resultant 3*3 matrix c is

30 36 42

55 81 96

102 126 150

iii Transpose of a matrix with memory dynamically allocated for the new matrix as row and column
counts may not be same.

#include <stdio.h>

int main()

31 | P a g e
{

int a[10][10], transpose[10][10], r, c, i, j;

printf("Enter rows and columns of matrix: ");

scanf("%d %d", &r, &c);

// Storing elements of the matrix

printf("\nEnter elements of matrix:\n");

for(i=0; i<r; ++i)

for(j=0; j<c; ++j)

printf("Enter element a%d%d: ",i+1, j+1);

scanf("%d", &a[i][j]);

// Displaying the matrix a[][] */

printf("\nEntered Matrix: \n");

for(i=0; i<r; ++i)

for(j=0; j<c; ++j)

printf("%d ", a[i][j]);

if (j == c-1)

printf("\n\n");

// Finding the transpose of matrix a

for(i=0; i<r; ++i)

for(j=0; j<c; ++j)

transpose[j][i] = a[i][j];

32 | P a g e
// Displaying the transpose of matrix a

printf("\nTranspose of Matrix:\n");

for(i=0; i<c; ++i)

for(j=0; j<r; ++j)

printf("%d ",transpose[i][j]);

if(j==r-1)

printf("\n\n");

return 0;

Output

Enter rows and columns of matrix: 2

Enter element of matrix:

Enter element a11: 2

Enter element a12: 3

Enter element a13: 4

Enter element a21: 5

Enter element a22: 6

33 | P a g e
Enter element a23: 4

Entered Matrix:

2 3 4

5 6 4

Transpose of Matrix:

2 5

3 6

4 4

20. Write C programs that use both recursive and non-recursive functions
i To find the factorial of a given integer.
ii To find GCD (greatest common divisor) of two given integers.
iii To find x^n.

i To find the factorial of a given integer.

#include <stdio.h>

34 | P a g e
#include <conio.h>

void main()

int n, a, b;

clrscr();

printf("Enter any number\n");

scanf("%d", &n);

a = recfactorial(n);

printf("The factorial of a given number using recursion is %d \n", a);

b = nonrecfactorial(n);

printf("The factorial of a given number using nonrecursion is %d ", b);

getch();

int recfactorial(int x)

int f;

if(x == 0)

return(1);

else

f = x * recfactorial(x - 1);

return(f);

int nonrecfactorial(int x)

int i, f = 1;

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

35 | P a g e
{

f = f * i;

return(f);

Input & Output:

Enter any number

The factorial of a given number using recursion is 120

The factorial of a given number using nonrecursion is 120

ii To find GCD (greatest common divisor) of two given integers.

#include <stdio.h>

#include <conio.h>

void main()

int a, b, c, d;

clrscr();

printf("Enter two numbers a, b\n");

scanf("%d%d", &a, &b);

c = recgcd(a, b);

printf("The gcd of two numbers using recursion is %d\n", c);

d = nonrecgcd(a, b);

printf("The gcd of two numbers using nonrecursion is %d", d);

getch();

int recgcd(int x, int y)

if(y == 0)

36 | P a g e
{

return(x);

else

return(recgcd(y, x % y));

int nonrecgcd(int x, int y)

int z;

while(x % y != 0)

z = x % y;

x = y;

y = z;

return(y);

Input & Output:

Enter two numbers a, b

3 6

The gcd of two numbers using recursion is 3

The gcd of two numbers using nonrecursion is 3

iii To find x^n.

#include <stdio.h>

37 | P a g e
int power(int n1, int n2);

int main()

int base, powerRaised, result;

printf("Enter base number: ");

scanf("%d",&base);

printf("Enter power number(positive integer): ");

scanf("%d",&powerRaised);

result = power(base, powerRaised);

printf("%d^%d = %d", base, powerRaised, result);

return 0;

int power(int base, int powerRaised)

if (powerRaised != 0)

return (base*power(base, powerRaised-1));

else

return 1;

Output

Enter base number: 3

Enter power number(positive integer): 4

38 | P a g e
3^4 = 81

21. Write a program for reading elements using pointer into array and display the values
using array.

#include <stdio.h>

int main()

int data[5], i;

printf("Enter elements: ");

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

scanf("%d", data + i);

printf("You entered: \n");

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

printf("%d\n", *(data + i));

return 0;

Output

Enter elements: 1

39 | P a g e
4

You entered:

22. Write a program for display values reverse order from array using pointer.

#include
#include
#include
int main()
{
int *ptr,i,n;
clrscr();
printf(“Enter the no of elements:”);
scanf(“%d”,&n);
ptr=(int *)malloc(sizeof(int)*n);
if(ptr==NULL)
{
printf(“Not enough memory”);
exit(1);
}
for(i=0; i<n; i++)
{
printf(“Enter %d element : “,i+1);
scanf(“%d”,&ptr[i]);
}
printf(“Array in original order\n”);
for(i=0; i<n; i++)
{
printf(“%d\n”,ptr[i]);
}

40 | P a g e
printf(“Array in reverse order\n”);
for(i=n-1; i>=0; i–)
{
printf(“%d\n”,ptr[i]);
}
getch();
return 0;
}

Output:
Enter the no of elements:5
Enter 1 element : 12
Enter 2 element : 56
Enter 3 element : 89
Enter 4 element : 45
Enter 5 element : 23
Array in original order
12
56
89
45
23
Array in reverse order
23
45
89
56
12

23. Write a program through pointer variable to sum of n elements from array.

#include<stdio.h>

int main(){

int n, i, sum = 0, elements[10];

int *pointr;

printf("How many elements to add : ");

41 | P a g e
//How many numbers to be added

scanf("%d", &n);

printf("\n Give the elements one by one : ");

for (i = 0; i < n; i++)

scanf("%d", &elements[i]);

// Assigning Starting Address to Pointer Variable

pointr = elements;

for (i = 0; i < n; i++) {

sum = sum + *pointr;

++pointr;

printf("\n\n The sum of array elements : %d", sum);

Sample Output:

How many elements to add : 4

Give the elements one by one : 4 5 6 3

The sum of array elements : 18

24. Write a C program to display the contents of a file to standard output device.

#include<stdio.h>

#include<conio.h>

FILE *fp1,*fp2;

char c;

void main()

42 | P a g e
clrscr();

printf("enter the text\n");

fp1 = fopen("abc.txt", "w");

while((c = getchar()) != EOF)

putc(c, fp1);

fclose(fp1);

fp1 = fopen("abc.txt","r");

fp2=fopen("xyz.txt","w");

while(!feof(fp1))

c = getc(fp1);

putc(c,fp2);

fclose(fp1);

fclose(fp2);

printf("the copied data is \n");

fp2 = fopen("xyz.txt", "r");

while(!feof(fp2))

c = getc(fp2);

printf("%c", c);

getch();

Input & Output:

enter the text

engineering students are very good.

^Z

the copied data is

43 | P a g e
engineering students are very good.

25. Write a C program which copies one file to another, replacing all lowercase characters
with their upper case equivalents.

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp1,*fp2;
char *fn1,*fn2,ch;
clrscr();
printf("\n enter the source file");
gets(fn1);
printf("\n enter the destination file");
gets(fn2);
fp1=fopen(fn1,"r");
fp2=fopen(fn2,"w");
if(fp1==NULL||fp2==NULL)
{
printf("\n unable to open file");
exit(0);
}
while(!feof(fp1))
{
ch=fgetc(fp1);
if(ch>='a'&& ch<='z')
ch=ch-32;
fputc(ch,fp2);
}
printf("\n file successfully copied");
fcloseall();
getch();
}

output
Enter a sentence
wELCOME tO sANFOUNDRY
The given sentence is : wELCOME tO sANFOUNDRY
Case changed sentence is: Welcome To Sanfoundry

26. Write a C program to count the number of times a character occurs in a text file. The

44 | P a g e
file name and the character are supplied as command line arguments.

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

void main()
{
int i=0,j=0,count=0;
char str1[100],str2[20],str3[20];
clrscr();
printf(“Enter the text: “);
gets(str1);

printf(“Enter word to count: “);


gets(str2);
while(str1[i]!=”)
{
while(str1[i]!=’ ‘&&str1[i]!=”) //copying the word from the text to a new string
str3[j++]=str1[i++];
str3[j]=”; //assigning null character at the end of string
j=0;
if((strcmpi(str2,str3))==0) //comparing the given word with the copied word
count++;

if(str1[i]==”)
break;
else
i++;
}

printf(“No. of words are %d”,count);


getch();
}

27. Write a C program that does the following:


It should first create a binary file and store 10 integers, where the file name and 10 values are given in
the command line. (hint: convert the string using atoi function). Now the program asks for an index and
a value from the user and the value at that index should be changed to the new value in the file. (hint: use
fseek function) the program should then read all 10 values and print them back.

#include<stdio.h>

/* Our structure */

45 | P a g e
struct record

int a,b,c;

};

int main()

int count;

FILE *ptr;

struct record myRecord;

ptr=fopen("test.bin","rb");

if (!ptr)

printf("Unable to open file!"); return 1;

for ( count=1; count <= 10; count++)

fread(&myRecord,sizeof(struct record),1,ptr); printf("%d\n",myRecord


.a);

} fclose(ptr);

return 0;

28. Write a C program to merge two files into a third file (i.e., the contents of the first t file followed
by those of the second are put in the third file).

46 | P a g e
#include<stdio.h>

void concatenate(FILE *fp1, FILE *fp2, char *argv[], int argc);

int main(int argc, char *argv[]){

FILE *fp1, *fp2;

concatenate(fp1, fp2, argv, argc);

return 0;

void concatenate(FILE *fp1, FILE *fp2, char **argv, int argc){

int i, ch;

fp2 = fopen("files", "a");

for(i = 1; i < argc - 1; i++){

fp1 = fopen(argv[i], "r");

while((ch = getc(fp1)) != EOF)

putc(ch, fp2);

Input & Output:

File1:

studentboxoffice.in.

File2:

This is Computer Programming Lab.

File 3:

studentboxoffice.in. This is Computer Programming Lab.

29. Write a C program to convert a Roman numeral ranging from I to L to its decimal
equivalent.

47 | P a g e
#include <stdio.h>

#include <conio.h>

#include <string.h>

#include <stdlib.h>

void main()

char rom[30];

int a[30], l, i, k, dec;

clrscr();

printf("Enter the roman number\n");

scanf("%s", &rom);

l =strlen(rom);

for(i = 0; i < l; i++)

switch (rom[i])

case 'I': a[i] = 1;

break;

case 'V': a[i] = 5;

break;

case 'X': a[i] = 10;

break;

case 'L': a[i] = 50;

break;

case 'C': a[i] = 100;

break;

case 'D': dec = dec + 500;

break;

case 'M': a[i] = 1000;

break;

48 | P a g e
default : printf("Invalid choice");

break;

k = a[l - 1];

for(i = l - 1; i > 0; i--)

if(a[i] > a[i - 1])

k = k - a[i - 1];

if(a[i] <= a[i - 1])

k = k + a[i - 1];

printf("decimal equivalent is %d", k);

getch();

Input & Output:

Enter the roman number

XIV

Decimal equivalent is 14

30. Write a C program that converts a number ranging from 1 to 50 to Roman equivalent.

#include<stdio.h>

void predigits(char c1,char c2);


void postdigits(char c,int n);

49 | P a g e
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){

50 | P a g e
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]);

51 | P a g e
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;

Sample output:

Enter any natural number: 87


Roman number will be: LXXXVII

31. Write a C program that uses functions to perform the following operations:
i To insert a sub-string in to a given main string from a given position.

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

52 | P a g e
{

char str1[20], str2[20];

int l1, l2, n, i;

clrscr();

puts("Enter the string 1\n");

gets(str1);

l1 = strlen(str1);

puts("Enter the string 2\n");

gets(str2);

l2 = strlen(str2);

printf("Enter the position where the string is to be inserted\n");

scanf("%d", &n);

for(i = n; i < l1; i++)

str1[i + l2] = str1[i];

for(i = 0; i < l2; i++)

str1[n + i] = str2[i];

str2[l2 + 1] = '\0';

printf("After inserting the string is %s", str1);

getch();

Input & Output:

Enter the string 1

sachin

Enter the string 2

tendulkar

53 | P a g e
Enter the position where the string is to be inserted

After inserting the string is sachtendulkarin

ii To delete n characters from a given position in a given string.

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char str[20];

int i, n, l, pos;

clrscr();

puts("Enter the string\n");

gets(str);

printf("Enter the position where the characters are to be deleted\n");

scanf("%d", &pos);

printf("Enter the number of characters to be deleted\n");

scanf("%d", &n);

l = strlen(str);

for(i = pos + n; i < l; i++)

str[i - n] = str[i];

str[i - n] = '\0';

printf("The string is %s", str);

getch();

54 | P a g e
Input & Output:

Enter the string

sachin

Enter the position where characters are to be deleted

Enter the number of characters to be deleted

The string is sain

32. Write a C program to determine if the given string is a palindrome or not (Spelled
same in both directions with or without a meaning like madam, civic, noon, abcba,
etc.)

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main()

char str[20];

int i, l, f = 0;

clrscr();

printf("Enter any string\n");

gets(str);

l = strlen(str);

for(i = 0; i <= l - 1; i++)

if(str[i] == str[l - 1 - i])

f = f + 1;

if(f == l)

55 | P a g e
{

printf("The string is palindrome");

else

printf("The string is not a palindrome");

getch();

Input & Output:

Enter any string

malayalam

The string is a palindrome

33. Write a C program that displays the position of a character ch in the string S or -
1 if S doesn’t contain ch.

#include<stdio.h>

#include<string.h>

#include<conio.h>

void main()

char s[30], t[20];

char *found;

clrscr();

puts("Enter the first string: ");

gets(s);

puts("Enter the string to be searched: ");

gets(t);

56 | P a g e
found = strstr(s, t);

if(found)

printf("Second String is found in the First String at %d position.\n"


, found - s);

else

printf("-1");

getch();

Input & Output:

1.Enter the first string:

kali

Enter the string to be searched:

li

second string is found in the first string at 2 position

34. Write a C program to count the lines, words and characters in a given text.

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main()

char str[100];

int i = 0, l = 0, f = 1;

clrscr();

57 | P a g e
puts("Enter any string\n");

gets(str);

for(i = 0; str[i] !='\0'; i++)

l = l + 1;

printf("The number of characters in the string are %d\n", l);

for(i = 0; i <= l-1; i++)

if(str[i] == ' ')

f = f + 1;

printf("The number of words in the string are %d", f);

getch();

Input & Output:

Enter any string

abc def ghi jkl mno pqr stu vwx yz

The number of characters in the string are 34

The number of words in the string are 9

35. Write a menu driven C program that allows a user to enter n numbers and then
choose between finding the smallest, largest, sum, or average. The menu and all
the choices are to be functions. Use a switch statement to determine what action
to take. Display an error message if an invalid choice is entered.

#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()

58 | P a g e
{
int optn,a,b;
float c;
printf("\n1.Add: ");
printf("\n2.Subtract: ");
printf("\n3.Multiply: ");
printf("\n4.Divide: ");
printf("\n5.Exit: ");
printf("\nWhat u want to do?: ");
scanf("%d",&optn);
switch(optn)
do
{
case 1: printf("\nEnter Numbers to Add: ");
scanf("%d %d",&a,&b);
c=a+b;
printf("Addition's Result Is= %f",c);
break;

case 2: printf("\nEnter Numbers to Subtract: ");


scanf("%d %d",&a,&b);
c=a-b;
printf("Subtraction's Result Is= %f",c);
break;

case 3: printf("\nEnter Numbers to Multiply: ");


scanf("%d %d",&a,&b);
c=a*b;
printf("Multiplication's Result Is= %f",c);
break;

case 4: printf("\nEnter Numbers to Divide: ");


scanf("%d %d",&a,&b);
c=a/b;
printf("Dividion's Result Is= %f",c);
break;

case 5: exit(0);
default :printf("\n Invalid option...");

}while(optn!=0);
getch();
}

59 | P a g e
36. Write a C program to construct a pyramid of numbers as follows:
1 * 1 1
12 ** 23 22
123 *** 456 333
4444

#include <stdio.h>

int main()

int i, j, rows;

printf("Enter number of rows: ");

scanf("%d",&rows);

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

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

printf("%d ",j);

printf("\n");

return 0;

Output:

1 2

1 2 3

1 2 3 4

60 | P a g e
1 2 3 4 5

#include <stdio.h>

int main()

int i, j, rows;

printf("Enter number of rows: ");

scanf("%d",&rows);

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

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

printf("* ");

printf("\n");

return 0;

Output:

* *

* * *

* * * *

61 | P a g e
* * * * *

#include <stdio.h>

int main()

int rows, i, j, number= 1;

printf("Enter number of rows: ");

scanf("%d",&rows);

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

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

printf("%d ", number);

++number;

printf("\n");

return 0;

Output:

2 3

4 5 6

62 | P a g e
7 8 9 10

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n;
clrscr();
printf("Enter pyramid range:");
scanf("%d",&n);

for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
printf(" ");

for(j=0;j<i;j++)
printf("%d ",i);
printf("\n");
}
getch();
}

Output:
Enter pyramid range:6

1
2 2
3 3 3

37. Write a C program that uses non recursive function to search for Key value in
a given list of integers using linear search method.

#include<stdio.h>

#include<conio.h>

void main()

int i, a[20], n, key, flag = 0;

63 | P a g e
clrscr();

printf(“Enter the size of an array \n”);

scanf(“%d”, &n);

printf(“Enter the array elements”);

for(i = 0; i < n; i++)

scanf(“%d”, &a[i]);

printf(“Enter the key elements”);

scanf(“%d”, &key);

for(i = 0; i < n; i++)

if(a[i] == key)

flag = 1;

break;

if(flag == 1)

printf(“The key elements is found at location %d”, i + 1);

else

printf(“The key element is not found in the array”);

getch();

Input & Output:

Enter the size of an array 6

Enter the array elements 50 10 5 200 20 1

Enter the key element 1

The key Element is found at location 6

64 | P a g e
38. Write a C program that uses non recursive function to search for a Key value in a
given sorted list of integers using binary search method.

#include<stdio.h>

#include<conio.h>

void main()

int a[20], i, n, key, low, high, mid;

clrscr();

printf(“Enter the array elements in ascending order”);

for(i = 0; i < n; i++)

scanf(“%d”, &a[i]);

printf(“Enter the key element\n”);

scanf(“%d”, &key);

low = 0;

high = n - 1;

while(high >= low)

mid = (low + high) / 2;

if(key == a[mid])

break;

else

if(key > a[mid])

low = mid + 1;

else

65 | P a g e
high = mid - 1;

if(key == a[mid])

printf(“The key element is found at location %d”, mid + 1);

else

printf(“the key element is not found”);

getch();

Input & Output:

Enter the size of the array 7

Enter the array elements in ascending order 23 45 68 90 100 789 890

Enter the key element 789

The key Element is found at location 6

39. Write a C program that implements the Bubble sort method to sort a given list of
integers in ascending order.

#include<stdio.h>

#include<conio.h>

void main()

int n, a[20], temp, i, j;

clrscr();

printf("Enter the size of the array\n");

scanf("%d", &n);

printf("Enter the array elements\n");

for(i = 0; i < n; i++)

66 | P a g e
{

scanf("%d", &a[i]);

for(i = 0; i < n - 1; i++)

for(j = 0; j < n - 1; j++)

if(a[j] > a[j + 1])

temp = a[j];

a[j] = a[j + 1];

a[j + 1] = temp;

printf("The sorted array is\n");

for(i = 0; i < n; i++)

printf("%d\n", a[i]);

getch();

Input & Output:

Enter the size of the array: 5

Enter the array elements: 50 40 30 20 10

The sorted array is: 10 20 30 40 50

40. Write a C program that sorts the given array of integers using selection sort in
descending order.

#include<stdio.h>

67 | P a g e
#include<conio.h>

void main()

int n, a[20], min, temp, i, j;

clrscr();

printf("Enter the size of the array\n");

scanf("%d", &n);

printf("Enter the array elements\n");

for(i = 0; i < n; i++)

scanf("%d", &a[i]);

for(i = 0; i < n - 1; i++)

min = i;

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

if(a[j] < a[min])

min = j;

temp = a[i];

a[i] = a[min];

a[min] = temp;

printf("The sorted array is\n");

for(i = 0; i < n; i++)

printf("%d\n", a[i]);

getch();

Input & Output:

68 | P a g e
Enter the size of the array: 7

Enter the array elements: 7 6 5 4 3 2 1

The Sorted array is: 1 2 3 4 5 6 7

41. Write a C program that sorts the given array of integers using insertion sort in
ascending order.

#include<stdio.h>

int main() {
int i, j, num, temp, arr[20];

printf("Enter total elements: ");


scanf("%d", &num);

printf("Enter %d elements: ", num);


for (i = 0; i < num; i++) {
scanf("%d", &arr[i]);
}

for (i = 1; i < num; i++) {


temp = arr[i];
j = i - 1;
while ((temp < arr[j]) && (j >= 0)) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}

printf("After Sorting: ");


for (i = 0; i < num; i++) {
printf("%d", arr[i]);
}

return 0;
}

69 | P a g e
Output:
Enter total elements: 5
Enter 5 elements: 9 4 1 0 2
After sorting: 0 1 2 4 9

42. Write a C program that sorts a given array of names.

#include <stdio.h>
#include <string.h>
void main()
{

char name[10][8], tname[10][8], temp[8];


int i, j, n;

printf("Enter the value of n \n");


scanf("%d", &n);
printf("Enter %d names n", \n);

for (i = 0; i < n; i++)


{
scanf("%s", name[i]);
strcpy(tname[i], name[i]);
}

for (i = 0; i < n - 1 ; i++)


{

70 | P a g e
for (j = i + 1; j < n; j++)
{
if (strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}

printf("\n----------------------------------------\n");
printf("Input NamestSorted names\n");
printf("------------------------------------------\n");

for (i = 0; i < n; i++)


{
printf("%s\t\t%s\n", tname[i], name[i]);
}

printf("------------------------------------------\n");

Output:

Enter the value of n


7
Enter 7 names
heap
stack
queue
object
class
program
project

----------------------------------------
Input Names Sorted names
------------------------------------------
heap class
stack heap
queue object

71 | P a g e
object program
class project
program queue
project stack
------------------------------------------

72 | P a g e

You might also like