2201018

You might also like

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

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: Bhuban Dabonath Mahit Kumar Paul
Roll: 2201018 Assistant Professor
Department of EEE Department of CSE
RUET, Kazla, Rajshahi-6204 RUET, Kazla, Rajshahi-6204

1
Table of Contents

SI. Problem Statements Page


No.
01 Write down a program that can take two numbers (a,b) as input and find out the addition
(a+b), subtraction(a-b), multiplication(a*b) and division(a/b) of two numbers. 04

02 Write down a program that can take the radius of a circle and find out its Area. 05
03 Write down a program that can take the base and height of a triangle and then find out its 05
Area.
04 Write down a program that can take two numbers as input and then find out which 06
number is bigger.
05 Write down a program that can take three numbers as input and find out which number 07
is bigger.
06 Write down a program that can take two numbers as input and then find out which 08
number is bigger (without using if-else statement).
07 Write down a program that can take three numbers as input and then find out which 09
number is bigger (without using if-else statement).
08 Write down a program that can take a number as input and find out if it is odd or even. 10
09 Write down a program that can take a year as input and find out if it is leap year or not. 11
10 Write down a program that can take the values of a, b and c then find out the value of x 12
when ax2+bx+c=0.
11 Write down a program that can take two numbers and find out their difference. The 13
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 uppercase if it 14
is lowercase and vice versa.
13 Write down a program that can take a character as input and then convert it to its ASCII 15
equivalent.
14 Write down a program that can take marks (out of 100) as input and then show the grade 16
as output.
15 Find out the summation of following series (take n as an input): 18
1+2+3+…………………………………+n

16 Write down a program that can take a lower bound L and an upper bound U and then 19
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 then 20
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 then 21
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 average. 22
20 Write down a program that can take n numbers as input and then find out the average of 23
those numbers which greater than 10.

2
21 Write down a program that can take n as input then generate the square of numbers.
Sample Input: 3
Sample Output: 1 2 3 24
456
789
22 Generate the following triangle.
Sample Input: 5
Sample Output: *
*** 25
*****
*******
*********
23 Generate the following triangle.
Sample Input: 5
Sample Output: 1
121 26
12321
1234321
123454321
24 Generate the following triangle.
Sample Input: 5
Sample Output: 1
131 28
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). 29
Sample Input: 12345
Sample Output: 54321
26 Write down a program that can take a number as input and then find out it is prime or 30
not
27 Write down a program that can take a lower bound L and an upper bound U and then 31
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 GCD of n 32
numbers.
29 Write down a program that can take n numbers as input and then find out the LCM of n 33
numbers.
30 Write down a program that can take a lower bound L and an upper bound U and find out 34
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 find out 35
the average of all even numbers using a function named average, from L to U.

3
Program-01: Write down a program that can take two numbers (a,b) as input and
find out the addition (a+b), subtraction(a-b), multiplication(a*b) and division(a/b)
of two numbers
Program:
#include<stdio.h>
int main()
{
float a,b,add,sub,mul,div;
printf("Enter the value of a and b :");
scanf("%f%f",&a,&b);
add=a+b;
sub=a-b;
mul=a*b;
div=a/b;
printf("Addition of %.2f and %.2f is %.2f.\n",a,b,add);
printf("Substruction of %.2f and %.2f is %.2f.\n",a,b,sub);
printf("Multiplication of %.2f and %.2f is %.2f.\n",a,b,mul);
printf("Division of %.2f and %.2f is %.2lf.\n",a,b,div);
return 0;
}
Input: Enter the value of a and b :30 12
Output: Addition of 30.00 and 12.00 is 42.00.
Substruction of 30.00 and 12.00 is 18.00.
Multiplication of 30.00 and 12.00 is 360.00.
Division of 30.00 and 12.00 is 2.50.

4
Program-02: Write down a program that can take the radius of a circle and find out its Area.
Program:
#include<stdio.h>
int main()
{
int r;
float area;
printf("Enter the radius :");
scanf("%d",&r);
area=3.1416*r*r;
printf("Area of the circle is %.2lf",area);
return 0;
}

Input: Enter the radius :5


Output: Area of the circle is 78.54

Program-03: Write down a program that can take the base and height of a triangle and then find out its Area.
Program:
#include<stdio.h>
int main()
{
int base, height;
float Area;
printf("Enter the base and height :");
scanf("%d%d",&base,&height);
Area=0.5*base*height;

5
printf("Area of the circle is %.2lf",Area);
return 0;
}

Input: Enter the base and height :8 12


