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

Computer: Assignment On Programming

Uploaded by

tahsin2011896
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Computer: Assignment On Programming

Uploaded by

tahsin2011896
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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: S.M.AJMAIN AHAMED Mahit Kumar Paul
RAFI Assistant Professor
Roll: 2201026 Department of CSE
Department of EEE RUET, Kazla, Rajshahi-6204
RUET, Kazla, Rajshahi-6204
1.write down a program that can be take two number a,b as a input
and find addition(a+b), subtraction(ab), multification(a*b),
divition(a/b) of two number
#include<stdio.h>
int main(){
char A;
int a,b,RESULT;
printf("enter the number1: ");
scanf("%d", &a);
printf("enter the number2: ");
scanf("%d", &b);
printf("enter the OPARATOR: ");
scanf("\n%c", &A);
if(A=='+'){
RESULT=a+b;
printf("RESULT IS :%d", RESULT);}
else if(A=='-'){
RESULT=a-b;
printf("RESUIT IS :%d", RESULT);}
else if(A=='*'){
RESULT=a*b;
printf("RESUIT IS :%d", RESULT);}
else if(A=='/'){
RESULT=a/b;
printf("RESUIT IS :%d", RESULT);}
return 0;
}
Ans.input enter the number1: 64
enter the number2: 45
enter the OPARATOR: -
output: RESUIT IS :19
2.write down a program that can be take the radius of a circle and find
out its Area.
#include<stdio.h>
float pi=3.14;
int main(){
float radius, area;
printf("enter the radius: ");
scanf("%f", &radius);
area=pi*radius*radius;
printf("REDIUS IS :%.3f", area);
return 0;
}
Ans.input enter the radius: 15
Output:REDIUS IS :706.500
3.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(){
int base,height;
float area;
printf("enter the base: ");
scanf("%d", &base);
printf("enter the height: ");
scanf("%d", &height);
area=.5*(base*height);
printf("REDIUS IS :%.3f", area);
return 0;
}
Ans.input :enter the base: 10
enter the height: 15
output :REDIUS IS :75.000
4. write down a program that can take two numbers as input then find
out which number is bigger.
#include<stdio.h>
int main(){
int a,b;
printf("enter the number1: ");
scanf("%d", &a);
printf("enter the number2: ");
scanf("%d", &b);
if(a>b){
printf("a is bigger");
}
else{
printf("b is bigger");
}
return 0;
}
Ans.input :enter the number1: 15
enter the number2: 16
output :b is bigger
5. write down a program that can take three numbers as input then
find out which number is bigger.
#include<stdio.h>
int main(){
int a,b,c;
printf("enter the a: ");
scanf("%d", &a);
printf("enter the b: ");
scanf("%d", &b);
printf("enter the c: ");
scanf("%d", &c);
if(a>b && a>c){
printf("a is bigger");
}
else if(b>a && b>c){
printf("b is bigger");
}
else{
printf("c is bigger");
}
return 0;
}
Ans.input:enter the a: 10
enter the b: 20
enter the c: 5
outputb is bigger

