Assignment-1 Arunkumar K
Assignment-1 Arunkumar K
Assignment-1 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;
}
#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);