[go: up one dir, main page]

0% found this document useful (0 votes)
12 views50 pages

PPS 1

The document contains multiple C programming examples that demonstrate various functionalities, including arithmetic operations, data type conversions, finding maximum and minimum values, calculating simple and compound interest, and generating Fibonacci sequences. Each program is designed to read input from the user and output the results based on the specified operations. The examples also cover concepts like prime number checking, binary conversion, and quadratic equation roots.

Uploaded by

Harsha Karthik
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)
12 views50 pages

PPS 1

The document contains multiple C programming examples that demonstrate various functionalities, including arithmetic operations, data type conversions, finding maximum and minimum values, calculating simple and compound interest, and generating Fibonacci sequences. Each program is designed to read input from the user and output the results based on the specified operations. The examples also cover concepts like prime number checking, binary conversion, and quadratic equation roots.

Uploaded by

Harsha Karthik
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/ 50

Page |1

Title: 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 that prints the results of all the operators available in C.
Reading required operand values from standard input.

#include <stdio.h>
int main()
{
int num1, num2;
printf(“Enter two numbers:\n”);
scanf(“%d%d”, &num1, &num2);
printf(“Sum : %d\n”, num1 + num2);
printf(“Difference : %d\n”, num1 – num2);
printf(“Product : %d\n”, num1 * num2);
printf(“Quotient : %d\n”, num1 / num2);
printf(“Remainder : %d\n”, num1 % num2);
if(num1 > num2)
printf(“%d is greater than %d\n”, num1, num2);
else
printf(“%d is less than %d\n”, num1, num2);
if(a == b)
printf(“%d is equal to %d\n”, num1, num2);
else
printf(“%d is not equal to %d\n”, num1, num2);
printf(“Logical AND : %d\n”, num1 && num2);
printf(“Logical OR : %d\n”, num1 || num2);
printf(“Bitwise AND : %d\n” num1 & num2);
printf(“Bitwise OR : %d\n”, num1 | num2);
printf(“Bitwise XOR : %d\n”, num1 ^ num2);
printf(“Post increment of number 1 is %d\n”, num1++);
printf(“Pre decrement of number 2 is %d\”, --num2);
return 0;
}
Page |2

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

//Program that converts one given data type to another using auto conversion
and casting. Taking the values from standard input.

#include <stdio.h>

int main()

int a, b;

printf("Enter two values:\n");

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

float c = a/b;

float d = float(a/b);

printf("%f%f\n", c, d);

return 0;

}
Page |3

Title: Write a program to find the max and min from three numbers.

//Program to find the max and min from the three numbers.

#include <stdio.h>
int main()
{
int num1;
int num2;
int num3;
printf("Enter Number 1:");
scanf("%d", &num1);
printf("Enter Number 2:");
scanf("%d", &num2);
printf("Enter Number 3:");
scanf("%d", &num3);
//maximum
if(num1 > num2)
{
if(num1 > num3)
printf("%d is greater than %d and %d\n", num1, num2, num3);
else
printf("%d is greater than %d and %d\n", num3, num1, num1);
}
else
{
if(num2 > num3)
printf("%d is greater than %d and %d\n", num2, num1, num3);
else
printf("%d is greater than %d and %d\n", num3, num1, num2);
}
//minimum
if(num1 < num2)
{
if(num1 < num3)
printf("%d is smaller than %d and %d\n", num1, num2, num3);
else
printf("%d is smaller than %d an d%d\n", num3, num1, num2);
}
else
{
if(num2 < num3)
printf("%d is smaller than %d and %d\n", num2, num1, num3);
else
printf("%d is smaller than %d and %d\n", num3, num1, num2);
}
return 0;
}
Page |4

Title: Write a program for simple and compound interest.

//Program to calculate for simple and compound interest.

#include <stdio.h>
#inlcude <math.h>
int main()
{
float p, t, r, si, ci;
printf("Enter Principle, Rate, and Time\n");
scanf("%f%f%f", &p, &r, &t);
si = p * r * t / 100;
ci = p * (pow(1 + r / 100, t) - 1);
printf("Simple interest = %f\n", si);
printf("Compound interest = %f\n", ci);
return 0;
}
Page |5

Title: Write a 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.

//Program that declares Class awarded for a given percentage of marks.