6. write down a program that can take two numbers as input then find
out which number is bigger (without using if else statement).
#include<stdio.h>
int main(){
int a,b;
printf("enter the a b: ");
scanf("%d%d", &a,&b);
(a>=b)?printf("%d is largest number", a):printf("%d is largest
number\n", b);
return 0;
}
Ans.input:enter the a b: 4 5
Output:5 is largest number
7. write down a program that can take three numbers as input then
find out which number is bigger (without using if else statement).
#include<stdio.h>
int main(){
int a,b,c,max;
printf("enter the a b c: ");
scanf("%d%d%d", &a,&b,&c);
max=a>b?(a>c?a:c):(b>c?b:c);
printf("%d is largest number\n", max);
return 0;
}
Ans.input: enter the a b c: 5 41 2
output41 is largest number
8. write down a program that can take three numbers as input then
find out it is odd or even.
#include<stdio.h>
int main(){
int a;
printf("enter the a: ");
scanf("%d", &a);
if(a%2==0){
printf("this is even number");
}
else {
printf("this is odd number");
}
return 0;}
Ans.input: enter the a: 10
Output:this is even number
9. . write down a program that can take a year as input then find out
it is leap year or not.
#include<stdio.h>
int main(){
int year;
printf("enter the year: ");
scanf("%d", &year);
if(year%400==0){
printf("this is leap year");
}
else if(year%100==0) {
printf("this is not leap year");
}
else if(year%4==0) {
printf("this is leap year");
}
else {
printf("this is not leap year");
}
return 0;
}
Ans.input: enter the year: 2023
Output:this is not leap year
10. . write down a program that can take the value of a,b and c then
find out the x when ax2+bx+c.
#include<math.h>
#include<stdio.h>
int main(){
double a,b,c,discriment,root1,root2,realpart,imgpart;
printf("enter the a,b and c :");
scanf("%lf%lf%lf",&a,&b,&c);
discriment=b*b-4*a*c;
if(discriment>0){
root1=(-b+sqrt(discriment))/(2*a);
root2=(-b-sqrt(discriment))/(2*a);
printf("root1=%.2lf and root2=%.2lf", root1,root2);
}
else if(discriment=0){
root1=root2=-b/(2*a);
printf("root1=root2=%.2lf",root1);
}
else{
realpart=-b/(2*a);
imgpart=sqrt(-discriment)/(2*a);
printf("root1=%.2lf+%.2lfi and root2=%.2lf-%.2lfi",
realpart,imgpart,realpart,imgpart);
}
return 0;
}
Ans.input: enter the a,b and c :1,-5,6
Output:root1=3.00 and root2=2.00
11. . write down a program that can take two numbers as input then
find out their difference. The result shows the difference in positive
number.
#include<stdio.h>
int main(){
int a,b,diff;
printf("enter the a,b: ");
scanf("%d%d", &a,&b);
if(a>b){
diff=a-b;
printf("difference is : %d",diff);
}
else{
diff=b-a;
printf("difference is : %d",diff);
}
return 0;
}
Ans.Input:enter the a,b: 8,20
Output:difference is : 12
12. . write down a program that can take a letter as input then convert
it to uppercase to if it is lowercase
#include<stdio.h>
int main(){
char a,b,ch;
int result;
printf("enter the letter: ");
scanf("%c", &ch);
if(ch>='a' && ch<='z'){
printf("the letter is upper: %c",ch-32);
}
else if(ch>='A' && ch<='Z'){
printf("the letter is lower: %c",ch+32);
}
else{
printf("this isn't a letter");
}
return 0;
}
Ans.input:enter the letter: L
Output:the letter is lower: l
13. . write down a program that can take a character as input then
convert to its ASCII equivalent.
#include<stdio.h>
int main(){
char a;
printf("enter the letter: ");
scanf("%c", &a);
printf("ASCII code is : %d",a);
return 0;
}
Ans.input:enter the letter: R
Output:ASCII code is : 82
14.write down a program that can take marks(out of 100) as input and
then show that the grade as output . study RUET grading system.
#include<stdio.h>
int main(){
int a;
printf("enter the mark: ");
scanf("%d", &a);
if(a<=100){
if(a>=80){
printf("A+");}
else if(a>=75 && a<80){
printf("A");}
else if(a>=70 && a<75){
printf("A-");}
else if(a>=65 && a<70){
printf("B+");}
else if(a>=60 && a<65){
printf("B");}
else if(a>=55 && a<60){
printf("B-");}
else if(a>=50 && a<55){
printf("C+");}
else if(a>=45 && a<50){
printf("C");}
else if(a>=40 && a<45){
printf("D");}
else if(a<40){
printf("A-");}
else{
printf("he is absent");}
}
return 0;
}
Ans.input:enter the mark: 64
Output:B
15.find out the summation of following series (take as a input)
1+2+3+………………………….+n.
#include<stdio.h>
int main(){
int i,N,sum=0;
printf("enter the last number:");
scanf("%d", &N);
for(i=1;i<=N;i=i+1){
sum=sum+i;}
printf("%d", sum);
return 0;
}
Ans.input: enter the last number:50
Output:1275
16. write down a program that can take a lower bound L and upper
bound U.and then find out the summation of all odd numbers from L
to U.
#include<stdio.h>
int main(){
int L,U,i,sum=0;
printf("enter the lower bound : ");
scanf("%d",&L);
printf("enter the upper bound : ");
scanf("%d",&U);
if(L>U){
printf("Invalid bounds. The lower bound should be less than or equal
to upper bound.\n");
return 1;
}
for(i=L;i<=U;i++){
if(i%2!=0){
sum=sum+i;
}
}
printf("sum of odd numbers from %d to %d is: %d\n", U,L,sum);
return 0;
}
Ans.input:enter the lower bound : 20
enter the upper bound : 50
output:sum of odd numbers from 50 to 20 is: 525
17. write down a program that can take a lower bound L and 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,i,sum=0;
printf("enter the lower bound : ");
scanf("%d",&L);
printf("enter the upper bound : ");
scanf("%d",&U);
if(L>U){
printf("Invalid bounds. The lower bound should be less than or equal
to upper bound.\n");
return 1;
}
for(i=L;i<=U;i++){
if(i%2==0){
sum=sum+i;
}
}
printf("sum of even numbers from %d to %d is: %d\n", U,L,sum);
return 0;
}
Ans.input:enter the lower bound : 20
enter the upper bound : 50
output:sum of even numbers from 50 to 20 is: 560
18. write down a program that can take a lower bound L and 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,i,sum=0;
printf("enter the lower bound : ");
scanf("%d",&L);
printf("enter the upper bound : ");
scanf("%d",&U);
for(i=L;i<=U;i++){
if(i%3==0){
sum=sum+i;
}
}
printf("avg of numbers is: %d\n", U,L,sum);
return 0;
}
Ans.input:enter the lower bound : 20
enter the upper bound : 50
output:avg of numbers is: 50
19. write down a program that can take n numbers as input then find
out their average.
#include<stdio.h>
int main(){
int i,n;
float sum=0,avg;
printf("enter the number : ");
scanf("%d",&n);
for(i=1;i<=n;i++){
sum=sum+i;
printf("%d ",i);
}
avg=sum/n;
printf("Avarage is: %.2f\n", avg);
return 0;
}
Ans.input:enter the number : 20
OUTPUT:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Avarage is: 10.50
20. write down a program that can take n numbers as input then find
out their average of those numbers which is greater than 10.
#include<stdio.h>
int main(){
int n,i,num;
float sum=0,count=0,avg;
printf("enter the value of n : ");
scanf("%d",&n);
printf("Enter %d value of n:",n);
for(i=0;i<n;++i){
scanf("%d",&num);
if(num>10){
sum+=num;
count++;}
}
if(count>0){
avg=sum/count;
printf("Avarage of numbers grater than 10:%.2f\n",avg);
}
else{
printf("no number grater than 10\n");
}
return 0;
}
Ans.input:enter the value of n : 5
Enter 5 value of n:10 20 30 40 50
Output:Avarage of numbers grater than 10:35.00
21. write down a program that can take n numbers as input then
generate the square of numbers.
Sample Input:3
Sample Output:1 2 3
456
789
#include<stdio.h>
int main(){
int n,i,j,count=1;
printf("enter the value of n : ");
scanf("%d",&n);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%d ",count);
count++;
}
printf("\n");
}
return 0;
}
Ans.input: enter the value of n : 3
Output:1 2 3
456
789

