20_10

Write a C program to accept an integer & find the sum of its digits.

Source code:

 //Write a C program to accept an integer & find the sum of its digits

#include <stdio.h>

int main()

{

int num, sum = 0;

printf("Enter a number: ");

scanf("%d", &num); 

int digit, temp;

temp = num;

while (num > 0)

{

digit = num % 10;

sum = sum + digit;

num /= 10;

}

printf("Sum of the digits %d = %d", temp, sum);

return 0;

}

Output:

C program to accept an integer & find the sum of its digits


Write a C program to find LCM of two numbers using GCD

Source code:

 //C program to find LCM of two numbers using GCD

#include <stdio.h>

int main()

{

int Num1, Num2, LCM; 

printf("Enter two number: ");

scanf("%d %d", &Num1, &Num2); 

int a = Num1;

int b = Num2;

int Temp, GCD;

while (Num2 != 0)

{

    Temp = Num2;

    Num2 = Num1 % Num2;

    Num1 = Temp;

}

GCD = Num1;

LCM = (a * b) / GCD;

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

return 0;

}

Output:

C program to find LCM of two numbers using GCD


Write a C program to convert days into years, month and days.

Source Code: 

/*Write a 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;

printf("Enter total number of days: ");

scanf("%d", &days); 

years=0;month=0;

if(days>=365)

{

years=days/365;

days=days%365;

}

if(days>=30)

{

month=days/30;

days=days%30;

}

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

return 0;

}


Or, Source Code:


#include <stdio.h>

int main()

{

int days, years, month;

printf("Enter total number of days: ");

scanf("%d", &days); 

years = (days / 365);

month = (days % 365) / 30;

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

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

return 0;

}

Output:

Write a C program to convert days into years, month and days


Write a C program to determine whether a given number is prime or not.

 Source code:

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

printf("Enter a number: ");

    scanf("%d",&num); 

int i;

for(i=2;i<=num/2;i++)

{

if(num%i==0)

    flag=1;

}

if(flag == 1)

        printf("NOT PRIME");

    else

        printf("PRIME");

    return 0;

}

Output:

Write a C program to determine whether a given number is prime or not


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

Source Code:

 /* 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*/
printf("Enter a number: ");
scanf("%d",&n); /*Input the  number from the test case 6, 28 & 45 respectively */
int i;
sum=0;
for(i=1;i<n;i++)
{
  if(n%i==0)
    sum=sum+i;
}
if(sum==n)
    printf("YES");
    else
    printf("NO");
    return 0;
}

Output:
Write a c program to check whether a given number is a perfect number or not


Write a program in C to find the sum of the series 1 +11 + 111 + 1111 + .. n terms

Source Code:

 //Write a program in C to find the sum of the series 1 +11 + 111 + 1111 + .. n terms

#include <stdio.h>

int main()

{

  int n, i; /* n is number of terms as input and i is a variable*/

  long sum=0; /* sum is a variable to store the sum and give as output */

  printf("Enter the n th term: ");

  scanf("%d",&n); /*Input the number of terms */


int k=0;

for(i=0;i<n;i++)

{

k=(k*10)+1;

sum=sum+k;

}

printf("%ld",sum);

return 0;

}

Output:

Write a program in C to find the sum of the series 1 +11 + 111 + 1111 + .. n terms




Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...