Week 5, NPTEL Assignemnt: 2. Write A C Program To Check Whether A Given Number Is A Perfect Number or Not
Week 5, NPTEL Assignemnt: 2. Write A C Program To Check Whether A Given Number Is A Perfect Number or Not
1. Write a program in C to find the sum of the series 1 +11 + 111 + 1111 + .. n terms
(n will be given as input from the user and sum will be the output)
#include <stdio.h>
void main()
{
int n, i; /* n is number of terms as input from test case and i is a variable*/
long sum=0; /* sum is a variable to store the sum and give as output */
scanf("%d",&n); /*Input the number of terms */
/* you have to write the rest of the code here */
/* You can Declare you own variables, do processing but do not give
anything as output. For output the code is already written which prints
the variable 'sum' as output. */
int j=0;
for(i=0;i<n;i++)
{
j = j * 10 + 1;
sum = sum + j;
}
printf("%ld",sum);
}
#include <stdio.h>
int main()
{
int n,sum; /*n to store the number, sum - to store the sum of factors*/
scanf("%d",&n); /*Input the number from the test case 6, 28 & 45 respectively */
int i;
sum = 1;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
sum = sum+i;
}
if(sum==n)
printf("YES");
else
printf("NO");
}
3.Write a C program to determine whether a given number (other than 1) is prime or
not
#include <stdio.h>
int main(){
int num, flag=0; /*flag is used to take decision */
scanf("%d",&num); /*input is given from test case */
/* you have to write the rest of the code here */
int i;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
flag = 1;
break;
}
}
if(flag == 1)
printf("NOT PRIME");
else
printf("PRIME");
}
#include <stdio.h>
int main()
{
int unit_consumed;
float netamount;
scanf("%d",&unit_consumed); /*unit consumed by the customer */
if(unit_consumed <200)
netamount = 1.2 * unit_consumed;
else if(unit_consumed >=200 && unit_consumed<400)
netamount = 1.5 * unit_consumed;
else if(unit_consumed >=400 && unit_consumed<600)
netamount = 1.8 * unit_consumed;
else
netamount = 1.8 * unit_consumed;
if(netamount<100)
netamount = 100;
if(netamount>400)
netamount = 1.15 * netamount;
printf("%.2f",netamount); /* netamount is the total amount to be paid*/
}
Complete the C program to convert days into years, month and days. (Ignoring leap year
and considering 1 month is 30 days)
#include <stdio.h>
int main()
{
int days, years, month;
scanf("%d", &days); /* Total number of days entered from the test case */
/* Write the code to complete the program */
#include <stdio.h>
int main()
{
int Num1, Num2, LCM;
scanf("%d %d", &Num1, &Num2); /*Two integer value Num1 and Num2 is entered from
test case and LCM is the output */
/* Write the remaining code here. You can declare new variables but the final
output should be stored in the variable LCM */
int a = Num1;
int b = Num2;
int r;
while(a!=0)
{
r = b%a;
b = a;
a = r;
}
Complete the C program to accept an integer & find the sum of its digits
#include <stdio.h>
int main()
{
int num, sum = 0;
scanf("%d", &num); /* The number is entered from test case in num */
int temp = num;
while(num)
{
sum = sum + num % 10;
num = num/10;
}
printf("Sum of the digits %d = %d", temp, sum);
Write a C program to check whether a triangle can be formed by the given value for
the angles
int main()
{
int anga, angb, angc, sum, flag;
scanf("%d %d %d", &anga, &angb, &angc); /* Three angles are given as input
from test case */
if(anga+angb+angc == 180)
flag = 1;
else
flag = 0;
if (flag == 1)
{
printf("The triangle is valid.");
}
else
{
printf("The triangle is not valid.");
}
}
Write a C program to calculate profit and loss (Cost Price and Selling Price is given as
inputs
#include <stdio.h>
int main()
{
float CP,SP, PLamt; /*CP is Cost Price and SP is Selling Price, PLamt total profit/loss */
int flag;
scanf("%f %f", &CP, &SP); /*input taken from test case data */
PLamt = SP - CP;
if(PLamt == 0)
flag = 2;
else if(PLamt < 0)
flag = 0;
else
flag = 1;
if(PLamt < 0)
PLamt = -Plamt;
if (flag == 0)
printf("Loss amount : Rs. %.2f", PLamt);
else if (flag == 1)
printf("Profit amount : Rs. %.2f", PLamt);
else
printf("No profit No loss");
}
#include <stdio.h>
int main()
{
int p,c,m, flag=0;
scanf("%d %d %d",&p, &c, &m); /* Input the marks obtained in Physics, Chemistry and
Maths */
if (flag == 1)
printf("The candidate is eligible");
else
printf("The candidate is not eligible");
}
Quiz – 5,
Key Answers :
Question Answer Question Answer
1 Z=2 2 10
3 5 4 13
5 Blue 6 Sum of the Squares
of 1stN-1 Natural
Numbers.
7 Infinite 8 11 times
9 Will Flash Error 10 All options are
incorrect mark any one.