22.generate the following triangle.


Sample Input:5
Sample Output: *
***
*****
*******
*********
#include<stdio.h>
int main(){
int n,i,j,k;
printf("enter the value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=n-i;j++){
printf(" ");
}
for(k=1;k<=2*i-1;k++){
printf("*");
}
printf("\n");}
return 0;
}
Ans.input:enter the value of n : 5
Output:
*
***
*****
*******
*********
23. Sample Input:5
Sample Output: 1
121
12321
1234321
123454321
#include<stdio.h>
int main(){
int rows,i,j,k;
printf("enter the value of rows : ");
scanf("%d",&rows);
for(i=1;i<=rows;i++){
for(j=1;j<=rows-i;j++){
printf(" ");
}
for(k=1;k<=i;k++){
printf("%d",k);
}
for(k=i-1;k>=1;k--){
printf("%d",k);
}
printf("\n");}
return 0;
}
Ans.input: enter the value of rows : 5
Output:
1
121
12321
1234321
123454321
24. Sample Input:5
Sample Output: 1
131
13531
1357531
135797531

#include <stdio.h>
int main() {
int n ;
printf("enter the value of n : ");
scanf("%d",&n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
for (int k = 1; k <= 2 * i - 1; k += 2) {
printf("%d", k);
}
for (int l = 2 * (i - 1) - 1; l >= 1; l -= 2) {
printf("%d", l);
}
printf("\n");
}
return 0;
}
Ans.input:enter the value of n : 5
Output:
1
131
13531
1357531
135797531
25.write down a program that can take a number as input then
reverse that number.
Sample Input:12345
Sample Output: 54321
#include<stdio.h>
int main(){
int num=1,n,i,j,k;
printf("enter the value of n : ");
scanf("%d",&n);
for(i=1;i<n;i++){
scanf("%d ",&i);}
printf("reversed order : ");
for(i=5;i>=1;i--){
printf("%d ",i);
}
return 0;
}
Ans.input:enter the value of n : 1 2 3 4 5
output:reversed order : 5 4 3 2 1
26 .write down a program that can take a number as input then find
out it is prime or not.
#include<stdio.h>
int isprime(int num){
if(num<=1){
return 0;
}
for(int i=2;i*i<=num;i++){
if(num%i==0){
return 0;
}
}
return 1;
}
int main(){
int num;
printf("enter a number : ");
scanf("%d",&num);
if(isprime(num)){
printf("%d is a prime number\n", num);
}
else {
printf("%d is a not prime number\n", num);
}
return 0;
}
Ans.input:enter a number : 11
Output:11 is a prime number
27. write down a program that can take a lower bound L and upper
bound U.and then print prime numbers from L to U.
#include<stdio.h>
int isprime(int num){
if(num<=1){
return 0;
}
for(int i=2;i*i<=num;i++){
if(num%i==0){
return 0;
}
}
return 1;
}
int main(){
int i,L,U;
printf("enter a lower bound : ");
scanf("%d",&L);
printf("enter a upper bound : ");
scanf("%d",&U);
printf("prime numbers between %d to %d are : ",L,U);
for(i=L;i<=U;i++){
if(isprime(i)){
printf("%d ", i);
}
}
return 0;
}
Ans.input:enter a lower bound : 20
Output:enter a upper bound : 50
prime numbers between 20 to 50 are : 23 29 31 37 41 43 47
28.write down a program that can take n numbers as input then find
out the GCD of n numbers.
#include<stdio.h>
int findGCD(int a,int b){
while(b!=0){
int temp=b;
b=a%b;
a=temp;
}
return a;
}
int main(){
int i,n,num,gcd;
printf("enter the number of elements : ");
scanf("%d",&n);
printf("enter a number1 : ");
scanf("%d",&gcd);
for(i=2;i<=n;i++){
printf("enter number %d: ", i);
scanf("%d",&num);
gcd=findGCD(gcd,num);
}
printf("the GCD is: %d\n", gcd);
return 0;
}
Ans.input:enter the number of elements : 5
enter a number1 : 4
enter number 2: 12
enter number 3: 16
enter number 4: 20
enter number 5: 24
output:the GCD is: 4
29. write down a program that can take n numbers as input then find
out the LCM of n numbers.
#include<stdio.h>
int findGCD(int a,int b){
while(b!=0){
int temp=b;
b=a%b;
a=temp;
}
return a;
}
int findLCM(int a,int b){
return(a*b)/findGCD(a,b);
}
int main(){
int i,n,num,lcm;
printf("enter the number of elements : ");
scanf("%d",&n);
printf("enter a number1 : ");
scanf("%d",&lcm);
for(i=2;i<=n;i++){
printf("enter number %d: ", i);
scanf("%d",&num);
lcm=findLCM(lcm,num);
}
printf("the LCM is: %d\n", lcm);
return 0;
}
Ans.input:enter the number of elements : 5
enter a number1 : 2
enter number 2: 4
enter number 3: 6
enter number 4: 8
enter number 5: 10
output:the LCM is: 120
30. write down a program that can take a lower bound L and upper
bound U.and then find out the summation of all odd numbers using a
function sum,from L to U.
#include <stdio.h>
int sum(int lower, int upper) {
int result = 0;
for (int i = lower; i <= upper; i++) {
if (i % 2 != 0) {
result += i;
}
}
return result;
}