Output: Area of the circle is 48.00

Program-04: Write down a program that can take two numbers as input and then find out which number is
bigger.

Program:
#include<stdio.h>
int main()
{
int num1,num2,big;
printf("Enter two number :");
scanf("%d%d",&num1,&num2);
big=num1;
if(num2>num1)
{
big=num2;
printf("%d is bigger among %d and %d",big,num1,num2);
}
else
printf("%d is bigger among %d and %d",big,num1,num2);
return 0;
}

Input: Enter two number :234 532


Output: 532 is bigger among 234 and 532

6
Program-05: Write down a program that can take three numbers as input and find out which
number is bigger.

Program:
#include<stdio.h>
int main()
{
int num1,num2,num3,big;
printf("Enter three number :");
scanf("%d%d%d",&num1,&num2,&num3);
big=num1;
if(num3>num1)
{
big=num3;
printf("%d is bigger among %d, %d and %d",big,num1,num2,num3);
}
else if(num2>num1)
{
big=num2;
printf("%d is bigger among %d, %d and %d",big,num1,num2,num3);
}
else
printf("%d is bigger among %d, %d and %d",big,num1,num2,num3);
return 0; }
Input: Enter three number :232 654 45
Output: 654 is bigger among 232, 654 and 45

7
Program-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).

Program:
#include<stdio.h>
int main()
{
int num1,num2;
printf("Enter two number :");
scanf("%d%d",&num1,&num2);
num1>num2?printf("%d is bigger.",num1):printf("%d is bigger.",num2);
return 0;
}

Input: Enter two number :23 32


Output: 32 is bigger.

8
Program-07: Write down a program that can take three numbers as input and then find
out which number is bigger (without using if-else statement).
Program:
#include<stdio.h>
int main()
{
int num1,num2,num3,max;
printf("Enter three number :");
scanf("%d%d%d",&num1,&num2,&num3);
max=num1>num2?num1:num2;
max>num3?printf("%d is bigger",max):printf("%d is bigger",num3);
return 0;
}
Input: Enter three number :12 43 32
Output: 43 is bigger

9
Program-08: Write down a program that can take a number as input and find out it is
odd or even.
Program:
#include<stdio.h>
int main()
{
int num;
printf("Enter a number :");
scanf("%d",&num);
if(num % 2 ==0)
{
printf("%d in an even number.",num);
}
else
{
printf("%d in an odd number.",num);
}
return 0;
}
Input: Enter a number :235
Output: 235 in an odd number

10
Program-09: Write down a program that can take a year as input and find out if it is leap year
or not.

Program:
#include<stdio.h>
int main()
{
int year;
printf("Enter year :");
scanf("%d",&year);
if(year%400==0)
printf("%d is a leap year.\n",year);
else if (year%4==0 && year%100!=0)
printf("%d is a leap year.",year);
else
printf("%d is not a leap year.",year);
return 0;
}
Input: Enter year :1234
Output: 1234 is not a leap year.

11
Program-10: Write down a program that can take the values of a, b and c then find out
the value of x when ax2+bx+c=0.

Program:
#include<stdio.h>
int main()
{
int a,b,c;
float X1,X2;
printf("Enter the value of a,b and c :");
scanf("%d%d%d",&a,&b,&c);

X1=(-b+sqrt(b*b-4*a*c))/2*a;
X2=(-b-sqrt(b*b-4*a*c))/2*a;

printf("The of x is %.2lf and %.2lf",X1,X2);


}

Input: Enter the value of a,b and c :1 5 6


Output: The of x is -2.00 and -3.00

12
Program-11: Write down a program that can take two numbers and find out their
difference. The result shows the difference in a positive number

Program:
#include<stdio.h>
int main()
{
int num1,num2,diff;
printf("Enter two number :");
scanf("%d%d",&num1,&num2);
if(num1>num2)
{
diff=num1-num2;
}
else
{
diff=num2-num1;
}
printf("Difference of %d and %d is %d",num1,num2,diff);
return 0;
}

Input: Enter two number :12 23


Output: Difference of 12 and 23 is 11

13
Program-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.

Program:
#include<stdio.h>
int main()
{
char ch;
printf("Enter character :");
scanf("%c",&ch);
if('A'<=ch && 'Z'>=ch)
{
printf("Uppercase of %c is %c.",ch,ch+32);
}
else if('a'<= ch && 'z'>=ch)
{
printf("Lowercase of %c is %c.",ch,ch-32);
}
else
{
printf("Not a letter.");
}
}

Input: Enter character :J


Output: Uppercase of J is j.

