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

Heaven’s Light is Our Guide

Rajshahi University of Engineering & Technology


Department of Electrical & Electronic Engineering

Assignment on Computer Programming

Course No.: CSE 1111


Course Title: Computer Programming

Submission Date: 29/11/2023

Submitted by Submitted To
Name:SAYEM MAHMUD Mahit Kumar Paul
Roll: 2201019 Assistant Professor
Department of EEE Department of CSE
RUET, Kazla, Rajshahi-6204 RUET, Kazla, Rajshahi-6204
table of contents
SL problem statements PAGE
NO. NO.
01. write down a program that can take two numbers (a,b) as input and find out 3
the addition (a+b), subtraction(a-b), multiplication(a*b) and division(a/b) of
two numbers.

02. write down a program that can take the radius of a circle and find out its area 4
03. write down a program that can take the base and height of a triangle and then 5
find out its area.

04. write down a program that can take two numbers as input and then find out 6
which number is bigger

05. write down a program that can take three numbers as input and find out which 7
number is bigger

06. write down a program that can take two numbers as input and then find out 8
which number is bigger (without using if-else statement).
07. write down a program that can take three numbers as input and then find out 9
which number is bigger (without using if-else statement)

08. write down a program that can take a number as input and find out it is odd or 10
even
09. write down a program that can take a year as input and find out it is leap year 11
or not. (study about the leap year first).
10. write down a program that can take the values of a, b and c then find out the 12
value of x when ax2+bx+c=0

11. write down a program that can take two numbers and find out their difference. 13
the result shows the difference in a positive number

12. write down a program that can take a letter as input and then convert it to 14
uppercase if it is lowercase and vice versa
13. write down a program that can take a character as input and then convert it to 15
its ascii equivalent.

14 write down a program that can take marks (out of 100) as input and then show 16
the grade as output. (study about ruet grading system first, from your
syllabus).

Page 2 of 37
table of contents
SL problem statements PAGE
NO. NO.
15. find out the summation of following series (take n as a input): 18
1+2+3+…………………………………+n
16. write down a program that can take a lower bound l and an upper bound u and 18
then find out the summation of all odd numbers from l to u
17. write down a program that can take a lower bound l and an upper bound u and 19
then find out the summation of all even numbers from l to u.
18. write down a program that can take a lower bound l and an upper bound u and 20
then find out the summation of all numbers which are divisible by 3 from l to u
19. write down a program that can take n numbers as input and then find out their 20
average.
20. write down a program that can take n numbers as input and then find out the 21
average of those numbers which is greater than 10.
21. write down a program that can take n as input then generate the square of 22
numbers.
22. generate the following triangle. (star) 23
23. generate the following triangle.(number) 25
24. generate the following triangle 26
25. write down a program that can take a number as input and then reverse that 28
number ( do not use strrev() function)
26. write down a program that can take a number as input and then find out it is 29
prime or not;
27. write down a program that can take a lower bound l and an upper bound u and 30
then print the prime numbers from l to u
28. write down a program that can take n numbers as input and then find out the 31
gcd of n numbers
29. write down a program that can take n numbers as input and then find out the 32
lcm of n numbers.
30. write down a program that can take a lower bound l and an upper bound u and 34
find out the summation of all odd numbers using a function named sum, from l
to u.
31. write down a program that can take a lower bound l and an upper bound u and 35
find out the average of all even numbers using a function named average, from
l to u
/*01 write down a program that can take 2

numbers as input and find out the

addition (a+b), subtraction (a-b),

multiplication (a*b) and division (a/b)

of two numbers.*/

#include<stdio.h>

int main ()

