Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

Weak 12:

Program : 1 Write a program in C to find the factorial of a given number using


pointers.

#include <stdio.h>
void findFact(int, long int*);
int main()
{
long int fact; //factorial of the number
int num1;
scanf("%d",&num1); //The number is taken from test data

findFact(num1, &fact);
printf("The Factorial of %d is : %ld\n",num1, fact);
return 0;
}

void findFact(int n, long int *f)


{
int i;
*f =1;
for(i=1;i<=n;i++)
*f=*f*i;
}

Program : 2 Write a C program to print the Record of the Student Merit wise.
Here a structure variable is defined which contains student rollno, name and
score.

#include<stdio.h>
struct student
{
int rollno;
char name[20];
int score;
};
void main()
{
struct student s[20];
int i, n;

scanf("%d" ,&n); //No. of Students taken from test data


// Roll no., Name and Score of n students are taken from test data
for(i=0;i<n;i++)
{
scanf("%d", &s[i].rollno);
scanf("%s", s[i].name);
scanf("%d", &s[i].score);
}

//Complete the program so that merit list is printed in descending order


struct student temp;
int j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(s[j].score<s[j+1].score)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
printf("The Merit List is :\n");
for(i=0;i<n;i++)
{
printf("%d", s[i].rollno);
printf(" %s", s[i].name);
printf(" %d\n", s[i].score);
}

Program : 3 Write a C program to store n elements using Dynamic Memory


Allocation – calloc() and find the Largest element

#include <stdio.h>
#include <stdlib.h>

int main()
{
int n;
float *element;

scanf("%d", &n);
element = (float*) calloc(n, sizeof(float));

if(element == NULL)
{
printf("Error!!! memory not allocated.");
exit(0);
}

// Stores the number entered by the user.


int i;
for(i = 0; i < n; ++i)
{
scanf("%f", element + i);
}

// find the largest


for(i = 1; i < n; ++i)
{
if(*element < *(element + i))
*element = *(element + i);
}
printf("Largest element = %.2f\n", *element);
return 0;
}

Program : 4 Write a C program to add two distance given as input in feet and
inches.
#include<stdio.h>

struct Distance{
int feet;
int inch;
}d1,d2,sum;

int main()
{
//Enter 1st distance
scanf("%d",&d1.feet);
scanf("%d",&d1.inch);
//Enter 2nd distance
scanf("%d",&d2.feet);
scanf("%d",&d2.inch);
sum.feet=d1.feet+d2.feet;
sum.inch=d1.inch+d2.inch;

/* If inch is greater than 12, converting it to feet. */


if (sum.inch>=12)
{
sum.inch=sum.inch-12;
++sum.feet;
}
printf("Sum of distances = %d feet %d inches",sum.feet,sum.inch);
return 0;
}
Weak 11
Program : 1

#include<stdio.h>
int main()
{
float t[100]={10,15,18,22,30}, v[100]={22,26,35,48,68};
float a; //Value of the t to find the respective value of v(t)
scanf("%f", &a);
int i,j;
float b, c, k =0;
for(i=0; i<5; i++)
{
b=1;
c=1;
for(j=0; j<5; j++)
{
if(j!=i)
{
b=b*(a-t[j]);
c=c*(t[i]-t[j]);
}
}
k=k+((b/c)*v[i]);
}
printf("The respective value of the variable v is: %.2f", k);
return 0;
}

Program : 2

#include<stdio.h>
float func(float x);
int main()
{
int n=10; //Taking n=10 sub intervals
float a,b,integral; //integral is the integration result
scanf("%f",&a); // initial limit taken from test case
scanf("%f",&b);
int i;
float h,x, sum=0;
if(b>a)
h=(b-a)/n;
else
h=-(b-a)/n;
for(i=1;i<n;i++)
{
x=a+i*h;
sum=sum+func(x);
}
integral=(h/2)*(func(a)+func(b)+2*sum);
printf("The integral is: %0.6f\n",integral);
return 0;
}

float func(float x)
{
return x*x;
}

Program : 3

#include<stdio.h>
float func(float x,float y);
int main()
{
float xn;
scanf("%f", &xn);
float g1,g2,g3,g4,g,h=0.3;
float x0 = 0.3, y0 = 5;
while(x0 < xn)
{
g1=func(x0,y0);
g2=func((x0+h/2.0),(y0+g1*h/2));
g3=func((x0+h/2.0),(y0+g2*h/2));
g4=func((x0+h),(y0+g3*h));
g=((g1+2*g2+2*g3+g4)/6);
y0=y0+g*h;
x0=x0+h;
}
printf("y=%.6f",y0);
return 0;
}
float func(float x,float y)
{
float m;
m=(x*(x+1)-3*y*y*y)/10;
return m;
}

Program : 4

#include <stdio.h>
int checkPrime(int, int); //Function to check prime or not

int main()
{
int num, check;
scanf("%d", &num); //The number is taken from test case data
check = checkPrime(num, num/2);
if (check == 1)
{
printf("%d is a prime number\n", num);
}
else
{
printf("%d is not a prime number\n", num);
}
return 0;
}
int checkPrime(int num, int i)
{
if (i == 1)
return 1;
else
{
if (num % i == 0)
return 0;
else
return checkPrime(num, i - 1);
}
}

Week 10

#include<stdio.h>
float fun (float x);
void bisection (float *x, float a, float b, int *itr);

int main ()
{
int itr = 0, maxmitr=10;
float x, a=1.0, b=2.0, allerr, x1;
scanf("%f", &allerr);
bisection (&x, a, b, &itr);

do
{
if(fun(a)*fun(x) < 0)
b=x;
else
a=x;
bisection (&x1, a, b, &itr);
if(((x1-x)<0 && -(x1-x)< allerr) || ((x1-x)>0 && (x1-x)< allerr))
{
printf("Root = %1.4f", x1);
return 0;
}
x=x1;
} while(itr < maxmitr);
return 1;
}

float fun(float x)
{
return (2*x*x*x - 3*x - 5);
}

void bisection(float *x, float a, float b, int *itr)


/* this function performs and prints the result of one iteration */
{
*x=(a+b)/2;
++(*itr);
}

2 Write a C code to check if a 3 x 3 matrix is invertible. A matrix is not


invertible if its determinant is 0.

#include<stdio.h>
int main()
{
int a[3][3], i, j;
long determinant;
//9 elements of matrix is taken as input from test data
for(i = 0 ; i < 3; i++)
for(j = 0; j < 3; j++)
scanf("%d", &a[i][j]);

determinant = a[0][0] * ((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1] * (a[1]


[0]
* a[2][2] - a[2][0] * a[1][2]) + a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]);
if(determinant == 0)
printf("The given matrix is not invertible");
else
printf("The given matrix is invertible");
return 0;
}