#include <stdio.h>
int main()
{
int perc;
printf("Enter marks out of 100:\n");
scanf("%d", &perc);
if(perc >= 70)
printf("Distinction\n");
else if(perc > 60 && perc < 70)
printf("First Class\n");
else if(perc > 40 && perc < 60)
printf("Second Class\n");
else
printf("Fail\n");
return 0;
}
Page |6

Title: Write a program that prints a multiplication table for a given number
and the number of rows in the table.

//Program to print a multiplication tale for a given number and the number of
rows in the table.

#include <stdio.h>
int main()
{
int i , n, r;
printf("Multiplication table of:\n");
scanf("%d", &n);
printf("Multiplication table til:\n");
scanf("%d", &r);
for(i = 0; i <= r; i++)
{
printf("%d x %d = %d\n", n, r, n * i);
}
return 0;
}
Page |7

Title: Write a program that shows the binary equivalent of a given positive
number between 0 to 255.

//Program to print the binary equivalents of a given positive number between 0


to 255.

#include <stdio.h>
int main()
{
int num, bin=0, place = 1, rem, temp;
printf("Enter Number between 0 and 255:\n");
scanf("%d", &num);
temp = num;
if(num > 0 && num < 255)
{
while(num > 0)
{
rem = num % 2;
bin = bin + rem * place;
place = place * 10;
num = num /2;
}
printf("The binary equivalent of %d is %d\n",temp, bin);
}
else
{
printf("Please enter valid number\n");
}
return 0;
}
Page |8

Title: A building has 10 floors with a floor height 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.

//Program to find the time taken by the ball to reach each floor.

#include <stdio.h>
#include <math.h>
int main()
{
int u = 0, a = 9.8, s = 3, t = 0, i;
for(i = 1; i <= 10; i++, s = s+3)
{
t = u + sqrt(u * u + 2 * a * s);
printf("Time taken is %d\n", t);
printf("Distance travelled is %d\n", s);
printf("%d\n", i);
}
return 0;
}
Page |9

Title: Write a C program, which takes two integer operands and one operator
from the user, performs the operation and then prints the result.

//Program that takes two integer operands and one operator from the user to
perform operation and then print the result.

#include <stdio.h>
int main()
{
int n1, n2, operator;
printf("Enter two numbers:\n");
scanf("%d%d", &n1, &n2);
printf("1 -- Addition\n 2 --- Subtraction\n 3 -- Multiplication\n 4 --
Quotient\n 5 -- Remainder\n");
printf("Enter Operator\n");
scanf("%d", &operator);
switch(operator)
{
case 1 : printf("The sum of %d and %d is %d\n", n1, n2, n1 + n2);
break;
case 2 : printf("The difference of %d and %d is %d\n", n1, n2, n1 - n2);
break;
case 3 : printf("The product of %d and %d is %d\n", n1, n2, n1 * n2);
break;
case 4 : printf("The quotient of %d and %d is %d\n", n1, n2, n1 / n2);
break;
case 5 : printf("The modulus of %d and %d is %d\n", n1, n2, n1 % n2);
break;
default : printf("Enter valid operator\n");
}
return 0;
}
P a g e | 10

Title: Write a program that finds if a given number, is a prime number.

//Program to find if given number is prime.


#include <stdio.h>
int main()
{
int n, i = 1, c = 0;
printf("Enter Number:\n");
scanf("%d", &n);
if(i <= n)
{
if(n % i == 0)
{
c = c + 1;
}
i++;
}
if(c == 2)
{
printf("%d is prime\n", n);
}
else
{
printf("%d is not a prime\n", n);
}
return 0;
}
P a g e | 11

Title: Write a C program to find the sum of individual digits of a positive


integer and test given number is palindrome.

//program to find the sum of individual digits of a positive integer and test
if number is palindrome.
#include <stdio.h>
int main()
{
int num, rem, temp, sum = 0, rev=0;
printf("Enter number:\n");
scanf("%d", &num);
temp = num;
while(num > 0)
{
rem = num % 10;
sum = sum + rem;
rev = rev*10+rem;
num = num / 10;
}
printf("The sum of individual digits of the given number is %d\n" ,sum);

if(temp == rev)
printf("The given number is a palindrome\n");
else
printf("The given number is not a palindrome\n");
return 0;
}
P a g e | 12

Title: 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.

//Program to generate the first n terms of the Fibonacci sequence.


#include <stdio.h>
int main()
{
int num, n1, n2, n3, i;
printf("Enter number of terms of Fibonacci Sequence:");
scanf("%d", &num);
n1 = 0;
n2 = 1;
printf("%d\t%d\t", n1, n2);
for(i = 2; i <= num; i++)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d \t", n3);
}
return 0;
}
P a g e | 13

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

