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

C Programming by RMP Sir

Uploaded by

Tamanna Islam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

C Programming by RMP Sir

Uploaded by

Tamanna Islam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

1

1. A C Program with Algorithm and Flow Chart to add two numbers.


Algorithm Flow Chart

Step- 1: Start
Step- 2: Input the values of a and b.
Step- 3: Process S = a + b.
Step- 4: Output S.
Step- 5: End

C Program with Full Explaination: Just C Program:


#include <stdio.h>
// Header file(s)
#include <stdio.h> int main()
{
int a, b, S;
int main() {
// Declare variables printf("Enter the first number: ");
scanf("%d", &a);
int a, b, S;
printf("Enter the second number: ");
// Prompt user for the first number scanf("%d", &b);
printf("Enter the first number: ");
S = a + b;
// Read the first number
scanf("%d", &a); printf("Sum: %d\n", S);

return 0;
// Prompt user for the second number }
printf("Enter the second number: ");
// Read the second number Output
scanf("%d", &b);
// Calculate the sum Enter the first number: 100
Enter the second number: 150
S = a + b;
Sum: 250

// Display the sum


printf("The sum of %d and %d is %d\n", a, b, S);

// return 0 command is fixed


return 0;
}

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
2

2. to 5: C Programs for subtraction, multiplication and division for


quotient and remainder.

Subtraction Multiplication
#include <stdio.h> #include <stdio.h>

int main() { int main() {


int a, b, Sub; int a, b, M;

printf("Enter first number: "); printf("Enter first number: ");


scanf("%d", &a); scanf("%d", &a);

printf("Enter second number: "); printf("Enter second number: ");


scanf("%d", &b); scanf("%d", &b);

Sub = a - b; M = a * b;

printf("Subtraction Result: %d\n", printf("Multiplication Result:


Sub); %d\n", M);

return 0; return 0;
} }

Determining Quotient by Division Determining Remainder by Division


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

int main() { int main() {


int a, b, Q; int a, b, R;

printf("Enter first number: "); printf("Enter first number: ");


scanf("%d", &a); scanf("%d", &a);

printf("Enter second number: "); printf("Enter second number: ");


scanf("%d", &b); scanf("%d", &b);

Q = a / b; R = a % b;

printf("Quotient: %d\n", Q); printf("Remainder: %d\n", R);

return 0; return 0;
} }

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
3

6. A C Program with Algorithm and Flow Chart to determine the


average of two numbers.

Algorithm Flow Chart

Step- 1: Start
Step- 2: Input values of a and b.
Step- 3: Process avg = (a + b) / 2.
Step- 4: Output avg.
Step- 5: End

C Program Output

#include <stdio.h> Enter first number: 12


Enter second number: 14
int main() { Average: 13
int a, b;
float avg;

printf("Enter first number: ");


scanf("%d", &a);

printf("Enter second number: ");


scanf("%d", &b);

avg = (a + b) / 2;

printf("Average: %f\n", avg);

return 0;
}

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
4

7. A C Program with Algorithm and Flow Chart to determine the


largest number between two.

Algorithm Flow Chart

Step- 1: Start
Step- 2: Read values of A and B.
Step- 3: A>B?
YES: A is larger
NO: B is larger
Step- 4: Display the result.
Step- 5: End

C Program Output

#include <stdio.h> Enter the value of A: 7


Enter the value of B: 5
int main() { A= 7 is larger
int A, B;

// Input values for A and B


printf("Enter the value of A: ");
scanf("%d", &A);
printf("Enter the value of B: ");
scanf("%d", &B);

// Compare A and B
if (A > B)
printf("A= %d is larger\n", A);
else
printf("B= %d is larger \n", B);

return 0;
}

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
5

8. A C program with Algorithm and Flow Chart to determine the area


of a triangle while base and height are given.

Algorithm Flow Chart

Step- 1: Start
Step- 2: Input values of l and h.
Step- 3: Process S = (l * h) / 2.
Step- 4: Output S.
Step- 5: End

C Program Output

#include <stdio.h> Enter the base of the triangle: 10


Enter the height of the triangle: 6
int main() { Area of the triangle: 30
float l, h, S;

printf("Enter the base of the triangle:


");
scanf("%f", &l);

printf("Enter the height of the triangle:


");
scanf("%f", &h);

S = (l * h) / 2;

printf("Area of the triangle: %f\n", S);

return 0;
}

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
6

9. A C Program with Algorithm and Flow Chart to determine the area


of a triangle while 3 sides are given.

Algorithm Flow Chart

Step- 1: Start
Step- 2: Read values of a, b, and c.
Step- 3: Process S = (a + b + c) / 2.
Step- 4: Process area = sqrt(s * (s - a) *
(s - b) * (s - c)).
Step- 5: Output area.
Step- 6: End

C Program Output
#include <stdio.h> Enter the first side of the triangle: 3
#include <math.h> Enter the second side of the triangle: 4
Enter the third side of the triangle: 5
int main() { Area of the triangle: 6
float a, b, c, s, area;

printf("Enter the first side of the triangle: ");


scanf("%f", &a);

printf("Enter the second side of the triangle: ");


scanf("%f", &b);

printf("Enter the third side of the triangle: ");


scanf("%f", &c);

s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));

printf("Area of the triangle: %f\n", area);

return 0;
}

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
7

10. A C Program with Algorithm and Flow Chart to determine the


largest number among three.

Algorithm Flow Chart

Step-1: Start.
Step- 2: Input a, b, c.
Step- 3: a>b?
YES: Go to step- 4.
NO: Go to step- 5.
Step- 4: a>c?
YES: Output a.
NO: Go to step- 5.
Step- 5: b>c?
YES: Output b.
NO: Output c.
Step- 6: End

C Program Output
#include <stdio.h> Enter the value of a: 5
Enter the value of b: 7
int main() Enter the value of c: 6
The largest number is b (7)
{
int a, b, c;

// Input values for a, b, and c


printf("Enter the value of a: ");
scanf("%d", &a);
printf("Enter the value of b: ");
scanf("%d", &b);
printf("Enter the value of c: ");
scanf("%d", &c);

// Determine the largest number


if (a > b && a >c)
printf("The largest number is a (%d)\n", a);
else if (b > c)
printf("The largest number is b (%d)\n", b);
else
printf("The largest number is c (%d)\n", c);

return 0;
}

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
8

11. & 12: C Programs for the conversion of temperature from Celsius to
Fahrenheit and vice-versa.
Algorithm Flow Chart

Step- 1: Start
Step- 2: Input Celsius value, c.
Step- 3: Process f = (9 * c) / 5 + 32.
Step- 4: Output f.
Step- 5: End

C Program for Celsius to Fahrenheit Output


#include <stdio.h>
Enter temperature in Celsius: 37
int main() { Temperature in Fahrenheit: 98.6
float c, f;

// Input the temperature in Celsius


printf("Enter temperature in Celsius: ");
scanf("%f", &c);

// Convert Celsius to Fahrenheit


f = (9 * c / 5) + 32;

// Display the result


printf("Temperature in Fahrenheit: %f\n", f);

return 0;
}
C Program Fahrenheit to Celsius Output
#include <stdio.h>
Temperature in Fahrenheit: 98
int main() { Enter temperature in Celsius: 36.67
float c, f;

// Input the temperature in Celsius


printf("Enter temperature in Celsius: ");
scanf("%f", &c);

// Convert Celsius to Fahrenheit


c = (f-32)*5/9;

// Display the result


printf("Temperature in Fahrenheit: %f\n", f);

return 0;
}

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
9

13. A C Program with algorithm and flow chart to determine a number


is either even or odd.

Algorithm Flow Chart

Step- 1: Start
Step- 2: Input A.
Step- 3: Process R= A mod 2;
Step- 4: R==0?
YES: A is Even.
NO: A is Odd.
Step- 5: Output result.
Step- 5: End

C Program Output

#include<stdio.h> Give the Number, A= 4


4 is even
int main( )
{
int A ;
printf(“Give the Number, A= ”)
scanf( “%d”,&A);
if (A%2==0)
printf(“%d is even”,A);
else
printf(“%d is odd”,B);

return 0;

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
10

14. A C Program with algorithm and flow chart to determine a number


is either positive or negative.

Algorithm Flow Chart

Step- 1: Start
Step- 2: Input N.
Step- 3: N>=0?
YES: N is Positive.
NO: N is Negative.
Step- 5: Output result.
Step- 5: End

C Program Output

#include<stdio.h> -5
-5 is negative
void main( )
{
int N ;
scanf( “%d”, &N);
if (N>=0)
printf(“%d is Positive”, N);
else
printf(“%d is Negative”, N);

return 0;

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
11

15. A C Program with algorithm and flow chart to determine the


HCF/GCF of two numbers.

Algorithm Flow Chart

Step- 1: Start.
Step- 2: Input a, b.
Step- 3: Process Z= a mod b.
Step- 4: Process a= b.
Step- 5: b= Z.
Step- 6: Z= 0?
YES: Output b.
NO: Return to Step- 4.
Step- 7: End.

C Program Output

#include <stdio.h> 12
8
int main() 4
{
int a, b, Z;
scanf("%d %d", &a, &b);

do
{
Z = a % b;
a = b;
b = Z;
}
while(b != 0);

printf("%d", b);

return 0;
}

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
12

16. A C Program with algorithm and flow chart to determine the LCM
of two numbers.

Algorithm Flow Chart

Step- 1: Start.
Step- 2: Input two integers, x and y.
Step- 3: Initialize a variable L with the
maximum value of x and y.
Step- 4: Check if L is divisible by both x
and y.
YES: L is the LCM of a and b,
and go to Step- 5.
NO: increment L by 1 and repeat
Step- 4.
Step- 5: Output the LCM.
Step- 6: End.

C Program Output
#include <stdio.h> Enter two integers: 12
5
int main() { The LCM of 12 and 5 is 60
int x, y, L, lcm;

// Input two integers


printf("Enter two integers: \n");
scanf("%d %d", &x, &y);

// Find the maximum of a and b


L = (x > y) ? x : y;

// Loop to find the LCM


while (1) {
if (L % a == 0 && L % b == 0) {
lcm = L;
break;
}
L++;
}

// Output the LCM


printf("The LCM of %d and %d is %d\n", x,
y, lcm);

return 0;
}

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
13

17. A C Program with algorithm and flow chart to determine the given
year is a Leap Year or Not.

Algorithm Flow Chart

Step- 1: Start.
Step- 2: Input Year, Y.
Step- 3: Y%400==0?
YES: Go to Step- 6.
NO: Go to Step- 4.
Step- 4: Y%100!=0 && Y%4==0?
YES: Go to Step- 5.
NO: Go to Step- 6.
Step- 5: Output “Not Leap Year”.
Step- 6: Output “Leap Year”.
Step- 7: End.

C Program Output

#include<stdio.h> 2000
2000 is Leap Year
int main( )

{
int Y ;
scanf(“%d”,&Y);
if(Y%400==0||(Y%100!=
0&&Y%4==0))
printf(“%d is Leap Year”, Y);
else
printf(“%d is Not Leap Year”, Y);
getch( );
}

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
14

18. A C Program to determine the area and the circumference of a


Circle.

C Program Output

#include<stdio.h> Enter the Radius of the Circle: 5


The Area of the Circle is 78.54
int main( ) The Circumference of the Circle is 31.416

{
float pi, r, area, c;
pi= 3.1416;
printf(“Enter the Radius of the Circle:
”);
scanf(“%f”, &r);
area= pi*r*r;
c= 2*pi*r;
printf(“The Area of the Circle is %f”,
area);
printf(“The Circumference of the
Circle is %f”, c);

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
15

19. A C Program with Algorithm and Flow Chart to Find the Series
1+2+3+……..+ N (upto any number)

Algorithm Flow Chart

Step- 1: Start.
Step- 2: Input the value of N.
Step- 3: Let, S= 0, i= 1.
Step- 4: Process S = S + i.
Step- 5: Process i= i+1.
Step- 6: i<=N?
YES: Go to Step- 4.
NO: Output S.
Step- 7: End

C Program Output

#include<stdio.h> Give the Number: 100


The Summation of integers upto 100
int main() is 5050
{
int N,S,i;
printf("Give the Number: ");
scanf("%d", &N);
S=0;
for(i=1; i<=N; i++)
S = S + i;
printf("The Summation of integers
upto %d is %d", N, S);

return 0;
}

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
16

20. A C Program to Find the Series 12+22+32+………... + N2 (upto any


number)

C Program Output

#include<stdio.h> Give the Number: 10


The Summation of the Squares of
int main() integers upto 10 is 385
{
int N,S,i;
printf("Give the Number: ");
scanf("%d",&N);
S=0;
for(i=1; i<=N; i++)
S = S + i * i;
printf("The Summation of the Squares of
integers upto %d is %d", N, S);

return 0;
}

21. A C Program to Find the Series 13+23+33+………...+ N3 (upto any number)

C Program Output

#include<stdio.h> Give the Number: 10


The Summation of the Squares of
int main() integers upto 10 is 385
{
int N,S,i;
printf("Give the Number: ");
scanf("%d",&N);
S=0;
for(i=1; i<=N; i++)
S=S + i * i;
printf("The Summation of the Squares of
integers upto %d is %d", N, S);

return 0;
}

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
17

22. A C Program to Find the Series 13+23+33+………...+ N3 (upto any number)

C Program Output

#include<stdio.h> Give the Number: 5


The Summation of the Cubes of integers
int main() upto 5 is 225
{
int N,S,i;
printf("Give the Number: ");
scanf("%d",&N);
S=0;
for(i=1; i<=N; i++)
S=S+i* i* i;
printf("The Summation of the Cubes of
integers upto %d is %d", N, S);

return 0;
}

23. A C Program to Find the Series 2+4+6+………...+ N (upto any number)

C Program Output

#include<stdio.h> Give the Number: 10


The Summation of the Even integers
int main() upto 10 is 30
{
int N,S,i;
printf("Give the Number: ");
scanf("%d",&N);
S=0;
for(i=0; i<=N; i=i+2)
S=S+i;
printf("The Summation of the Even
integers upto %d is %d", N, S);

return 0;
}

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
18

24. A C Program to Find the Series 2+4+6+………...+ N (upto any number)

C Program Output

#include<stdio.h> Give the Number: 11


The Summation of the Odd integers upto
int main() 11 is 36
{
int N,S,i;
printf("Give the Number: ");
scanf("%d",&N);
S=0;
for(i=1; i<=N; i=i+2)
S=S+i;
printf("The Summation of the Odd
integers upto %d is %d", N, S);

return 0;
}

25. A C Program for sorting the numbers as a triangle.

C Program Output
#include <stdio.h>
1
int main() 22
{ 333
int i, j; 4444
55555
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d ", i);
}
printf("\n");
}

return 0;
}

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
19

26. A C Program with Algorithm and Flow Chart to determine the


Factorial of any number.

Algorithm Flow Chart

Step- 1: Start.
Step- 2: Input the value of N.
Step- 3: Let, F= 1, i= 1.
Step- 4: Process F = F * i.
Step- 5: Process i= i+1.
Step- 6: i<=N?
YES: Go to Step- 4.
NO: Output F.
Step- 7: End

C Program Output

#include<stdio.h> Give the Number: 10


The Factorial of 10 is 3628800.000000
int main( )
{
int N, i;
double F;
printf(“Give the Number: ”);
scanf ("%d",&N);
F=1;
for(i=1;i<=N;i++)
F=F*i;
printf ("The Factorial of %d is %lf", N,
F);

return 0;
}

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704
20

27. to 29: A C Program to give 1 to 100 as output.

Using for loop Using while loop

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

int main() int main()

{ {
int i; int i = 1;
for (i = 1; i <= 100; i++) while (i <= 100)
{ {
printf("%d\n", i); printf("%d\n", i);
} i++;
}
return 0;
return 0;
}
}

Using do-while loop Output

#include <stdio.h> 1
2
int main() 3
4
{ 5
int i = 1; 6
do { 7
printf("%d\n", i); 8
i++; 9
} 10
while (i <= 100); .
.
return 0; .
.
} 98
99
100

Engr. Rahat Mahmud Polin


01400-111222, 01917-337704

You might also like