0% found this document useful (0 votes)
7 views10 pages

Assignment 6 on C Programming

Uploaded by

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

Assignment 6 on C Programming

Uploaded by

masumrezarock100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Assignment - 6 on C Programming

B.Sc Computer Science (Hons.)


Sammilani Mahavidyalaya
Department of Computer Science

Masum Reza,
Roll No. : 749.
Date of Assignment - 14.01.2021
Date of Submission - 16.01.2021
Submitted to Mrs. Bortoti Mondal
Introduction
What is C Language ?

Ans: C is a general-purpose, procedural computer programming language supporting structured


programming, lexical variable scope, and recursion, with a static type system. By design, C provides
constructs that map efficiently to typical machine instructions. It has found lasting use in applications
previously coded in assembly language. Such applications include operating systems and various
application software for computer architectures that range from supercomputers to PLCs and embedded
systems.
A successor to the programming language C was originally developed at
Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities
running on Unix. It was applied to re-implementing the kernel of the Unix
operating system. During the 1980s, C gradually gained popularity. It has
become one of the most widely used programming languages, ] with C
compilers from various vendors available for the majority of existing computer architectures and
operating systems. C has been standardized by the ANSI since 1989 (ANSI C) and by the International
Organization for Standardization (ISO). As of September 2020, C is the most popular programming
language.

C is an imperative procedural language. It was designed to be


compiled to provide low-level access to memory and language
constructs that map efficiently to
machine instructions, all with minimal runtime support.
Despite its low-level capabilities, the language
was designed to encourage cross-
platform programming. A
standards-compliant C program written with portability in mind
can be compiled for a wide variety of computer platforms and
operating systems with few changes to its source code.

Like most procedural languages in the ALGOL tradition, C has facilities for structured
programming and allows lexical variable scope and recursion. Its static type system prevents unintended
operations. In C, all executable code is contained within subroutines (also called "functions", though not
strictly in the sense of functional programming). Function parameters are always passed by value. Pass-
by-reference is simulated in C by explicitly passing pointer values. C program source text is free-format,
using the semicolon as a statement terminator and curly braces for grouping blocks of statement

1
Assignment Contents
Questions

1. Write a program to calculate and print the sum and average of the elements of the
array.(3)
2. Write a program to print the maximum and minimum element of the array.(5)
3. Write a program to print the even-valued elements in an array.(7)
4. Write a program to print the odd-valued elements in an array.(9)
5. Write a program to print the array in reverse order.(11)
6. Write a program to find if a number to be searched is present in the array and if it
ispresent, display the position and number of times it appears in the array.(13)

1
Let's do our Assignments---------------
Write a program to calculate and print the sum and average
of the elements of the array.

#include <stdio.h> void


main()
{
int i,n,sum=0;
float average;
printf ("\n\tEnter the value of elements\t:\t");
scanf("%d", &n); int array[n];
for (i=0;i<n;i++)
{
printf("\tEnter the number%d\t:\t",i+1);
scanf("%d", &array[i]);
sum=sum+array[i];
}
printf("\n");
for (i=0;i<n;i++)
{ printf("\t%d",array[i]);
}
average=sum/n;
printf("\n\n\tSum of all numbers = %d\n",sum);
printf("\n\tAverage of all input numbers = %.2f\n",average);
getch();
}

● Output -
Enter the value of elements : 5
Enter the number1 : 1
Enter the number2 : 2
Enter the number3 : 3 Enter
the number4 : 4
Enter the number5 : 5

1 2 3 4 5

Sum of all numbers = 15

Average of all input numbers = 3.00

3
Write

a program to print the maximum and minimum


element of the array.

#include <stdio.h> void


main()
{
int i,n,max,mini;
printf ("\n\tEnter the value of elements\t:\t");
scanf("%d", &n); int array[n];
for (i=0;i<n;i++)
{
printf("\tEnter the number%d\t:\
t",i+1); scanf("%d", &array[i]);
}
max=array[i];
mini=array[i];
for (i=0;i<n;i++)
{ printf("\t%d",array[i]);
if(array[i]>max)
{ max=array[i];
}
if(array[i]<mini)
{ mini=array[i];
}
}
printf("\n\tmaximum array is\t:\t%d\n",max);
printf("\n\tminimum array is\t:\t%d\n",mini);
getch();
}