Page 3 of 37
{

float a,b,w,x,y,z;

printf(“Enter a number:”);

scanf(“%f”,&a);

printf(“\nEnter another number:”);

scanf(“%f”,&b);

w=a+b;

x=a-b;

y=a*b;

z=a/b;

printf(“Addition is:%f\n”,w);

printf(“Subtraction is:%f\n”,x);

printf(“Multiplication is:%f\n”,y);

printf(“Divition is:%f\n”,z);

return 0;

input :Enter a number:8

Enter another number:4

output:

Addition is:12.000000

Subtraction is:4.000000

Multiplication is:32.000000

Divition is:2.000000

/*02 write down a program that can take the radius of a

circle and find out its area.*/

#include<stdio.h>

int main ()

float A,r,p=3.14;

printf(“Enter radius of the circle:”);

scanf(“%f”,&r);

Page 4 of 37
A=p*r*r;

printf(“Area is:%f”,A);

return 0 ;

input:

Enter radius of the circle:10

output:

Area is:314.000000

/*03 write down a program that can take the


base and height of a triangle and then find out its area.*/
#include<stdio.h>
int main()
{
float a,b;
float answer;

printf(“Height a=”);

scanf(“%f”,&a);

printf(“\nBase b=”);

scanf(“%f”,&b);

answer=.5 *a*b;

printf(“%f”,answer);

Page 5 of 37
}
input:
Height a=8

Base b=2
output:

16.000000
/*04.write down a program that can
take two numbers as input and then
find out which number is bigger.*/
#include<stdio.h>
int main ()
{
float m,n;
printf(“Enter first number:”);
scanf(“%f”,&m);
printf(“\nEnter second number:”);
scanf(“%f”,&n);

if(m>n)
{
printf(“Bigger number is:%f”,m);
}
else if(m<n)
{
printf(“Bigger number is:%f”,n);
}
else

Page 6 of 37
{
printf(“The numbers are equal!”);
}
return 0;
}
input:
Enter first number:8

Enter second number:4


output:
Bigger number is:8.000000

/*05 write down a program that can


take three numbers as input and
find out which number is bigger.*/
#include<stdio.h>
int main ()
{
float m,n,p;
printf(“Enter first number:”);
scanf(“%f”,&m);
printf(“\nEnter second number:”);
scanf(“%f”,&n);
printf(“\nEnter third number:”);
scanf(“%f”,&p);

if((m>n)&&(m>p))
{
printf(“Bigger number is:%f”,m);

Page 7 of 37
}
else if((n>p)&&(n>p))
{
printf(“Bigger number is:%f”,n);
}
else if((p>m)&&(p>n))
{
printf(“bigger number is:%f”,p);
}
else
{
printf(“The numbers are equal!”);
}
return 0;
}
input:
Enter first number:5
Enter second number:5
Enter third number:5
output:
The numbers are equal!

/*06 write down a program that can take two numbers


as input and then find out which number is bigger
(without using if-else statement).*/
#include<stdio.h>
int main ()
{
float x,y,z;

Page 8 of 37
printf(“Enter a number:”);
scanf(“%f”,&x);
printf(“\nEnter another number:”);
scanf(“%f”,&y);

z=x>y?x:y;
printf(“Bigger number is:%f”,z);
return 0;
}
input:
Enter a number:10
Enter another number:8
output:
Bigger number is:10.000000

/*07 write down a program that can take


three numbers as input and then find
out which number is bigger
(without using if-elsestatement).*/

#include<stdio.h>
int main()
{
float a,b,c,d;
printf(“Enter first number:”);
scanf(“%f”,&a);
printf(“\nEnter second number:”);

Page 9 of 37
scanf(“%f”,&b);
printf(“\nEnter third number:”);
scanf(“%f”,&c);
d=(a>b)?(a>c?a:c)b>c?b:c);
printf(“Biggest number is:%f”,d);
return 0;
}
input:
Enter first number:1

Enter second number:5

Enter third number:9


output:
biggest number is:9.000000

/*08 write down a program that can take a number as input and find out it is odd or
even.*/

#include<stdio.h>
int main ()
{
int m;
printf(“Enter a number:”);
scanf(“%d”,&m);

if(m%2==0)
{
printf(“The number is Even”);
Page 10 of 37
}
else
{
printf(“The number is Odd”);
}
return 0 ;

}
input:
Enter a number:7
output:
The number is Odd
/*09 write down a program that can take a year as input and find out it is leap year or
not.*/
#include<stdio.h>
int main()
{
int x;
printf(“Enter any year:”);
scanf(“%d”,&x);
if(y%400==0)
{
printf(“Leap year”);
}
else if(y%4==0 && y%100!=0)
{
printf(“Leap year”);
}
else
{
Page 11 of 37
printf(“Not leap year”);
}
return 0 ;
}
input:
Enter any year:2001
output:
Not leapNotleap year

