COMPUTER GAYN
LET US C
Q1. write a C program to calculate sum of array element?
ans:-
#include <stdio.h>
int main()
{
int a[1000],i,n,sum=0;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
{
sum+=a[i];
}
printf("sum of array is : %d",sum);
return 0;
ALGORITHM
step1:- start
step2:- declare array
step3:-input Num,n
step4:-i<n
sum=sum+array[num]
step5:- else
display sum
step6:- stop
FLOWCHART
Q2. what is Function?
ans:-
A function is a set of statements that take inputs, do some specific computation and produces output.
The idea is to put some commonly or repeatedly done task together and make a function so that instead of writing the same code again and again for different inputs, we can call the function.
Q3. what is pointer? Explain with suitable example?
ans:-
he Pointer in C, is a variable that stores address of another variable. A pointer can also be used to refer to another pointer function. A pointer can be incremented/decremented, i.e., to point to the next/ previous memory location. The purpose of pointer is to save memory space and achieve faster execution time.
How to Use Pointers in C
If we declare a variable v of type int, v will actually store a value.
v is equal to zero now.
ans:-Structure is a group of variables of different data types represented by a single name. Lets take an example to understand the need of a structure in C programming.
We can solve this problem easily by using structure. We can create a structure that has members for name, id, address and age and then we can create the variables of this structure for each student.
Q5.implement the c program to find sum of 2D array?
ans:-
#include<stdio.h>
int main()
{
int a[3][2];//array declaration( 3*2=6 element)
int i,j,sum=0;
printf("enter 6 element in 2D array \n");
for(i=0;i<=2;i++)
{
for(j=0;j<1;j++)
{
scanf("%d",&a[i][j]);
}
}
for (i=0;i<=2;i++)
{
for(j=0;j<=1;j++)
{
sum=sum+a[i][j];
}
}
printf("sum of all array element are =%d",sum);
return 0;
}
ALGORITHM
Step1:- start
step2:- enter the value of first array
step3:- enter the value of second array
step4:-sum=sum+a[i][j]
add the array
step5:-display the sum of array
step6:- stop
FLOWCHART
Q6. implement the c program to read 5 elements of a 1-D array and
display those with their index on output screen?
ans:-
#include<stdio.h>
int main()
{
int arr[5]={21,31,41,51,61}; //static initilazation of 1D array
int i;
for(i=0;i<=4;i++)
{
printf("array index %d having value %d",i,arr[i]);
}
return 0;
}
ALGORITHM
Step1:-start
step2:-int arr[5]
step3:- int i
print the element in array
step4:-i<5
print element %d ,arr[i]
i++
step5:- else
print element in array ,i=0
step6:-stop
FLOWCHART
0 Comments