14
Program-13: Write down a program that can take a character as input and then convert
it to its ASCII equivalent.

Program:
#include<stdio.h>
int main()
{
char ch;
printf("Enter character :");
scanf("%c",&ch);
printf("ASCII equivqlent of %c is %d",ch,ch);
return 0;
}

Input: Enter character :G


Output: ASCII equivqlent of G is 71

15
Program-14: Write down a program that can take marks (out of 100) as input and then
show the grade as output.

Program:
#include<stdio.h>
int main()
{
int mark;
printf("Enter mark :");
scanf("%d",&mark);
if(mark<=100 && mark>=80)
{
printf("Your grade is 'A+'");
}
else if(mark<=79 && mark>=75)
{
printf("Your grade is 'A'");
}
else if(mark<=74 && mark>=70)
{
printf("Your grade is 'A-'");
}
else if(mark<=69 && mark>=65)
{
printf("Your grade is 'B+'");
}
else if(mark<=64 && mark>=60)

16
{
printf("Your grade is 'B'");
}
else if(mark<=59 && mark>=55)
{
printf("Your grade is 'B-'");
}
else if(mark<=54 && mark>=50)
{
printf("Your grade is 'C+'");
}
else if(mark<=49 && mark>=45)
{
printf("Your grade is 'C'");
}
else if(mark<=44 && mark>=40)
{
printf("Your grade is 'D'");
}
else if(mark<40)
{
printf("You Failed");
}
else
printf("Invalid mark");
return 0;

17
}

Input: Enter mark :67


Output: Your grade is 'B+'

Program-15: Find out the summation of following series (take n as a input):


1+2+3+…………………………………+n

Program:
#include<stdio.h>
int main()
{
int num,sum=0;
printf("Enter number :");
scanf("%d",&num);
for(int i=1;i<=num;i++)
{
sum=sum+i;
}
printf("Summation is %d",sum);
return 0;
}

Input: Enter number :100


Output: Summation is 5050

18
Program-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.

Program:
#include<stdio.h>
int main()
{
int L,U,sum=0;
printf("Enter value of L and U :");
scanf("%d%d",&L,&U);

for(int i=L;i<=U;i++)
{
if(i %2 !=0)
{
sum=sum+i;
}
}
printf("Summation is %d",sum);
return 0;
}

Input: Enter value of L and U :1 100


Output: Summation is 2500

19
Program-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.

Program:
#include<stdio.h>
int main()
{
int L,U,sum=0;
printf("Enter value of L and U :");
scanf("%d%d",&L,&U);

for(int i=1;i>=L && i<=U;i++)


{
if(i %2 ==0)
{
sum=sum+i;
}
}
printf("Summation is %d",sum);
return 0;
}
Input: Enter value of L and U :1 100
Output: Summation is 2550

20
Program18: 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

Program:
#include<stdio.h>
int main()
{
int L,U,sum=0;
printf("Enter value of L and U :");
scanf("%d%d",&L,&U);
for(int i=1;i>=L && i<=U;i++)
{
if(i %3 ==0)
{
sum=sum+i;
}
}
printf("Summation is %d",sum);
return 0;
}

Input: Enter value of L and U :1 20


Output: Summation is 63

21
Program-19: Write down a program that can take n numbers as input and then find out
their average.

Program:
#include<stdio.h>
int main()
{
float n,sum=0,avg;
printf("Enter number :");
scanf("%f",&n);
for(int i=1;i<=n;i++)
{
sum=sum+i;
avg=sum/n;
}
printf("Average is %.2lf",avg);
return 0;
}
Input: Enter number :100
Output: Average is 50.50

22
Program-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.
Program:
#include<stdio.h>
int main()
{
float n,sum=0,avg;
printf("Enter number :");
scanf("%f",&n);

for(int i=1;i<=n;i++)
{
if(i>10)
{
sum=sum+i;
avg=sum/(n-10);
}
}
printf("Average is %.2lf",avg);
return 0;
}
Input: Enter number :20
Output: Average is 15.50

23
Program-21: Write down a program that can take n as input then generate the square of
numbers.
Program:
#include<stdio.h>
int main()
{
int n,i,j,a=1;
printf("Enter number :");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a++);
printf("\n");
}
return 0;
}
Input: Enter number :3
Output: 1 2 3
456
789

24
Program-22: Generate the following triangle.
Sample Input: 5
Sample Output: *
***
*****
*******
*********

Program:
#include<stdio.h>
int main()
{
int n,i,j;
printf("Enter number :");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(j=1;j<=2*i-1;j++)
printf("*");
printf("\n");
}
}

