Programs
Programs
Programs
#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;
}
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;
#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);
}
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;
#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);
}
#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]);
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;
}
}
}
#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
}
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;
}
#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 */
j12 = k0 = 0;
#include <stdio.h>
int main()
{
int c, n, search,array[100];
scanf("%d",&n);
scanf("%d", &search);
int first, last, middle;
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
#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;
}
#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);
}
scanf("%d", &num); //The number taken as input from test case data
#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;
}
#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++)
{
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);
}
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