This article will help you with the answers of Problem Solving Through Programming in C NPTEL Week 5 programs.
Week 5 Program 01 Solution
Write a C program to count total number of digits of an Integer number (N).
#include <stdio.h>
int main()
{
int N;
scanf("%d",&N); /*The number is accepted from the test case data*/
/* Complete the rest of the code. Please use the printf statements as below
by just changing the variables used in your program
printf("The number %d contains %d digits.", N, count);
*/
int num = N, count = 0;
while(num > 0) {
num = num / 10;
count++;
}
printf("The number %d contains %d digits.", N, count);
return 0;
}
Week 5 Program 02 Solution
Write a C program to find sum of following series where the value of N is taken as input
1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N
#include<stdio.h>
int main()
{
int N;
float sum = 0.0;
scanf("%d",&N); /*Read the value of N from test cases provided*/
/* Complete the program. Please use the printf statement given below:
printf("Sum of the series is: %.2f\n", sum);
*/
for(int i=1; i<=N; i++){
sum += (float)1/i;
}
printf("Sum of the series is: %.2f\n", sum);
return 0;
}
Week 5 Program 03 Solution
Write a C program to print the following Pyramid pattern upto Nth row. Where N (number of rows to be printed) is taken as input. For example when the value of N is 5 the pyramid will be printed as follows
*****
****
***
**
*
#include<stdio.h>
int main()
{
int N;
scanf("%d", &N); /*The value of N is taken as input from the test case */
for(int i=N; i>0; i--) {
for(int j=1; j<=i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Week 5 Program 04 Solution
Write a C program to find sum of following series where the value of N(odd integer number) is taken as input
12+32+52+…..+n2
#include<stdio.h>
int main()
{
int n, sum=0;
scanf("%d",&n); //Value of n is taken from the test cases
for(int i=1; i<=n; i=i+2){
sum += i*i;
}
printf("Sum = %d", sum);
return 0;
}
For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.