0% found this document useful (0 votes)
22 views5 pages

Assignment-1 Arunkumar K

Uploaded by

Mr.K .Arun Kumar
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)
22 views5 pages

Assignment-1 Arunkumar K

Uploaded by

Mr.K .Arun Kumar
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/ 5

NAME : ARUNKUMAR K

ROLL NO : 11
Programming and Data Structures Assignment-1
Problem 1.1. Write a C program to find sum of all prime numbers between 1 to
n using for loop.
include <stdio.h>
int main()
{
int i, j, end, isPrime, sum=0;
printf("Find sum of all prime between 1 to : "); /* Input upper limit from user */
scanf("%d", &end);
/* Find all prime numbers between 1 to end */
for(i=2; i<=end; i++)
{
/* Check if the current number i is Prime or not */
isPrime = 1;
for(j=2; j<=i/2 ;j++)
{
if(i%j==0)
{
/* 'i' is not prime */
isPrime = 0;
break;
}
}
/** If 'i' is Prime then add to sum **/
if(isPrime==1)
{
sum += i;
}
}
printf("Sum of all prime numbers between 1 to %d = %d", end, sum);
return 0;
}

C program to generate sum of all primes between a given range.


#include <stdio.h>
int main()
{
int i, j, start, end;
int isPrime, sum=0;
printf("Enter lower limit: "); /* Input lower and upper limit from user */
scanf("%d", &start);
printf("Enter upper limit: ");
scanf("%d", &end);
/* Find all prime numbers in given range */
for(i=start; i<=end; i++)
{
/* Check if the current number i is Prime or not */
isPrime = 1;
for(j=2; j<=i/2 ;j++)
{
if(i%j==0)
{
/* 'i' is not prime */
isPrime = 0;
break;
}
}
/** If i is Prime then add to sum */
if(isPrime==1)
{
sum += i;
}
}
printf("Sum of all prime numbers between %d to %d = %d", start, end, sum);
return 0;
}
Problem 1.2. Write a C program to input a number from user and calculate
product of its digits. How to find product of digits of a number using loop in C
programming.

#include <stdio.h>
int main()
{
int num;
long long product=1ll;
/* Input number from user */
printf("Enter any number to calculate product of digit: ");
scanf("%d", &num);
product = (num == 0 ? 0 : 1ll);
/* Repeat the steps till num becomes 0 */
while(num != 0)
{
/* Get the last digit from num and multiplies to product */
product = product * (num % 10);
/* Remove the last digit from n */
num = num / 10;
}
printf("Product of digits = %lld", product);
return 0; }
Problem 1.3. Write a C program to input a number and calculate its factorial
using for loop.How to find the factorial of a number in a C program using a
loop.
#include <stdio.h>
int main()
{
int i, num;
unsigned long long fact=1LL;
/* Input number from user */
printf("Enter any number to calculate factorial: ");
scanf("%d", &num);

/* Run loop from 1 to num */


for(i=1; i<=num; i++)
{
fact = fact * i;
}
printf("Factorial of %d = %llu", num, fact);
return 0;
}

You might also like