3 Write a C program to sort a given 1D array using pointer in ascending order.


#include <stdio.h>
int main()
{
int a[100],i, n;
scanf("%d",&n);

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


{
scanf("%d",a+i); // Input the array elements
}

int j,t;
for(i=0; i<(n-1); i++)
{
for (j=i+1; j<n; j++)
{
if (*(a+i)>*(a+j))
{
t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
}
}
}

// Printing sorted array in ascending order


for (i=0; i<n; i++)
{
printf("%d\n",*(a+i));
}
return 0;
}

4 Write a C program to sort a 1D array using pointer by applying Bubble sort


technique.

#include<stdio.h>
void sort(int *a, int n);
int main()
{
int a[20];
int n,i;
scanf("%d",&n);

for(i=0;i<n;i++)
{
scanf("%d",&a[i]); // The elements of the array is taken from the test data
}

sort(a, n); // Calling the sorting function


//Printing the sorted array
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
return 0;
}

void sort(int *a, int n)


{
int ii,temp,j2;
for(ii=1;ii < n;ii++)
{
for(j2=0;j2 < n-ii;j2++)
{
if(*(a+j2) >= *(a+j2+1))
{
temp = *(a+j2);
*(a+j2)= *(a+j2+1);
*(a+j2+1)= temp;
}
}
}
}

Weak 9
Question : 1 Write a program to print all the locations at which a particular
element (taken as input) is found in a list and also print the total number of times
it occurs in the list. The location starts from 1.
For example if there are 4 elements in the array 5 6 5 7
If the element to search is 5, then the output will be:
5 is present at location 1
5 is present at location 3
5 is present 2 times in the array.

#include <stdio.h>
int main()
{
int array[100], search, n, count = 0;
scanf("%d", &n);
int c;
for (c = 0; c < n; c++)
scanf("%d", &array[c]);

scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search)
{
printf("%d is present at location %d.\n", search, c+1);
count++;
}
}
if (count == 0)
printf("%d is not present in the array.\n", search);
else
printf("%d is present %d times in the array.\n", search, count);

return 0;
}

Question : 2 Write a C program to marge two given sorted arrays (sorted in


ascending order).

#include <stdio.h>
void merge(int a[], int m, int b[], int n, int sorted[]);
int main()
{
int a[100], b[100], m, n, c, sorted[200];
/* a[100] and b[100] are the two given arrays and m and n are the their
respective sizes. c is a counter and sorted[200] is the final sorted array */

scanf("%d", &m); //Number of elements in the first array

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


{
scanf("%d", &a[c]); //Elements of first array is read
}

scanf("%d", &n); //Number of elements in second array

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


{
scanf("%d", &b[c]); //Elements of second array is read
}
merge(a, m, b, n, sorted);
//The merged function is called where the two arrays are merged and sorted.
printf("Sorted array:\n");
for (c = 0; c < m + n; c++)
{
printf("%d\n", sorted[c]);
}
return 0;
}