/*10 write down a program that can


take the values of a, b and c then
find out the value of x when ax²+bx+c=0.*/
#include<stdio.h>
#include<math.h>
int main ()
{
float a,b,c,d,m,n;
printf(“Enter the value of a:”);
scanf(“%f”,&a);
printf(“\nEnter the value of b:”);
scanf(“%f”,&b);
printf(“\nEnter the value of c:”);
scanf(“%f”,&c);
d=sqrt(b*b-4*a*c);
m=((-b+d)/2);
n=((-b-d)/2);
printf(“Value of d is:%f\n”,d);
printf(“First root is:%f\n”,m);
printf(“Second root is:%f”,n);

Page 12 of 37
return 0;
}
input:
Enter the value of a:2.5

Enter the value of b:4.6

Enter the value of c:2


output:
Value of d is:1.077033
First root is:-1.761483
Second root is:-2.838516
/*11 write down a program that can take two numbers and find out their difference. the
result shows the difference in a positive number.*/
#include<stdio.h>
int main()
{
float x,y;
printf(“Enter first number:”);
scanf(“%f”,&x);
printf(“\nEnter second number:”);
scanf(“%f”,&y);

if(x>y)
{
printf(“Difference is:%f”,x-y);
}
if(x>y)
{
printf(“Difference is:%f”,y-x);
Page 13 of 37
}
return 0 ;

}
input:
Enter first number:4
Enter second number:6
output:
difference is:2.000000

/*12 write down a program that can


take a letter as input and then convert
it to uppercase if it is lowercase and
vice versa.*/
#include<stdio.h>
int main ()
{
int a,b;
char m;
printf(“Enter a character:”);
scanf(“%c”,&m);
a=m+32;
b=m-32;
if(m>=65 && m<=90)
{
printf(“Lowercase is:%c”,a);
}
else if(y>=97 && y<=122)
{

Page 14 of 37
printf(“Uppercase is:%c”,b);
}
else
{
printf(“Not an Alphabet”);
}
return 0 ;

}
input:
Enter a character:X
output:
Lowercase is:x

/*13 write down a program that can take a character as input and then convert it to its
ascii equivalent.*/
#include<stdio.h>
int main()
{
char a;
printf(“Enter a character:”);
scanf(“%c”,&a);
printf(“The ASCII value is:%d”,a);
return 0;
}
input:
Enter a character:a
output:
The ASCII value is:97
Page 15 of 37
/*14 write down a program that can take marks (out of 100) as input and then show the
grade as output.*/
#include<stdio.h>
int main ()
{
float x;
printf(“Enter mark:”);
scanf(“%f”,&x);
if(x>100||x<0)
{
printf(“Invalid number”);
}
else if(x>=80 && x<=100)
{
printf(“CGPA is 4.00”);
}
else if (x>=75 && x<80)
{
printf(“CGPA is 3.75”);
}
else if (x>=70 && x<75)
{
printf(“CGPA is 3.50”);
}
else if (x>=65 && x<70)
{
printf(“CGPA is 3.25”);
}
else if (x>=60 && x<65)
{
Page 16 of 37
printf(“CGPA is 3.00”);
}
else if (x>=55 && x<60)
{
printf(“CGPA is 2.75”);
}
else if (x>=50 && x<55)
{
printf(“CGPA is 2.50”);
}
else if (x>=45 && x<50)
{
printf(“CGPA is 2.25”);
}
else if (x>=40 && x<45)
{
printf(“CGPA is 2.00”);
}
else
{
printf(“Fail”);
}
return 0 ;
}
input:
Enter mark:76
output:
CGPA is 3.75

Page 17 of 37
/*15 find out the summation of following series (take n as a input):1+2+3+………+n*/
#include<stdio.h>
int main()

{
int N,result;
scanf("%d",&N);

result=(n*(n+1))/2;

printf("%d",result);
}