● Output -
Enter the value of elements : 5
Enter the number1 : 1
Enter the number2 : 2
Enter the number3 : 3 Enter
the number4 : 4
Enter the number5 : 5
1 2 3 4 5 maximum
array is : 5 minimum array
is : 1

a program to print the even-valued elements in


an array.

4
● Codes -

#include <stdio.h> void


main()
{ int i,n;
printf ("\n\tEnter the value of elements\t:\t");
scanf("%d", &n); int array[n];
for (i=0;i<n;i++)
{
printf("\tEnter the number%d\t:\
t",i+1); scanf("%d",&array[i]);
}
for (i=0;i<n;i++)
{ printf("\t%d",array[i]);
}
printf("\n\teven array\n");
for (i=0;i<n;i++)
{
if(array[i]%2==0)
{ printf("\t%d",array[i]);
}
}
getch();
}

Enter the value of elements : 5


Enter the number1 : 1
Enter the number2 : 2
Enter the number3 : 3 Enter
the number4 : 4
Enter the number5 : 5
1 2 3 4 5
even array
2 4

● Output -
a program to print the odd-valued elements in
an array.

5
Write

#include <stdio.h> void


main()
{ int i,n;
printf ("\n\tEnter the value of elements\t:\t");
scanf("%d", &n); int array[n];
for (i=0;i<n;i++)
{
printf("\tEnter the number%d\t:\
t",i+1); scanf("%d",&array[i]);
}
for (i=0;i<n;i++)
{ printf("\t%d",array[i]);
}
printf("\n\todd array\n");
for (i=0;i<n;i++)
{
if(array[i]%2!=0)
{ printf("\t%d",array[i]);
}
}
getch();
}

Enter the value of elements : 5


Enter the number1 : 1
Enter the number2 : 2
Enter the number3 : 3 Enter
the number4 : 4
Enter the number5 : 51
2 3 4 5

odd array

1 3 5

● Output -

6
● Codes -

Write a program to print the array in reverse order.

#include <stdio.h> void


main()
{ int i,n;
printf ("\n\tEnter the value of elements\t:\t");
scanf("%d", &n); int array[n];
for (i=0;i<n;i++)
{
printf("\tEnter the number%d\t:\
t",i+1); scanf("%d", &array[i]);
}
printf("\n\n\tBefore reverse...........\n");
for (i=0;i<n;i++)
{ printf("\t%d",array[i]);
}
printf("\n\n\tAfter reverse..........\n");
for (i=n-1;i>=0;i--)
{ printf("\t%d",array[i]);
}
getch();
}

● Output -
Enter the value of elements : 5
Enter the number1 : 1
Enter the number2 : 2
Enter the number3 : 3 Enter
the number4 : 4 Enter the
number5 : 5

Before reverse........... 1
2 3 4 5

After reverse..........
5 4 3 2 1

7
Write a program to find if a number to be searched is present
in the array and if it ispresent, display the position and
number of times it appears in the array.
#include <stdio.h> void
main()
{
int i,n,search,f,count;
printf ("\n\tEnter the value of elements\t:\t");
scanf("%d", &n); int array[n];
for (i=0;i<n;i++)
{
printf("\tEnter the number%d\t:\
t",i+1); scanf("%d", &array[i]);
}
for (i=0;i<n;i++)
{ printf("\t%d",array[i]);
}
printf("\n\tEnter the search number\t:\t");
scanf("%d", &search);
count=0;
for(i=0;i<n;i++)
{
if(array[i]==search)
{ count++;
}
}
f=0;
for(i=0;i<n;i++)
{
if(array[i]==search)
{
f=1;
break;
}
}
if(f==1)
{
printf("\n\tNumber found.
array",search,i+1,count);
}
else printf("Worng input");
getch();
}
Number %d at position %d and %d times it appears in the

● Output -
Enter the value of elements : 6
Enter the number1 : 1
Enter the number2 : 5
Enter the number3 : 6
Enter the number4 : 5
Enter the number5 : 4

8
● Codes -

Enter the number6 : 5


1 5 6 5 4 5
Enter the search number : 5

Number found. Number 5 at position 2 and 3 times it appears in the array

You might also like