void merge(int a[], int m, int b[], int n, int sorted[]) {


int i, j12, k0;

j12 = k0 = 0;

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


if (j12 < m && k0 < n) {
if (a[j12] < b[k0]) {
sorted[i] = a[j12];
j12=j12+1;
}
else {
sorted[i] = b[k0];
k0++;
}
i++;
}
else if (j12 == m) {
for (; i < m + n;) {
sorted[i] = b[k0];
k0++;
i++;
}
}
else {
for (; i < m + n;) {
sorted[i] = a[j12];
j12++;
i++;
}
}
}
}

Question : 3 Write a C program to search a given number from a sorted 1D


array and display the position at which it is found using binary search algorithm.
The index location starts from 1.

#include <stdio.h>
int main()
{
int c, n, search,array[100];
scanf("%d",&n);

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


scanf("%d",&array[c]);

scanf("%d", &search);
int first, last, middle;
first = 0;
last = n - 1;
middle = (first+last)/2;

while(first <= last)


{
if(array[middle] < search)
first = middle + 1;
else if(array[middle] == search)
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if(first > last)
printf("Not found! %d isn't present in the list.\n", search);

return 0;
}

Question : 4 Write a C program to reverse an array by swapping the elements


and without using any new array.

#include <stdio.h>
int main() {
int array[100], n, c;
scanf("%d", &n); // n is number of elements in the array.
for (c = 0; c < n; c++)
{
scanf("%d", &array[c]);
}
int temp, end;
end = n - 1;
for(c = 0; c < n/2; c++)
{
temp = array[c];
array[c] = array[end];
array[end] = temp;
end--;
}
printf("Reversed array elements are:\n");
for (c = 0; c < n; c++)
{
printf("%d\n", array[c]);
}
return 0;
}

NPTEL Problem Solving Through Programming In C WEEK 8 Assignment


2023

Question : 1 Write a C Program to find HCF of 4 given numbers using recursive


function

#include<stdio.h>
int HCF(int, int); //You have to write this function which calculates the HCF.

int main()
{
int a, b, c, d, result;
scanf("%d %d %d %d", &a, &b, &c, &d); /* Takes 4 number as input from the
test data */
result = HCF(HCF(a, b), HCF(c,d));
printf("The HCF is %d", result);
}

int HCF(int x, int y)


{
while (x != y)
{
if (x > y)
return HCF(x - y, y);
else
return HCF(x, y - x);
}
return x;
}

Question : 2 Write a C Program to find power of a given number using recursion.


The number and the power to be calculated is taken from test case
#include <stdio.h>
long power(int, int);
int main()
{
int pow, num;
long result;

scanf("%d", &num); //The number taken as input from test case data

scanf("%d", &pow); //The power is taken from the test case


result = power(num, pow);
printf("%d^%d is %ld", num, pow, result);
return 0;
}
long power(int n, int p)
{
if (p)
{
return (n * power(n, p - 1));
}
return(1);
}

Question : 3 Write a C Program to print Binary Equivalent of an Integer using


Recursion

#include <stdio.h>
int binary_conversion(int);
int main()
{
int num, bin;

scanf("%d", &num);
bin = binary_conversion(num);
printf("The binary equivalent of %d is %d\n", num, bin);
return 0;
}

int binary_conversion(int num)


{
if(num == 0)
return 0;
else
return (num%2) + 10 * binary_conversion(num/2);
}

Question : 4 Write a C program to print a triangle of prime numbers upto given


number of lines of the triangle.
e.g If number of lines is 3 the triangle will be
2
3 5
7 11 13

#include<stdio.h>
int prime(int num); //Function to find whether the number is prime or not.
int main() {
int lines;
scanf("%d", &lines);
int q, t;
int num = 2;
for (q = 0; q < lines; q++)
{

for (t = 0; t <= q; t++)


{
while (!prime(num))
{
num++;
}
printf("%d\t", num++);
}
printf("\n");
}
return (0);
}

int prime(int num)


{
int q, flag;
for (q = 2; q < num; q++)
{
if (num % q != 0)
flag = 1;
else {
flag = 0;
break;
}
}

if (flag == 1 || num == 2)
return (1);
else
return (0);
}

Weak7
Week-07 Program-04 Write a C program to print lower triangle of a square matrix.
For example the output of a given matrix
2 3 4 will be 200
567 560
456 456

#include <stdio.h>
int main() {
int matrix[20][20];
int i,j,r;
scanf("%d", &r); //Accepts number of rows or columns
for(i=0;i< r;i++) //Accepts the matrix elements from the test case
{ for(j=0;j<r; j++)
{ scanf("%d",&matrix[i][j]);
}
}
/* Complete the program to get the desired output. Use the printf() sta printf("%d ",
matrix[i][j]); */
for(i=0; i<r; i++) {
for(j=0; j<r; j++) {
if(i>=j)
printf("%d ", matrix[i][j]);
else
printf("%d ", 0);
}
printf("\n");
}
return 0;
}

Week-07 Program-02
Write a C program to find the sum of all elements of each row of a matrix.
Example: For a matrix 4 5 6
673
123
The output will be
15
16
6

#include <stdio.h>
int main() {
int matrix[20][20];
int i,j,r,c;
scanf("%d",&r); //Accepts number of rows
scanf("%d",&c); //Accepts number of columns
for(i=0;i< r;i++) //Accepts the matrix elements from the test case
{
for(j=0;j< c;j++)
{
scanf("%d",&matrix[i][j]);
}
}
int sum;
for(i=0;i< r;i++) {
sum=0;
for(j=0;j< c;j++)
{
printf("%d\t",matrix[i][j]);
sum += matrix[i][j];
} printf("%d\n",sum);
}
}

Week-07 Program-01
Write a C Program to Count Number of Uppercase and Lowercase Letters in a
given string. The string may be a word or a sentence.

#include<stdio.h>
int main() {
int upper = 0, lower = 0;
char ch[100];
scanf(" %[^\n]s", ch); /*A word or a sentence is accepted from test /* Complete
the remaining part of the code to store number of uppercase in the variable upper
and lowercase letters in variable lower. The print part of already written. You can
declare any variable if nece*/

int i = 0;
while (ch[i] != '\0') {
if (ch[i] >= 'A' && ch[i] <= 'Z')
upper++;
if (ch[i] >= 'a' && ch[i] <= 'z')
lower++;
i++;
}
printf("Uppercase Letters : %d\n", upper); /*prints number of upperc
printf("Lowercase Letters : %d\n", lower); /*prints number of lowerc
return (0);
}

Question : 3 Write a C program to find subtraction of two


matrices i.e, matrix_A – matrix_B=matrix_C.
If the given martix are:
2 3 5 and 1 5 2 Then the output will be 1 -2 3
456 234 222
657 334 323
#include <stdio.h>
int main()
{
int matrix_A[20][20], matrix_B[20][20], matrix_C[20][20];
int i,j,row,col;
scanf("%d",&row);
scanf("%d",&col);

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


{
for(j=0; j<col; j++)
{
scanf("%d", &matrix_A[i][j]);
}
}

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


{
for(j=0; j<col; j++)
{
scanf("%d", &matrix_B[i][j]);
}
}
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{

matrix_C[i][j] = matrix_A[i][j] - matrix_B[i][j];


}
}

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


{
for(j=0; j<col; j++)
{
printf("%d ", matrix_C[i][j]);
}
printf("\n");
}
return 0;
}
Weak_06_Prg03

Write a C program to read Two One Dimensional Arrays of same data type
(integer type) and merge them into another One Dimensional Array of same type.

#include<stdio.h>
int main() {
int arr1[20], arr2[20], array_new[40], n1, n2, size, i; /*n1 size of first array (i.e.
arr1[]), n2 size of second array(i.e. a size is the total size of the new array
(array_new[]) */
scanf("%d", &n1); //Get the size of first array from test data and s
for (i = 0; i < n1; i++)
scanf("%d", &arr1[i]); //Accepts the values for first array
scanf("%d", &n2); //Get the size of second array from test data and
for (i = 0; i < n2; i++)
scanf("%d", &arr2[i]); //Accepts the values for second array
//Me rge two arrays
int j;
for (i=0;i<n1;++i)
array_new[i]=arr1[i];
size = n1 + n2;
for(i=0, j=n1; j<size && i<n2; ++i, ++j)
array_new[j] = arr2[i];
//Printing after merging
for (i = 0; i < size; i++)
{
printf("%d\n", array_new[i]);
}
}

Weak 06_prg_01

Write a C Program to find Largest Element of an Integer Array.


#include <stdio.h>
int main()
{
int i, n, largest;
int arr[100];
scanf("%d", &n); /*Accepts total number of elements
from the test data */

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


{
scanf("%d", &arr[i]); /* Accepts the array element from
test data */
}
largest=arr[0];
for(i = 0; i < n; ++i)
{
if(largest>arr[i])
continue;
largest=arr[i];
}
printf("Largest element = %d", largest);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy