0% found this document useful (0 votes)
108 views

Week 5, NPTEL Assignemnt: 2. Write A C Program To Check Whether A Given Number Is A Perfect Number or Not

The document contains solutions to various programming problems in C language. It includes programs to find the sum of a series, check if a number is perfect, check if a number is prime, calculate electricity bill amount, convert days to years and months, find LCM of two numbers, find sum of digits of a number, check validity of a triangle, calculate profit/loss, and check eligibility of a candidate. The programs contain code snippets with comments to complete the programs to solve the given problems.

Uploaded by

akshay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Week 5, NPTEL Assignemnt: 2. Write A C Program To Check Whether A Given Number Is A Perfect Number or Not

The document contains solutions to various programming problems in C language. It includes programs to find the sum of a series, check if a number is perfect, check if a number is prime, calculate electricity bill amount, convert days to years and months, find LCM of two numbers, find sum of digits of a number, check validity of a triangle, calculate profit/loss, and check eligibility of a candidate. The programs contain code snippets with comments to complete the programs to solve the given problems.

Uploaded by

akshay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Week 5, NPTEL Assignemnt

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);
}

2. Write a c program to check whether a given number is a perfect number or not.


(Perfect number is a positive number which sum of all positive divisors excluding
that number is equal to that number. For example 6 is perfect number since divisor
of 6 are 1, 2 and 3. Sum of its divisor is 1 + 2+ 3 = 6)

#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 */

years = days / 365;

month = (days - years * 365)/30;

days = days - years * 365 - month * 30;

printf("YEARS: %d MONTH: %d DAYS: %d", years, month, days);


}

C program to find LCM of two numbers using GCF

#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;
}

LCM = (Num1 * Num2)/b;

printf("LCM = %d", LCM);


}

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(m>=65 && p>=55 && c>=50)


flag = 1;

else if(m+p+c >=180)


flag = 1;

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.

You might also like