//Program to generate all the prime numbers between 1 and n.

#include <stdio.h>
int main()
{
int i, j, n, m;
printf("Enter Maximum value:\n");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
m = 0;
for(j = 1; j <= n; j++)
if(i%j == 0)
{
m++;
}
if(m == 2)
{
printf("%d\t", i);
}
printf("\t");
}
return 0;
}
P a g e | 14

Title: Write a C program to find the roots of a Quadratic equation.

//Program to find the roots of a quadratic equation.


#include <stdio.h>
#include <math.h>
int main()
{
int a, b, c, disc;
float rt1, rt2;
printf("Enter values of a b and c\n");
scanf("%d%d%d", &a, &b, &c);
disc = b * b - 4 * a * c;
if(disc > 0)
{
printf("Roots are real and distinct\n");
rt1 = (-b + sqrt(disc))/(2 * a);
rt2 - (-1 - sqrt(disc))/(2 * a);
printf("The roots of the quadratic equation are %f and %d\n", rt1,
rt2);
}
else if(disc == 0)
{
printf("Roots are real and equal\n");
rt1 = b / (2 * a);
rt2 = -b / (2 * a);
printf("The roots of quadratic equation are %f and %d\n", rt1, rt2);
}
else
{
printf("Roots are imaginary\n");
}
return 0;
}
P a g e | 15

Title: Write a C program to calculate the following, where x is a fractional


𝒙 𝒙𝟐 𝒙𝟑
value 𝟏 − 𝟐 + 𝟒
− 𝟔

𝒙 𝒙𝟐 𝒙𝟑
//Program to calculate 𝟏 − 𝟐 + 𝟒
− 𝟔
.

#include <stdio.h>
#include <math.h>
int main()
{
int i, j;
float x, sum = 1;
printf("Enter Values of x:\n");
scanf("%f", &x);
for(i = 1, j = 2; i <= 3; i++, j + 2)
{
sum = sum + pow(x, i)/j;
}
printf("%f", sum);
return 0;
}
P a g e | 16

Title: Write a C program to read numbers, x and n, and then compete the sum of
this geometric progression 𝟏 + 𝒙 + 𝒙𝟐 + 𝒙𝟑 + ⋯ + 𝒙𝒏.

//Program to read two numbers x and n and to find the solution for 𝟏+𝒙+
𝒙𝟐 + 𝒙𝟑 + ⋯ + 𝒙𝒏 .

#include <stdio.h>
#include <math.h>
int main()
{
int x, n, i, sum = 1;
printf("enter value of x and n:\n");
scanf("%d%d", &x, &n);
for(i = 2; i <= n; i++)
{
sum = sum + pow(x,i);
}
printf("%d", sum);
return 0;
}
P a g e | 17