Input-10
Output-55
/*16 write down a program that can
take a lower bound l and an upper
bound u and then find out the
summation of all odd numbers from l to u*/
#include<stdio.h>
int main ()
{
int low,h,sum=0;

scanf(“%d”,&low);
scanf(“%d”,&h);
for(low; low<=h; owl++)
{
if(l%2!=0)

Page 18 of 37
{
sum=sum+l;
}
}
printf(“sum is:%d”,sum);
return 0;
}
input:1,10
output:
sum is:25
/*17 write down a program that can take a lower bound l and an upper bound u and then
find out the summation of all even numbers from l to u.*/
#include<stdio.h>
int main ()
{
int l,u,sum=0;
scanf(“%d”,&l);
scanf(“%d”,&u);
for(l; l<=u; l++)
{
if(l%2==0)
{
sum=sum+l;
}
}
printf(“sum is:%d”,sum);
return 0;
}
input:1,10
output:
Page 19 of 37
sum is:30

/* 18 write down a program that can take a lower bound l and an upper bound u and then
find out the summation of all numbers which are divisible by 3 from l to u.*/
#include<stdio.h>
int main()
{
int l,u,sum=0;
scanf("%d",&l);
scanf("%d",&u);
while(l<=u)
{
if(l%3==0)
{
sum=sum+l;
}
l++;
}
printf("sum is:%d",sum);
return 0;
}

input:
1
9
output:
sum is:18
/*19 write down a program that can take n numbers as input and then find out their
average.*/
#include<stdio.h>

Page 20 of 37
int main()
{
int X,N,result1=0,number;

printf("number=");
scanf("%d",&N);

printf("input numbers=");
for(x=1;x<=n;x++){

scanf("%d",&number);

result1=num+result1;
}

float result=result1/(n+.0) ;

printf("average=%f",result);

}
number=3
input numbers=4,5,6
average=5.000000
/*20 write down a program that can take n numbers as input and then find out the
average of those numbers which is greater than 10.*/
#include<stdio.h>
int main()
{
int x=1,n,result1=0,number,count=0;
float result;
Page 21 of 37
printf("numbers=");
scanf("%d",&n);

while(x<=n){

scanf("%d",&number);

if(number>10){
result1=result1+number;
count++;

}
x++;
}
result=result1/(count+.0);

printf("%f",result);

Input-numbers=5
11,2,50,74,12
Output-37.250000
/*21 write down a program that can take n as input thenvgenerate the square of
numbers.sample input:3 sample output:
123
456
7 8 9*/

Page 22 of 37
#include<stdio.h>

int main ()
{
int row,col,n,x=1;
scanf(“%d”,&n);
for(row=1; row<=n; row++)
{
for(col=1; col<=n; col++)
{
printf(“%d”,x);
x++;
}
printf(“\n”);
}
}
input:
enter number:3
output:
123
456
789

/*22 generate the following triangle.


sample input: 5
sample output:
*
***

Page 23 of 37
*****
*******
*/

#include<stdio.h>
int main ()
{
int N,row,col;
scanf(“%d”,&N);

for(row=1; row<=N; row++)


{
for(col=1; col<=N-row; col++)
{
printf(“ “);
}
for(col=1; col<=2*row-1; col++)
{
printf(“*”,col);
}
printf(“\n”);
}
return 0;

}
input:
enter n:5
output:
*

Page 24 of 37
***
*****
*******
*********

/*23 generate the following triangle.

sample input: 5

sample output:
1
121
12321
1234321
123454321. */
#include<stdio.h>
int main ()
{
int r,c,n;
printf(“enter n:”);
scanf(“%d”,&n);
for(r=1; r<=n; r++)
{
for(c=1; c<=n-r; c++)
{
printf(“ “);
}
for(c=1; c<=r; c++)
{

Page 25 of 37
printf(“%d”,c);
}
for(c=r-1; c>=1; c--)
{
printf(“%d”,c);
}
printf(“\n”);

}
return 0 ;
}
input:
enter n:5
output:
1
121
12321
1234321
123454321
/*24 generate the following triangle.
sample input : 5
*/
#include<stdio.h>

int main()

int row,col,n;

Page 26 of 37
scanf(“%d”,&n);

for(row=1;row<=n;row++){

for(col=1;col<=n-row;col++){

printf(“ “);

for(col=1;col<=(2*x)-1;col++)

if(col%2!=0)

printf(“%d”,col);

for(col=(2*row-1)-1;col>=1;col--)

if(col%2!=0)

printf(“%d”,col);

Page 27 of 37
printf(“\n”);
}
}
input:5
output:

1
131
13531
1357531
135797531

/*25 write down a program that can take a number as input and then reverse that
number( do not use strrev() function).
sample input: 12345
sample output: 54321*/

#include<stdio.h>

int main()
{
int N,rev=0;
printf(“enter number:”);
scanf(“%d”,&N);
while (N!=0)
{
rev=rev*10+N%10;
N=N/10;
}
Page 28 of 37
printf(“reverse number is:%d”,rev);
return 0;
}
input:
enter number:12345
output:
reverse number is:54321

/*26 write down a program that can take a number as input and then find out it is prime
or not.*/
#include<stdio.h>
int main ()
{
int n,i,z=0;
scanf(“%d”,&n);
for(i=2; i<n; i++)
{
if(n%i==0)
{
z++;
break;
}

}
if(z==0)
{
printf(“prime number\n”);
}
else
{
Page 29 of 37
printf(“not a prime number”);
}
return 0 ;

}
input:11
output:
prime number
/*27 write down a program that can take a lower bound l and an upper bound u and then
print the prime numbers from l to u.*/
#include<stdio.h>
int main ()
{
int l,u,i,z,j;
scanf(“%d”,&l);
scanf(“%d”,&u);
for(i=l; i<=u; i++)
{
z=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
z++;
}
}
if(z==2)
{
printf(“prime number%d\n”,i);
}
Page 30 of 37
}

