Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Programming for Problem Solving Lab (C)

(ESCS - 291)
CSE – 2nd Sem. – 1st Year

Programming for Problem Solving Lab (C) – ES(CS)291


REPORT ON: …………………………..………………………………………………………………Lab.

MD IMRAJUL ALAM
NAME: …………………………………………………………………………………………………………..

COMPUTER SCIENCE & ENGINEERING


DEPARTMENT: ……………………………………………………………………………………………..

2ND
20/CSE/002 SEM: …….…….…
ROLL NO: .……………..….…… C
GRP: ….…… CSE-II
SECTION: ……..........

III
ASSIGNMENT NO: ………..……PROGRAM NO: …..………………………………………....
1,2,3,4,5,6,7,8

TITLE / OBJECTIVE:

Solving C programs using Loops


.………………………………………………………………………………………..................................

……….………………………………………………………………………………..................................

………….……………………………………………………………………………..................................

………….……………………………………………………………………………..................................

………….……………………………………………………………………………..................................

21/06/2021 SUBMISSION DATE: ………………….......


PERFORMANCE DATE: …....……………….. 29/06/2021

EXAMINER’S SIGNATURE: ……………………………………………………………………………..


Assignment No. – III Page - 1 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

1. Write a Program in C to compute the Factorial of a given Number.

Program: prog1.c

#include<stdio.h>