Input: Enter number : 5

25
Output: *
***
*****
*******
*********
Program-23: Generate the following triangle.
Sample Input: 5
Sample Output: 1
121
12321
1234321
123454321
Program:
#include<stdio.h>
int main()
{
int n,i,j;
printf("Enter n :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(j=1;j<=i;j++)
{
printf("%d",j);

26
}
for(j=i-1;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
}
}
Input: Enter number :5
Output: 1
121
12321
1234321
123454321

27
Program-24: Generate the following triangle.
Sample Input : 5
Sample Output: 1
131
13531
1357531
135797531
Program:
#include <stdio.h>
int main()
{
int i,j,n;
printf("Enter number : ");
scanf("%d",&n);
for(i=1; i<=2*n; i+=2)
{
for(j=1; j<2*n-i; j+=2)
printf(" ");
for(j=1; j<=i; j+=2)
printf("%d",j);
for(j=i-2; j>=1; j-=2)
printf("%d",j);
printf("\n");
}
return 0;
}

28
Input: Enter number : 5
Output: 1
131
13531
1357531
135797531

Program-25: Write down a program that can take a number as input and then reverse
that number ( do not use strrev() function).
Program:
#include<stdio.h>
int main()
{
int num,rev=0;
printf("Enter number :");
scanf("%d",&num);
while(num>0)
{
rev=rev*10+num%10;
num=num/10;
}
printf("The reversed number is %d",rev);
}
Input: Enter number :8767563
Output: The reversed number is 3657678

29
Program-26: Write down a program that can take a number as input and then find out it
is prime or not.
Program:
#include<stdio.h>
int main()
{
int num,count=0,i;
printf("Enter the number :");
scanf("%d",&num);
for(i=2;i<num;i++)
{
if(num%i==0)
{
count++;
break;
}
}
if(count==0 )
printf("%d is a prime number.",num);
else
printf("%d is not a prime number.",num);
}
Input: Enter the number :53
Output: 53 is a prime number.

30
Program-27: Write down a program that can take the base and height of a triangle and
then find out its Area.
Program:
#include<stdio.h>
int main()
{
int L,U,count=0,i,j;
printf("Enter the value of L and U :");
scanf("%d%d",&L,&U);
for(i=L;i<U;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
{
count++;
break;
}
}
if(count==0 && i!=1)
printf("%d ",j);
count=0;
}
}
Input: Enter the value of L and U :5 55
Output: 5 7 11 13 17 19 23 29 31 37 41 43 47 53

31
Program-28: Write down a program that can take n numbers as input and then find out
the GCD of n numbers.
Program:
#include<stdio.h>
int main()
{
int a,b,min,i;
printf("Enter the value of a and b :");
scanf("%d%d",&a,&b);
min=a>b?a:b;
for(i=min;i<=min;i--)
{
if(a%i==0 && b%i==0)
break;
}
printf("GCD is %d",i);
}
Input: Enter the value of a and b :65 30
Output: GCD is 5

32
Program-29: Write down a program that can take n numbers as input and then find out
the LCM of n numbers.
Program:
#include<stdio.h>
int main()
{
int a,b,max,i;
printf("Enter the value of a and b :");
scanf("%d%d",&a,&b);
max=a>b?a:b;
for(i=max;i>=max;i++)
{
if(i%a==0 && i%b==0)
break;
}
printf("LCM is %d",i);
}
Input: Enter the value of a and b :13 5
Output: LCM is 65

33
Program-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.
Program:
#include<stdio.h>
int sum(int a, int b)
{
int sum;
for(int i=a;i<=b;i++)
{
if(i %2 !=0)
sum=sum+i;
}
return sum;
}
int main()
{
int L,U,add=0;
printf("Enter value of L and U :");
scanf("%d%d",&L,&U);
add=sum(L,U);
printf("Summation is %d",add);
return 0;
}
Input: Enter value of L and U :1 20
Output: Summation is 100

34
Program-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.
Program:
#include<stdio.h>
int avg(int a, int b)
{
int sum,avg;
for(int i=a;i<=b;i++)
{
if(i %2 ==0)
sum=sum+i;
avg=2*sum/((b-a)+1);
}
return avg;
}
int main()
{
int L,U;
float av=0;
printf("Enter value of L and U :");
scanf("%d%d",&L,&U);
av=avg(L,U);
printf("Average is %.2lf",av);
return 0;

35
}
Input: Enter value of L and U :1 20
Output: Average is 11.00

36

You might also like