PPS 1
PPS 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;
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
#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.
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.
#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
//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.
Title: 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>
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
𝒙 𝒙𝟐 𝒙𝟑
//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
#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);
#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
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);
//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.
//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;
}
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);
}
//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);
}
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);
}
//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);
}
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
return 0;
}
P a g e | 30
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);
}
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
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);
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
#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);
}
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;
}
printf("%d\t", array[k]);
}
}
P a g e | 49
#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