int main()
{
printf("\t\t**** C PROGRAM TO COMPUTE THE FACTORIAL OF A GIVEN NUMBER
****\n\n");

int i,fact=1,number;

printf("Enter the number: ");


scanf("%d",&number);

for(i=1;i<=number;i++)
{
fact=fact*i;
}
printf("\nFactorial of %d is: %d",number,fact);

return 0;
}

Assignment No. – III Page - 2 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

OUTPUT:

**** C PROGRAM TO COMPUTE THE FACTORIAL OF A GIVEN NUMBER ****

Enter the number: 6

Factorial of 6 is: 720


Process returned 0 (0x0) execution time : 7.726 s
Press any key to continue.

Assignment No. – III Page - 3 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

2. Write a Program in C to find the sum of the following series

(a) S=1 + 3 + 5 + 7 + . . . . . + N (Input N).

(b) S=1 + 3 + 5 + 7 + . . . . . + Up to Nth Term. (Input N).

(c) S=1+1/2 + 1/4 + 1/8 + 1/16 + . . . . . + 1/ 2 N (Input N).

(d) S=1 + 1/3 + 1/9 + 1/27 + 1/81 + 1/243 + . . . . . Up to Nth Term. (Input N).

(e) S= 1 - 1/3 + 1/9 - 1/27 + 1/81 - 1/243 …. Up to Nth Term. (Input N).

(f) S= 1! + 2! + 3! + 4! + . . . . . + N! (Input N).

(g) S= 1/1! + 1/2! + 1/3! + 1/4! + . . . . . + 1/N! (Input N).

Program: prog2(A).c

#include<stdio.h>

int main ()
{
printf("\t\t**** C PROGRAM TO FIND THE SUM OF THE SERIES****\n\n");

int N, sum = 0;
printf("Enter the number: ");
scanf("%d", &N);

printf("\nThe series is: ");

for(int i = 1; i<=N ; i=i+2)


{
sum = sum + i;
printf("%d + ", i);
}
printf("\b\b ");

printf("\n\nThe sum of the series is %d \n", sum);

return 0;
}

Assignment No. – III Page - 4 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

OUTPUT:

**** C PROGRAM TO FIND THE SUM OF THE SERIES****

Enter the number: 19

The series is: 1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19

The sum of the series is 100

Process returned 0 (0x0) execution time : 3.884 s


Press any key to continue.

Assignment No. – III Page - 5 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

Program: prog2(B).c

#include <stdio.h>

int main()
{
printf("\t\t**** C PROGRAM TO FIND THE SUM OF THE SERIES****\n\n");

int N,sum = 0;
printf("Enter the number: ");
scanf("%d", &N);

int F=(2*N-1);

printf("\nThe series is: ");


for(int q = 1; q<=F ; q+=2)
{
printf("%d + ",q);
}
printf("\b\b ");

for (int i = 1; i <= N; i++)


{
sum = sum + i * i - (i - 1) * (i - 1); //Tn = n2 – ( n – 1 )2
}

printf("\n\nThe sum of the series is %d \n", sum);

return 0;
}

Assignment No. – III Page - 6 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

OUTPUT:

**** C PROGRAM TO FIND THE SUM OF THE SERIES****

Enter the number: 15

The series is: 1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 + 21 + 23 + 25 + 27 + 29

The sum of the series is 225

Process returned 0 (0x0) execution time : 8.431 s


Press any key to continue.

Assignment No. – III Page - 7 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

Program: prog2(C).c

#include<stdio.h>
#include<math.h>
int main()
{
printf("\t\t**** C PROGRAM TO FIND THE SUM OF THE SERIES****\n\n");

float N,A;
float sum = 0;

printf("Enter the number: ");


scanf("%f",&N);

printf("\nThe series is: ");

for(float i=0;i<=N;i++)
{
A = pow(2.0,i);
sum = sum +(1/A);
printf("1/%0.0f + ",A );
}
printf("\b\b ");

printf("\n\nThe sum of the series is %f \n", sum);

return 0;
}

Assignment No. – III Page - 8 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

OUTPUT:

**** C PROGRAM TO FIND THE SUM OF THE SERIES****

Enter the number: 5

The series is: 1/1 + 1/2 + 1/4 + 1/8 + 1/16 + 1/32

The sum of the series is 1.968750

Process returned 0 (0x0) execution time : 11.381 s


Press any key to continue.

Assignment No. – III Page - 9 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

Program: prog2(D).c

#include<stdio.h>
#include<math.h>

int main()
{
printf("\t\t**** C PROGRAM TO FIND THE SUM OF THE SERIES****\n\n");

float N,A;
float sum = 0;

printf("Enter the number: ");


scanf("%f",&N);

printf("\nThe series is: ");

for(float i=0;i<N;i++)
{
A=pow(3.0,i);

sum=sum +(1/A);
printf("1/%0.0f + ",A );
}
printf("\b\b ");

printf("\n\nThe sum of the series is %f \n", sum);

return 0;
}

Assignment No. – III Page - 10 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

OUTPUT:

**** C PROGRAM TO FIND THE SUM OF THE SERIES****

Enter the number: 8

The series is: 1/1 + 1/3 + 1/9 + 1/27 + 1/81 + 1/243 + 1/729 + 1/2187

The sum of the series is 1.499771

Process returned 0 (0x0) execution time : 1.475 s


Press any key to continue.

Assignment No. – III Page - 11 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

Program: prog2(E).c

#include<stdio.h>
#include<math.h>

int main()

{
printf("\t\t**** C PROGRAM TO FIND THE SUM OF THE SERIES****\n\n");

float t,N,A,sign;
float sum = 0;
int c;

printf("Enter the number: ");


scanf("%f",&N);

printf("\nThe series is: ");


for(t=0,c = 0; c<N;t++,c++)
{
if (c%2==0)
{
A=pow(3.0,t);
sign=pow(-1,t);
sum=sum+(1/A)*sign;
printf(" + 1/%0.0f",A);
}
else
{
A=pow(3.0,t);
sign=pow(-1,t);
sum=sum+(1/A)*sign;
printf(" - 1/%0.0f",A);
}
}

printf("\n\nThe sum of the series is %f \n", sum);

return 0;
}

Assignment No. – III Page - 12 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

OUTPUT:

**** C PROGRAM TO FIND THE SUM OF THE SERIES****

Enter the number: 7

The series is: + 1/1 - 1/3 + 1/9 - 1/27 + 1/81 - 1/243 + 1/729

The sum of the series is 0.750343

Process returned 0 (0x0) execution time : 13.811 s


Press any key to continue.

Assignment No. – III Page - 13 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

Program: prog2(F).c

#include<stdio.h>
#include<math.h>

int main()
{
printf("\t\t**** C PROGRAM TO FIND THE SUM OF THE SERIES****\n\n");

int N,fact = 1;
int sum = 0;

printf("Enter the number: ");


scanf("%d",&N);

printf("\nThe series is: ");

for(int i=1;i<=N;i++)
{
fact = 1;
for(int j=1;j<=i;j++)
{
fact=fact*j;
}

sum=sum + fact;
printf("%d! + ",i);
}
printf("\b\b ");

printf("\n\nThe sum of the series is %d \n", sum);

return 0;

Assignment No. – III Page - 14 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

OUTPUT:

**** C PROGRAM TO FIND THE SUM OF THE SERIES****

Enter the number: 6

The series is: 1! + 2! + 3! + 4! + 5! + 6!

The sum of the series is 873

Process returned 0 (0x0) execution time : 16.540 s


Press any key to continue.

Assignment No. – III Page - 15 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

Program: prog2(G).c

#include<stdio.h>
#include<math.h>

int main()
{
printf("\t\t**** C PROGRAM TO FIND THE SUM OF THE SERIES****\n\n");

float N,fact=1;
float sum = 0;

printf("Enter the number ");


scanf("%f",&N);

printf("\nThe series is: ");

for(float i=1;i<=N;i++)
{
fact = 1;
for(float j=1;j<=i;j++)
{
fact=fact*j;

}
sum = sum + 1/fact;
printf("1/%0.0f! + ",i);
}
printf("\b\b ");

printf("\n\nThe sum of the series is %f \n", sum);

return 0;

Assignment No. – III Page - 16 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

OUTPUT:

**** C PROGRAM TO FIND THE SUM OF THE SERIES****

Enter the number 6

The series is: 1/1! + 1/2! + 1/3! + 1/4! + 1/5! + 1/6!

The sum of the series is 1.718055

Process returned 0 (0x0) execution time : 2.330 s


Press any key to continue.

Assignment No. – III Page - 17 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

3. Write a Program in C to Generate the Fibonacci Series up to Nth Term.


1 + 1 + 2 + 3 + 5 + 8 + 13 + . . . . + Up to Nth Term.

Program: prog3.c

#include <stdio.h>

int main()
{
printf("\t\t**** C PROGRAM TO GENERATE THE FIBONACCI SERIES UP TO NTH TERM
****\n\n");

int a, b, c, i, terms;
printf("Enter number of terms: ");
scanf("%d", &terms);

a = 0;
b = 1;
c = 0;

printf("\n Fibonacci series up to %dth term is: \n",terms);


printf("\n");

for(i=1; i<=terms; i++)


{
printf("%d + ", c);

a = b;
b = c;
c = a + b;
}

printf("\b\b ");

return 0;
}

Assignment No. – III Page - 18 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

OUTPUT:

**** C PROGRAM TO GENERATE THE FIBONACCI SERIES UP TO NTH TERM ****

Enter number of terms: 24

Fibonacci series up to 24th term is:

0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55 + 89 + 144 + 233 + 377 + 610 + 987 + 1597 +


2584 + 4181 + 6765 + 10946 + 17711 + 28657
Process returned 0 (0x0) execution time : 7.229 s
Press any key to continue.

Assignment No. – III Page - 19 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

4.Write a Program in C to Reverse a Number & the Sum of the Digits of the given
Number.

Program: prog4.c

#include<stdio.h>

int main()
{

printf("\t\t**** C PROGRAM TO REVERSE A NUMBER & THE SUM OF THE DIGITS OF THE GIVEN
NUMBER ****\n\n");

int n, reverse=0, rem;


int sum = 0;
printf("Enter a number: ");
scanf("%d", &n);

while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
sum = sum + rem;
}
printf("\nSum of the digits = %d \n",sum);
printf("\nReverse of the number: %d",reverse);
return 0;
}

Assignment No. – III Page - 20 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

OUTPUT:

**** C PROGRAM TO REVERSE A NUMBER & THE SUM OF THE DIGITS OF THE GIVEN
NUMBER ****

Enter a number: 4563

Sum of the digits = 18

Reverse of the number: 3654


Process returned 0 (0x0) execution time : 7.903 s
Press any key to continue.

Assignment No. – III Page - 21 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

5. Write a Program in C to check whether a number is Prime or not.

Program: prog5.c

#include <stdio.h>
int main()
{
printf("\t\t**** C PROGRAM TO CHECK WHETHER A NUMBER IS PRIME OR NOT ****\n\n");

int n, remainder = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

for (int i = 2; i <= n / 2; i++)


{

if (n % i == 0)
{
remainder = 1;
break;
}
}

if (n == 1)
{
printf("1 is neither prime nor composite.");
}
else
{
if (remainder == 0)
{
printf("\n%d is a prime number.", n);
}

else
{
printf("\n%d is not a prime number.", n);
}

return 0;
}

Assignment No. – III Page - 22 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

OUTPUT:

**** C PROGRAM TO CHECK WHETHER A NUMBER IS PRIME OR NOT ****

Enter a positive integer: 97

97 is a prime number.
Process returned 0 (0x0) execution time : 8.344 s
Press any key to continue.

**** C PROGRAM TO CHECK WHETHER A NUMBER IS PRIME OR NOT ****

Enter a positive integer: 64

64 is not a prime number.


Process returned 0 (0x0) execution time : 11.969 s
Press any key to continue.

Assignment No. – III Page - 23 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

6. Write a Program in C to find the sum of all the Prime numbers between a given
range of numbers.

Program: prog6.c

#include <stdio.h>

int main()
{
printf("\t\t****C PROGRAM TO FIND THE SUM OF ALL THE PRIME NUMBERS BETWEEN A GIVEN
RANGE OF NUMBERS****\n\n");

int a, b, i, j, remainder;
int sum = 0;

printf("Enter lower bound of the interval: ");


scanf("%d", &a);

printf("\nEnter upper bound of the interval: ");


scanf("%d", &b);

printf("\nPrime numbers between %d and %d are: ", a, b);

for (i = a; i <= b; i++)


{
if (i == 1 || i == 0)
continue;

remainder = 1;

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


{
if (i % j == 0)
{
remainder = 0;
break;
}
}

if (remainder == 1)
{
printf("%d, ", i);
sum = sum + i;
}

}
Assignment No. – III Page - 24 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

printf("\b\b ");

printf("\n\nSum of all prime numbers between %d to %d = %d", a, b, sum);

return 0;
}

Assignment No. – III Page - 25 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

OUTPUT:

****C PROGRAM TO FIND THE SUM OF ALL THE PRIME NUMBERS


BETWEEN A GIVEN RANGE OF NUMBERS****

Enter lower bound of the interval: 15

Enter upper bound of the interval: 85

Prime numbers between 15 and 85 are: 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83

Sum of all prime numbers between 15 to 85 = 833


Process returned 0 (0x0) execution time : 9.809 s
Press any key to continue.

Assignment No. – III Page - 26 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

7. Write a Program in C to find the sum of all the Even and Odd numbers between
1 and 100.

Program: prog7.c

#include <stdio.h>

int main()
{
printf("\t\t****C PROGRAM TO FIND THE SUM OF ALL THE EVEN AND ODD NUMBERS BETWEEN
1 AND 100 ****\n\n");

int i, num = 100;


int odd_sum = 0, even_sum = 0;

printf("The even numbers between 1 and 100 are: ");

for (i = 1; i <= num; i++)


{
if (i % 2 == 0)
{
even_sum = even_sum + i;
printf("%d, ", i);
}
}
printf("\b\b ");

printf("\n\nSum of all even numbers between 1 and 100 = %d\n", even_sum);

printf("\n\nThe odd numbers between 1 and 100 are: ");

for (i = 1; i <= num; i++)


{
if (i % 2 != 0)
{
odd_sum = odd_sum + i;
printf("%d, ", i);
}

}
printf("\b\b ");

printf("\n\nSum of all odd numbers between 1 and 100 = %d\n\n", odd_sum);

return 0;
}

Assignment No. – III Page - 27 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

OUTPUT:

****C PROGRAM TO FIND THE SUM OF ALL THE EVEN AND ODD NUMBERS BETWEEN 1
AND 100 ****

The even numbers between 1 and 100 are: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30,
32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80,
82, 84, 86, 88, 90, 92, 94, 96, 98, 100

Sum of all even numbers between 1 and 100 = 2550

The odd numbers between 1 and 100 are: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31,
33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81,
83, 85, 87, 89, 91, 93, 95, 97, 99

Sum of all odd numbers between 1 and 100 = 2500

Process returned 0 (0x0) execution time : 0.080 s


Press any key to continue.

Assignment No. – III Page - 28 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

8. Write a Program in C o find & Print the Sum of all the Numbers Divisible by 7 within a
given Range.

Program: prog8.c

#include <stdio.h>

int main()
{
printf("\t\t**** C PROGRAM TO FIND THE SUM OF ALL THE NUMBERS DIVISIBLE BY 7
WITHIN A GIVEN RANGE OF NUMBERS ****\n\n");

int A, B, N = 7;
int sum = 0;

printf("Enter lower bound of the interval: ");


scanf("%d", &A);

printf("\nEnter upper bound of the interval: ");


scanf("%d", &B);

printf("\nThe numbers divisible by 7 between %d and %d are: ",A,B);

for (int i = A; i <= B; i++)


{
if (i % N == 0)
{
sum += i;
printf("%d , ",i);
}

}
printf("\b\b ");
printf("\n\nThe sum of all the numbers divisible by 7 between %d and %d = %d",A,B,
sum);

return 0;
}

Assignment No. – III Page - 29 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM
Programming for Problem Solving Lab (C)
(ESCS - 291)
CSE – 2nd Sem. – 1st Year

OUTPUT:

**** C PROGRAM TO FIND THE SUM OF ALL THE NUMBERS DIVISIBLE BY 7


WITHIN A GIVEN RANGE OF NUMBERS ****

Enter lower bound of the interval: 10

Enter upper bound of the interval: 85

The numbers divisible by 7 between 10 and 85 are: 14 , 21 , 28 , 35 , 42 , 49 , 56 , 63 ,


70 , 77 , 84

The sum of all the numbers divisible by 7 between 10 and 85 = 539


Process returned 0 (0x0) execution time : 20.134 s
Press any key to continue.

Assignment No. – III Page - 30 MD IMRAJUL ALAM, 20/CSE/002, </> CSE Dept, FIEM

You might also like