NPTEL Problem Solving Through Programming In C
Week 11 programming 01.
#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); // This will be taken from test cases
int i,j;
float x,y,k=0;
for(i=0; i<5; i++)
{
x=1,y=1;
for(j=0; j<5; j++)
{
if(j!=i)
{
x=x*(a-t[j]);
y=y*(t[i]-t[j]);
}
}
k=k+((x/y)*v[i]);
}
printf("The respective value of the variable v is: %.2f\n", k);
return 0;
}
Week 11 programming 02.
#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); // Final limit taken from test case
//Use the printf statement as printf("The integral is: %0.6f\n",integral);
int i;
float m,x,total=0;
if(b>a)
m=(b-a)/n;
else
m=-(b-a)/n;
for(i=1; i<n; i++)
{
x=a+i*m;
total=total+func(x);
}
integral=(m/2)*(func(a)+func(b)+2*total);
printf("The integral is: %0.6f\n",integral);
return 0;
}
float func(float x)
{
return x*x;
}
Week 11 programming 03.
#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 11 programming 04.
#include <stdio.h>
#define MAX 100
char *reverse(char[]);
int main()
{
char str[MAX], *rev;
//printf("Enter a String: ");
scanf("%s", str);
rev = reverse(str); //You have to write this function
printf("The reversed string is : %s\n", rev);
return 0;
}
char *reverse(char str[])
{
static int i=0;
static char rev[MAX];
if(*str)
{
reverse(str+1);
rev[i++]=*str;
}
return rev;
}
Week 11 programming 05.
#include<stdio.h>
float func(float x,float y);
int main()
{
float xn;
//printf("Enter xn:");
scanf("%f",&xn); // xn will be taken from test cases
//Use printf statement as: printf("y=%f\n",y0);
float x0=0.3,y0=2,m1,m2,m3,m4,m,y,h=0.2;
while(x0<xn)
{
m1=func(x0,y0);
m2=func((x0+h/2.0),(y0+m1*h/2.0));
m3=func((x0+h/2.0),(y0+m2*h/2.0));
m4=func((x0+h),(y0+m3*h));
m=((m1+2*m2+2*m3+m4)/6);
y0=y0+m*h;
x0=x0+h;
}
y=y0;
printf("y=%f\n",y0); // Final output
return 0;
}
float func(float x,float y)
{
float m;
m=(x*(2*x+1)-3*y*y)/10;
return m;
}
Week 12 Programming 01.
#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;
}
Week 12 Programming 02.
#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);
}
}
Week 12 Programming 03.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
float *element;
scanf("%d", &n); //Total number of elements
// Allocate the memory for 'n' number of elements.
//Then take the elements as input from test data
// Use the printf statement as:
// printf("Largest element = %.2f\n", *element);
element=(float*) calloc(n,sizeof(float));
if(element==NULL)
{
printf("Error!!! memory not allocated.");
exit(0);
}
int i;
for(i=0;i<n;++i)
{
scanf("%f",element+i);
}
for(i=1;i<n;++i)
{
if(*element<*(element+i))
*element=*(element+i);
}
printf("Largest element = %.2f\n", *element);
return 0;
}
Week 12 Programming 04.
#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(sum.inch>=12)
{
sum.inch=sum.inch-12;
++sum.feet;
}
printf("Sum of distances = %d feet %d inches\n",sum.feet,sum.inch);
return 0;
}
Week 12 Programming 05.
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i,n;
//The number of elements in each array is taken from test case data
scanf("%d", &n);
int *a,*b,*c;
a=(int *) malloc(n*sizeof(int));
b=(int *) malloc(n*sizeof(int));
c=(int *) malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
for(i=0;i<n;i++)
{
scanf("%d",b+i);
}
for(i=0;i<n;i++)
{
*(c+i)=*(a+i)+*(b+i);
}
printf("Result is\n");
for(i=0; i<n; i++)
{
printf("%d\n",*(c+i));
}
}
Comments
Post a Comment