return 0 ;
}
input:
enter lower bound:2
enter upper bound:9
output:
prime number2
prime number3
prime number5
prime number7
/*28 write down a program that can take n numbers as
input and then find out the gcd of n numbers.*/

#include<stdio.h>
int main()
{
int i,j,z,a;
printf("how many numbers?\n");
scanf("%d",&z);

if(z>=2){

printf("number 1=\n"); //29


scanf("%d",&a);
for(i=2;i<=z;i++){
printf("number %d=\n",i);

Page 31 of 37
scanf("%d",&j);}

while(j!=0){
int end=a%j;

a=j;
j=end;
}
printf("%d",a);
}

else
printf(" z is less than 2 ");
}

Intput-6,12,18
Output-6

/*29 write down a program that can take n numbers as input and then find out the lcm of
n numbers.*/
#include<stdio.h>
int main() {
int x,z,y,num;

printf(“how many numbers?:”);

scanf(“%d”,&z);

if (z>=2) {

Page 32 of 37
printf(“number 1=\n”);

scanf(“%d”,&num);

for (x=2; x<=z; x++) {

printf(“number %d=\n”, x);

scanf(“%d”,&y);

int k=num,l=y;

while (l!= 0) {

int end=k%l;

k=l;

l=end;

num = (num*y)/k;

printf(“lcm=%d”,num);

} else {

Page 33 of 37
printf(“z is less than 2 “);

return 0;

}
input:
how many numbers?:3
number 1=
6
number 2=
12
number 3=
18
output:
lcm=18

/*30 write down a program that can take a lower bound l


and an upper bound u and find out the summation of all
odd numbers using a function named sum, from l to u.*/

#include<stdio.h>
int odd_mult(int l,int u){
int x=0;
for(l;l<=u;l++){
if( l%2 !=0){
x=x+l;

Page 34 of 37
}
}
return x;

int main()

{
int a ,b;

scanf("%d %d",&a,&b);
int Result=odd_mult(a,b);

printf("%d",Result);
}

Input-1
11
Output-36

/*31 write down a program that can take a lower bound l


and an upper bound u and find out the average of all
even numbers using a function named average, from l
to u.*/
#include<stdio.h>

int average(int l,int u){

Page 35 of 37
int sum=0,num=0; float average;
while(l<=u){
if( l%2 ==0){
sum=sum+l;
num++;

}
l++;
}

average=sum/num+.0;
return average;

int main()
{
int low ,high;
scanf("%d %d",&low,&high);
float result=average(low,high);

printf("%f",result);
}

Input- 1,21
Output-11.0000

Page 36 of 37
Page 37 of 37

You might also like