Khulna University: Structured Programing Assignment, CSE

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

KHULNA UNIVERSITY

Structured Programing Assignment, CSE

SUBMITTED BY
MD.TOWHIDUL ISLAM
STUDENT_ID: 200913
ELECTRONICS AND COMMUNICATION ENGINEERING DISCIPLINE
Problem 1:Perform the following operations on an integer array
of 10 elements. Accept thevalues from the user.
a. Sort an array in ascending order.
Solution:
#include<stdio.h>
int main()
{
int i,j,a,num[50];

printf("Enter the numbers: \n");


for(i=0;i<10;++i)
scanf("%d",&num[i]);

for(i=0;i<10;i++)
{

for(j=i+1;j<10;j++)
{

if(num[i]>num[j])
{
a=num[i];
num[i]=num[j];
num[j]=a;
}
}

}
printf("Sorted elements of array in ascending order: ");
for(i=0;i<10;i++)
{
printf(" %d",num[i]);
}
return 0;

b. Display the sum of all odd values stored in an array.

Solution:
#include<stdio.h>

int main()
{
int i,num[50],sum=0;
for(i=0;i<10;i++)
{
scanf("%d",&num[i]);
}
for(i=0;i<10;i++)
{
if(num[i]%2!=0)
sum=sum+num[i];
}

printf("Summation of all odd values stored in the array : %d",sum);

return 0;
}

c. Display the number of even values stored in an array.

Solution:
#include<stdio.h>

int main()
{
int i, numbers[50], a=0;
for(i=0; i<10; i++)
{
scanf("%d", &numbers[i]);
}
for(i=0; i<10; i++)
{
if(numbers[i]%2==0)
a=a+1;
}
printf("The number of even values stored in an array : %d", a);
return 0;
}

Problem 2:Write a program to evaluate the net salary of an


employee given the followingconstraints:
Solution:
#include<stdio.h>
int main()
{
float salary, da, hra, ta, others, pf, it, net;
printf("Basic Salary = ");
scanf("%f", &salary);
printf("TA = ");
scanf("%f", &ta);
printf("HRA = ");
scanf("%f", &hra);
printf("Others = ");
scanf("%f", &others);
da = (12.0*salary)/100;
pf = (14.0*salary)/100;
it = (15.0*salary)/100;

net = salary+da+hra+ta+ others -( pf + it);

printf("The Net Salary of the employee is %.0f$", net);

return 0;
}

Problem 3: Write a program which prints the ASCII values of the


characters ‘A’ and ‘b’.

Solution:
#include<stdio.h>
int main()
{
printf("the ASCII value of the character A = %d\n and ", 'A');
printf("the ASCII value of the character b = %d\n ", 'b');

return 0;
}
Problem 4: Write a program to count the number of vowels in a
line of text.

Solution:
#include<stdio.h>
#include<string.h>

int main()
{
    char a[100], ch;
    int i, n, c = 0;
    scanf("%s", a);
    n = strlen(a);

    for(int i=0; i<n ; i++)


  {
        ch = a[i];
        if(ch=='a'|| ch=='e' || ch == 'i' || ch  == 'o' || ch == 'u' )
            c=c+1;
  }
    printf("The number of vowels in the text is %d", c);

    return 0;
}

Problem 5: Write a program that accepts the following


numbers in an array and reverses thearray.

Solution:
#include<stdio.h>
int main()
{
int a[50], n, i;
scanf("%d", &n);
for(i=0 ; i<n; i++)
{
scanf("%d", &a[i]);
}
printf("Reverse Array: ");
for(i=n-1; i>=0; i--)
{
printf("%d ", a[i]);
}

return 0;
}

Problem 6: Write a C program to find the minimum and the


maximum value in an array (UseFunction)

Solution:
#include<stdio.h>

int max(int a[], int n)


{
int max = 0;
for(int i=0; i<n; i++)
{
if(max<a[i])
max = a[i];
}
return max;
}

int min(int a[], int n)


{
int min = 1000000;
for(int i=0; i<n; i++)
{
if(min>a[i])
min = a[i];
}
return min;
}

int main()
{
int a[100], i, n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
for(i=0 ; i<n; i++)
{
scanf("%d", &a[i]);
}

printf("Maximum = %d\n", max(a, n));


printf("Minimum = %d\n", min(a, n));

return 0;
}

Problem 7:Write a C program to calculate the factorial of an


integer using Recursion.

Solution:

#include<stdio.h>

int fac(int n)
{
if(n==1)
return 1;
else
return (n*fac(n-1));
}

int main()
{
int n;
scanf("%d", &n);
printf("factorial = %d", fac(n) );

return 0;
}

Problem 8:Write a C Program to Find the Sum of Natural


Numbers using Recursion.

Solution:
#include<stdio.h>

int sum(int n)
{
    if(n==1)
        return 1;
    else
        return n+sum(n-1);
}

int main()
{
    int n;
    scanf("%d", &n);

    printf("Summation = %d.", sum(n));

    return 0;
}

Problem 9:Write a C program to check whether a number is


even or odd using functions.

Solution:
#include<stdio.h>
int is_even(int n)
{
    if(n%2==0)
        return 1;
    else
        return 0;
}
int main()
{
    int n;
    scanf("%d", &n);
    if(is_even(n))
        printf("Even");
    else
        printf("Odd");
    return 0;
}
Problem 10: Write a C program to read an amount (integer
value) and break the amount into smallest possible number of
bank notes.

Solution:
#include<stdio.h>

int main()
{
    int amount;
    printf("Input the amount: ", &amount);
    scanf("%d", &amount);

    printf("There are: \n");


    printf("%d Note(s) of 100.00\n", amount/100);
    amount%=100;
    printf("%d Note(s) of 50.00\n", amount/50);
    amount%=50;
    printf("%d Note(s) of 20.00\n", amount/20);
    amount%=20;
    printf("%d Note(s) of 10.00\n", amount/10);
    amount%=10;
    printf("%d Note(s) of 5.00\n", amount/5);
    amount%=5;
    printf("%d Note(s) of 2.00\n", amount/2);
    amount%=2;
    printf("%d Note(s) of 1.00\n", amount/1);
    amount%=1;

    return 0;
}

You might also like