int main() {
int lower, upper;
printf("Enter the lower bound (L): ");
scanf("%d", &lower);

printf("Enter the upper bound (U): ");


scanf("%d", &upper);
if (lower > upper) {
printf("Error: Lower bound should be less than or equal to upper
bound.\n");
return 1;
}

int result = sum(lower, upper);


printf("Sum of odd numbers from %d to %d is: %d\n", lower, upper,
result);
return 0;
}
Ans.input:Enter the lower bound (L): 20
Enter the upper bound (U): 50
Output:Sum of odd numbers from 20 to 50 is: 525
31. write down a program that can take a lower bound L and upper
bound U.and then find out the average of all even numbers using a
function average,from L to U.
#include <stdio.h>
float calculateAverage(int L, int U) {
int sum = 0, count = 0;
for (int i = L; i <= U; i++) {
if (i % 2 == 0) {
sum += i;
count++;
}
}

if (count == 0) {
printf("No even numbers in the specified range.\n");
return 0;
}

return (float)sum / count;


}

int main() {
int lowerBound, upperBound;

printf("Enter lower bound: ");


scanf("%d", &lowerBound);

printf("Enter upper bound: ");


scanf("%d", &upperBound);

float result = calculateAverage(lowerBound, upperBound);


printf("Average of even numbers from %d to %d is: %.2f\n",
lowerBound, upperBound, result);

return 0;}
Ans.input:Enter lower bound:20
Enter upper bound:50
Output:Average of even numbers from 20 to 50 is : 35.00

You might also like