Title: Write a C program to find the minimum, maximum and average in an array
of integers.
//Program to find the minimum and maximum and average in an array of integers.
#include<stdio.h>
int main()
{
int array[20], i, size, max, min, sum = 0, avg;
printf("Enter the size of the array:\n");
scanf("%d", &size);
printf("Enter elements of the array:\n");
for(i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
max = min = array[0];
for(i = 1; i < size; i++)
{
if(max < array[i])
{
max = array[i];
}
else if(min > array[i])
{
min = array[i];
}
}
printf("The maximum element of the array is %d and minimum element of the
array is %d\n", max, min);
for(i = 0; i < size; i++)
{
sum = sum + array[i];
avg = sum/size;
}
printf("The average of the elements in the array is %d\n", avg);
return 0;
}
P a g e | 18

Title: Write a function to compute mean, variance, standard deviation, sorting


of n elements in single dimension array.
//Program to compute mean, variance, standard deviation, sorting n elements in
single dimensional array.

#include <stdio.h>
#include <math.h>
int main()
{
int array[20], s1 = 0, s2 = 0, size, i, j, temp = 0;
float v, sd, m;
printf("Enter size of array\n");
scanf("%d", &size);
printf("Enter elements:\n");
for(i = 0; i < size; i++)
{
scanf("%d", &array[i]);
s1 = s1 + array[i];
}
m = s1/ size;
printf("The mean value of given elements is %f\n", m);

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


{
s2 = s2 + pow((array[i] - m),2);
}
v = s2 / size;
sd = sqrt(v);
printf("The variance is %f and standard deviation is %d\n", v, sd);
for(i = 0; i < size; i++)
{
for(j = i + 1; j < size; j++)
{
if(array[i] = array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
for(i = 0; i < size; i++)
{
printf("%d", array[i]);
}
return 0;
}
P a g e | 19

Title: Write a C program that uses functions to perform the addition of


matrices.
//Program that uses functions to add two matrices.

#include <stdio.h>
void sumofmatrices();
int main()
{
sumofmatrices();
}

void sumofmatrices()
{
int i, j, r1, c1, r2, c2, mat1[10][10], mat2[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix 1:\n");
scanf("%d%d", &r1, &c1);
printf("Enter elements of matrix 1\n");
for(i = 0; i < r1; i++)
{
for(j = 0; j < c1; j++)
{
scanf("%d",&mat1[i][j]);
printf("%d\t", mat1[i][j]);
}
printf("\n");
}
printf("Enter the number of rows and columns of matrix 2:\n");
scanf("%d%d", &r2, &c2);
printf("Enter elements into matrix 2:\n");
for(i = 0; i < r2; i++)
{
for(j = 0; j < c2; j++)
{
scanf("%d", &mat2[i][j]);
printf("%d\t", mat2[i][j]);
}
printf("\n");
}
printf("The sum of Matrix 1 and Matrix 2 is:\n");
if(r1 == r2 && c1 == c2)
{
for(i = 0; i < r1; i++)
{
for(j = 0; j < c1; j++)
{
sum[i][j] = mat1[i][j] + mat2[i][j];
printf("%d\t", sum[i][j]);
}
printf("\n");
}
}
}
P a g e | 20

Title: Write a C program that uses functions to perform multiplication of


matrices
//Program that uses functions to multiply two matrices.
#include <stdio.h>
void productofmatrices();
int main()
{
productofmatrices();
}

void productofmatrices()
{
int i, j, k, r1, r2, c1, c2, mat1[10][10], mat2[10][10], m[10][10];
printf("Enter order of matrix 1:\n");
scanf("%d%d", &r1, &c1);
printf("Enter order of matrix 2:\n");
scanf("%d%d", &r2, &c2);

printf("Enter elements of matrix 1:\n");


for(i = 0; i < r1; i++)
{
for(j = 0; j < c1; j++)
{
scanf("%d", &mat1[i][j]);
printf("%d\t", mat1[i][j]);
}
printf("\n");
}

printf("Enter elements of matrix 2:\n");


for(i = 0; i < r2; i++)
{
for(j = 0; j < c2; j++)
{
scanf("%d", &mat2[i][j]);
printf("%d\t", mat2[i][j]);
}
printf("\n");
}

//mat1 * mat2
printf("The product of Matrix 1 and matrix 2 is:\n");
if(c1 == r2)
{
for(i = 0; i < r1; i++)
{
for(j = 0; j < c1; j++)
{
m[i][j] = 0;
for(k = 0; k < c1; k++)
{
m[i][j] = m[i][j] + mat1[i][k] * mat2[k][j];
}
printf("%d\t", m[i][j]);
}
printf("\n");
}
}
P a g e | 21

Title: Write a C program that uses both recursive and Non-recursive functions
to find the factorial of a given number.

//Program that uses recursive and non-recursive functions to find the


factorial of a given number.
//With recursion
#include <stdio.h>
int fact(int num);
int main()
{
int num, result;
printf("Enter a number:\n");
scanf("%d", &num);
result = fact(num);
printf("The factorial of %d is %d\n", num, result);
return 0;
}
int fact(int num)
{
if(num == 1)
return 1;
else
return num * fact(num - 1);
}
P a g e | 22

//Without Recursion
#include <stdio.h>
int fact(int num);
int main()
{
int num, result;
printf("Enter Number:\n");
scanf("%d", &num);
result = fact(num);
printf("The factorial value of %d is %d\n", num, result);
return 0;
}

int fact(int num)


{
int fact = 1, i = 1;
while( i <= num)
{
fact = fact * i;
i++;
}
return fact;
}
P a g e | 23

Title: Write a C program that uses both recursive and non-recursive functions
to find the GCD of two given integers.
//Program that uses recursive and non-recursive functions to find the GCD of
two given numbers.
//With recursion
#include <stdio.h>
int gcd(int x, int y);
int main()
{
int x, y;
printf("Enter two numbers:\n");
scanf("%d%d", &x, &y);
int result = gcd(x, y);
int temp1 = x, temp2 = y;
printf("The greatest common divisor of %d and %d is %d\n", temp1,
temp2, result);
}

int gcd(int x, int y)


{
if(x % y == 0)
return y;
else
return gcd(y, x % y);
}
P a g e | 24

//Without recursion
#include <stdio.h>
int gcd(int num1, int num2);
int main()
{
int num1, num2, result;
printf("Enter two numbers:\n");
scanf("%d%d", &num1, &num2);
result = gcd(num1, num2);
printf("The greatest common divisor is %d\n", result);
}

int gcd(int num1, int num2)


{
while((num1 % num2) != 0)
{
int temp = num1 % num2;
num1 = num2;
num2 = temp;
}
return num2;
}
P a g e | 25

Title: Write a C program that uses both recursive and non-recursive functions
to find xn
//Program that uses recursive and non-recursive functions to find 𝒙𝒏
//without recursion
#include <stdio.h>
int power(int num, int n);
int main()
{
int num, result, n;
printf("Enter a number:\n");
scanf("%d", &num);
printf("Enter power:\n");
scanf("%d", &n);
result = power(num, n);
printf("%d\n", result);
}

int power(int num, int n)


{
int i = 0, result = 1;
while(i < n)
{
result = result * num;
i++;
}
return result;
}
P a g e | 26

//With recursion

#include <stdio.h>
int power(int num, int n);
int main()
{
int num, result, n;
printf("Enter a number:\n");
scanf("%d", &num);
printf("Enter power:\n");
scanf("%d", &n);
result = power(num, n);
printf("%d\n", result);
}

int power(int num, int n)


{
if(n == 0)
return 1;
else if(n == 1)
return num;
else if (n % 2 == 0)
return power(num * num, n/2);
else
return num*power(num*num, (n-1)/2);
}
P a g e | 27

Title: Write a C program to read elements using pointers into an array and
display the values of the array.
//Program to read elements using pointer into array and display the values
using array.
#include <stdio.h>
int main()
{
int array[20];
int size, i;
int *ptr = array;
printf("Enter size of array:\n");
scanf("%d", &size);
printf("Enter elements intro an array:\n");
for(i = 0; i < size; i++)
{
scanf("%d", ptr);
ptr++;
}
ptr = array;
printf("Array Elements:");
for(i = 0; i < size; i++)
{
printf("%d", *ptr);
ptr++;
}
return 0;
}
P a g e | 28

Title: Write a C program to display the values of an array in reverse order


using pointers.
//Program to display the values in the reverse order from an array using
pointers.
#include <stdio.h>
int main()
{
int i, size, array[20], *ptr = array;
printf("Enter size of array:\n");
scanf("%d", & size);
printf("Enter elements:\n");
for(i = 0; i < size; i++)
{
scanf("%d", ptr+i);
}
for(i = size - 1; i >= 0; i--)
{
printf("%d\t", array[i]);
}
return 0;
}
P a g e | 29

Title: Writer a C program through pointer variable to sum of n elements from


array.
//Program to print the sum of n elements from an array using pointer variable.
#include <stdio.h>
int main()
{
int i, size, array[20], *ptr = array, sum = 0;
printf("Enter size of array:\n");
scanf("%d", & size);
printf("Enter elements:\n");
for(i = 0; i < size; i++)
{
scanf("%d", ptr+i);
sum = sum + array[i];
}
printf("The Sum of elements is %d\n", sum);

return 0;
}
P a g e | 30

Title: Write a C program that uses functions to insert a sub-string into a


given main string from a given position.
//Program that uses functions to insert a sub-string into a given main string
from a given position.
#include <stdio.h>
#include <string.h>
int main()
{
char m[20], s[20], d[20];
int p;
printf("Enter string:\n");
scanf("%s", m);
printf("Enter Substring:\n");
scanf("%s", s);
printf("Enter the position at which substring needs to be inserted.\n");
scanf("%d", &p);
strncpy(d, m, p);
strcat(d, s);
strcat(d, m+p);
printf("%s\n", d);
}
P a g e | 31

Title: Write a C program that uses functions to delete n characters from a


given position in a given string.
//Program that uses functions to delete n characters from a given position in
a given string.
#include <stdio.h>
#include <string.h>

void delchar(char *x,int a, int b);

void main()
{
char string[10];
int n,pos,p;
printf("Enter string:\n");
gets(string);
printf("Enter the position from where to delete:\n");
scanf("%d",&pos);
printf("Enter the number of characters to be deleted:\n");
scanf("%d",&n);
delchar(string, n,pos);
}

void delchar(char *x,int a, int b)


{
if ((a+b-1) <= strlen(x))
{
strcpy(&x[b-1],&x[a+b-1]);
printf("%s", x);
}
}
P a g e | 32

Tile: Write a C program to determine if the given string is a palindrome or


not.
//Program to determine if the given string is a palindrome or not.
#include<stdio.h>
#include<string.h>

int main()
{
int i,length;
int temp = 0;
char str[100];
printf("Enter String:\n");
scanf("%s",str);
length = strlen(str);
for(i = 0; i < length; i++)
{
if(str[i] != str[length - i -1])
{
temp = 1;
break;
}
}
if(temp == 0)
{
printf("%s is a palindrome.\n", str);
}
else
{
printf("%s is not a palindrome.\n", str);
}
return 0;
}
P a g e | 33

Title: Write a C program that displays the position of a character ch in the


string S or -1 if S does not contain ch.
//C program to display the position of a character in string.
#include<stdio.h>
#include<string.h>

int main()
{
char string[20], ch;
int length, f = 0, i;
printf("Enter a String:\n");
scanf("%s",string);
length = strlen(string);
printf("Enter character to be searched for in string:\n");
scanf("%c", &ch);

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


{
if(string[i] == ch)
{
printf("%c is found at %d position\n", ch, i+1);
f = 1;
}
}
if(f == 0)
{
printf("%c is not found is %s.\n", ch, string);
}
return 0;
}
P a g e | 34

Title: Write a C program to count the lines, words, and characters in a given
text.
//C program to count the length of a given string.
#include <stdio.h>
int main()
{
char name[20];
int i, length = 0;
printf("Enter string:\n");
scanf("%s", name);
for(i = 0; name[i] != '\0'; i++)
{
length++;
}
printf("Length of %s is %d\n", name, length);
return 0;
}
P a g e | 35

Title: Write a C program to construct a pyramid of numbers as follows.


//Program to construct pyramid of numbers.
1
1 2
1 2 3

#include <stdio.h>
int main()
{
int n, j, i;
printf("Enter Number or rows:\n");
scanf("%d", &n);
for(i = 1; i <= n; ++i)
{
for(j = 1; j <= i; ++j)
{
printf("%d\t", j);
}
printf("\n");
}
return 0;
}
P a g e | 36

*
* *
* * *

#include <stdio.h>
int main()
{
int n, j, i;
printf("Enter Number or rows:\n");
scanf("%d", &n);
for(i = 1; i <= n; ++i)
{
for(j = 1; j <= i; ++j)
{
printf("*\t");
}
printf("\n");
}
return 0;
}
P a g e | 37

1
2 3
4 5 6

#include<stdio.h>
int main()
{
int i, j, n, num=1;
printf("Enter number of rows:\n");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
{
printf("%d\t", num);
num++;
}
printf("\n");
}
return 0;
}
P a g e | 38

1
2 2
3 3 3
4 4 4 4

#include<stdio.h>
int main()
{
int i, j, n;
printf("Enter number of rows:\n");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
{
printf("%d\t", i);
}
printf("\n");
}
return 0;
}
P a g e | 39

*
* *
* * *
* *
*
P a g e | 40

Title: Write a C program that uses non-recursive functions to search for a key
value in a given list of integers using linear search method.
//C program to search for a key value in a given list of integers using linear
search method using non-recursive.
#include <stdio.h>
int linearsearch(int array[20], int key, int size);
int main()
{
int array[20], i, key, size;
printf("Enter size of array:\n");
scanf("%d", &size);
printf("Enter list of elements:\n");
for(i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
printf("Enter element to search for:\n");
scanf("%d", &key);
int position = linearsearch(array, key, size);
if(position >= 0)
printf("The element %d is found at position %d", key, position);
else
printf("The element %d is not found in the list", key);
}
int linearsearch(int array[20], int key, int size)
{
int i;
for(i = 0; i < size; i++)
{
if(array[i] == key)
return i + 1;
}
return -1;
}
P a g e | 41

Title: Write a C program that uses non-recursive functions to search for a key
value in a given sorted list of integers using binary search method.
//C program that uses non-recursive functions to search for a key value in a
given sorted list using binary search.
#include <stdio.h>
int binarysearch(int array[20], int key, int low, int high);
int main()
{
int array[20], i, key, size, high, low;
printf("Enter size of array:\n");
scanf("%d", &size);
high = size - 1;
printf("Enter list of elements:\n");
for(i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
printf("Enter element to search for:\n");
scanf("%d", &key);
int position = binarysearch(array, key, low, high);
if(position >= 0)
printf("The element %d is found at position %d", key, position);
else
printf("The element %d is not found in the list", key);
}
int binarysearch(int array[20], int key, int low, int high)
{
int mid;
while(low <= high)
{
mid = (low + high)/2;
if(array[mid] == key)
return mid + 1;
else if(array[mid] > key)
high = mid - 1;
P a g e | 42

else
low = mid + 1;
}
return -1;
}
P a g e | 43

Title: Write a C program that implements the bubble sort method to sort a
given list of integers in ascending order.
//C program that implements bubble sort method.
#include <stdio.h>
void bubblesort(int array[20], int size);
int main()
{
int array[20];
int i, size;
printf("Enter size of array:\n");
scanf("%d", &size);
printf("Enter elements:\n");
for(i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
bubblesort(array, size);
}
void bubblesort(int array[20], int size)
{
int i, j, temp, k;
for(i = 0; i < size; i++)
{
for(j = 1; j < (size - i - 1); j++)
{
if(array[j-1] > array[j])
{
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
}
}
}
for(k = 0; k < size; k++)
{
P a g e | 44

printf("%d\t", array[k]);
}
}
P a g e | 45

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

#include <stdio.h>
void selectionsort(int array[20], int size);
int main()
{
int array[20];
int i, size;
printf("Enter size of array:\n");
scanf("%d", &size);
printf("Enter elements:\n");
for(i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
selectionsort(array, size);
}

void selectionsort(int array[20], int size)


{
int max, i, j , k , temp;
for(i = 0; i < size; i++)
{
max = i;
for(j = i+1; j < size - 1; j++)
{
if(array[j] > array[max])
{
max = j;
}
}
temp = array[i];
array[i] = array[max];
P a g e | 46

array[max] = temp;
}
for(k = 0; k < size; k++)
{
printf("%d\t", array[k]);
}

}
P a g e | 47

Title: Write a C program that sorts the given array of integers using
insertion sort in ascending order.
//C program to sort given array of integers using insertion sort in ascending
order.

#include <stdio.h>
void insertionsort(int array[20], int size);
int main()
{
int array[20];
int i, size;
printf("Enter size of array:\n");
scanf("%d", &size);
printf("Enter elements:\n");
for(i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
insertionsort(array, size);
return 0;
}

void insertionsort(int array[20], int size)


{
int i, j, temp, k;
for(i = 1; i < size; i++)
{
temp = array[i];
for(j = i; j > 0 && temp < array[j-1]; j--)
{
array[j] = array[j-1];
}
array[j] = temp;
}
for(k = 0; k < size; k++)
{
P a g e | 48

printf("%d\t", array[k]);
}
}
P a g e | 49

Title: Write a C program that sorts a given array of names.


//C program to sort a given array of names.

#include <stdio.h>
#include <string.h>
int main()
{
char name[20][20], temp[20];
int i, j, a, b, size;
printf("Enter the size of array:\n");
scanf("%d", &size);
printf("Enter elements:\n");
for(i = 0; i < size; i++)
{
scanf("%s", name[i]);
}
for(i = 0; i < size; i++)
{
for(j = i + 1; j < size; j++)
{
if(strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
printf("The sorted order of the elements is:\n");
for(i = 0; i < size; i++)
{
printf("%s\t", name[i]);
}
return 0;
}
P a g e | 50

You might also like