Professional Documents
Culture Documents
1.a) Devlop an algorithm for sum of digits in a given number. Draw a flow chart for it.
Algorithm:
Step 1: Start
Step 7: Stop
Flowchart:
1.b) Define algorithm? Write the characteristics of an algorithm.
Algorithms are very important to the way computers process information, because a
computer program is basically an algorithm that tells computer what specific tasks to perform
in what specific order to accomplish a specific task. The same problem can be solved with
different methods. So, for solving the same problem, different algorithms can be designed. In
these algorithms, number of steps, time and efforts may vary more or less.
Important characteristics to be considered while designing an algorithm for any program are
as follows:
• Clear and Unambiguous: Algorithm should be clear and unambiguous. Each of its
steps should be clear in all aspects and must lead to only one meaning.
• Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined
inputs.
• Well-Defined Outputs: The algorithm must clearly define what output will be yielded
and it should be well-defined as well.
• Finite-ness: The algorithm must be finite, i.e. it should not end up in an infinite loops
or similar.
• Feasible: The algorithm must be simple, generic and practical, such that it can be
executed upon will the available resources. It must not contain some future technology,
or anything.
• Language Independent: The Algorithm designed must be language-independent, i.e. it
must be just plain instructions that can be implemented in any language, and yet the
output will be same, as expected.
2.a) Draw the flowchart to find the factorial of a given number.
2.b) Write the importance of precedence and associativity? Write the table for operator
precedence.
Operator precedence: It dictates the order of evaluation of operators in an expression.
For Example, Solve 10+2*3, 10+2*3 is calculated as 10+(2*3) and not as (10+2)*3.
Operator Associativity: It defines the order in which operators of the same precedence are
evaluated in an expression. Associativity can be either from left to right or right to left.
For example, ‘*’ and ‘/’ have same precedence and their associativity is Left to Right, so the
expression “100/10*10” is treated as “(100/10)*10”.
Operators Precedence and Associativity are two characteristics of operators that determine
the evaluation order of sub-expressions in absence of brackets.
3.a) List and explain various Looping statements available in C with Example.
Programming languages provide various control structures that allow for more complicated
execution paths.
Loops in programming come into use when we need to repeatedly execute a block of
statements. A loop statement allows us to execute a statement or group of statements
multiple times. Given below is the general form of a loop statement in most of the
programming languages:
C programming language provides the following types of loops to handle looping
requirements:
1. For loop:
Executes a sequence of statements multiple times and abbreviates the code that manages the
loop variable.
Syntax:
Example:
#include<stdio.h>
Void main()
{
int i;
for(i=0;i<11;i++)
{
printf(“d”, i);
Output: 1 2 3 4 5 6 7 8 9 10
2. While loop:
Syntax:
While(test expression)
• The while loop evaluates the test expression inside the parenthesis ().
• If the test expression is true, statements inside the body of while loop are
executed. Then the test expression is evaluated again.
• The process goes on until the test expression is evaluated to false.
• If the test expression is false, the loop terminates.
Example:
#include<stdio.h>
Void main()
int i=1;
while(i<=5)
Printf(“%d”, i);
++i;
Output: 1 2 3 4 5
3. do…While loop:
It is more like a while statement, except that it tests the condition at the end of the loop body.
Syntax:
do
}while(test expression);
• The body of do…while loop is exexuted once. Only then, the test expression is
evaluated.
• If the test expression is true, the body of the loop is executed again and the test
expression is evaluated.
• This process goes on until the test expression becomes false.
• If the test expression is false, the loop terminates.
Example:
#include<stdio.h>
Void main()
int n,sum=0;
do
printf(“Enter a number:”);
scanf(“%d”, &n);
sum+=n;
} while(n!=0);
Output:
Enter a number: 10
Enter a number: 20
Enter a number: 30
Enter a number: 40
Enter a number: 0
Sum = 100
4. Nested Loops:
You can use one or more loops inside any other while, for, or do..while loop.
3.b) Develop a program to check whether the given number is Armstrong number or
not?
#include <stdio.h>
int main()
scanf(“%d”, &num);
temp = num;
while (temp != 0)
i = temp % 10;
sum += i * i * i;
temp /= 10;
if (sum == num)
else
return 0;
#include<stdio.h>
#include<math.h>
int main()
int a,t,d;
scanf(“%ld”,&n);
t=n;
d=digi(n);
while(t>0)
a=t%10;
s=s+pow(a,d);
t=t/10;
if(n==s)
else
return 0;
}
int digi(long int t)
int dig=0;
while(t>0)
dig++;
t=t/10;
return dig;
Output:
4.a) Develop a program for the printing the following pattern on the screen.
* *
* * *
* * * *
Program:
#include <stdio.h>
void main()
int i, j, num;
printf("\nEnter number of rows:");
scanf("%d", &num);
for(i=0;i<=num;i++)
for(j=0;j<=i;j++)
printf("*");
printf("\n");
Output:
* *
* * *
* * * *
i=j;
Program:
#include<stdio.h>
int main()
if(m2!=n1)
for(i=0;i<m1;i++)
for(j=0;j<n1;j++)
scanf(“%d”,&a[i][j]);
for(i=0;i<m2;i++)
for(j=0;j<n2;j++)
scanf(“%d”,&b[i][j]);
for(i=0;i<m1;i++)
for(j=0;j<n2;j++)
c[i][j]=0;
for(k=0;k<n1;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
printf(“%d\t”,c[i][j]);
printf(“\n”);
return 0;
}
Output 1:
2 3
3 2
1 2 3
4 5 6
1 2
2 3
0 5
5 23
14 53
Output 2:
2 3
2 2
Program:
#include <stdio.h>
#include<string.h>
void main()
int i, l=0;
char str[50];
scanf("%s", str);
for(i=0;str[i]!='\0';i++)
l++;
Output:
Program:
#include <stdio.h>
int main(){
scanf("%d",&n);
printf("Elements in array:");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
if(arr[i]%2==0)
sum+=arr[i];
return 0;
Output:
Elements in array: 2 3 4 5 6 7
#include<stdio.h>
Void main()
int arr[5], i, n;
for(i=0;i<5;i++)
scanf(“%d”, &arr[i]);
for(i=0;i<5;i++)
printf(“%d\t”, arr[i]);
}
Example for two-dimensional array:
#include<stdio.h>
Void main()
int arr[5][5], i, n;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
scanf(“%d”, &arr[i][j]);
for(i=0;i<5;i++)
for(j=0;j<5;j++)
printf(“%d\t”, arr[i][j]);
Program:
#include <stdio.h>
int main()
p=&a;
q=&b;
temp=*p;
*p=*q;
*q=temp;
return 0;
Output:
C programming allows passing a pointer to a function. To do so, simply declare the function
parameter as a pointer type.
Pointer as a function parameter is used to hold addresses of arguments passed during function
call. This is also known as call by reference. When a function is called by reference any
change made to the reference variable will effect the original variable.
Following is a simple example where we pass an unsigned long pointer to a function and
change the value inside the function which reflects back in the calling function –
#include<stdio.h>
(*ptr)++;
int main()
addOne(p);
printf(“%d”, *p);
return 0;
Output: 11
We then passed the pointer p to the addOne() function. The ptr pointer gets this address in the
addOne() function.
Inside this fiunction, we increased the value stored at ptr by 1 using (*ptr)++;. Since ptr and p
pointers both have the same address, *p inside main() is also 11.
8.a) What is pointer? How to initialize and declare pointer variables? Explain with
examples.
Pointer is a variable that stores the address of another variable. C pointer is used to allocate
memory dynamically i.e. at run-time. The variable might be any of the data type such as int,
float, char, double, short etc.
Like any variable or constant, you must declare a pointer before using it to store any variable
address.
data_type *pointer_name;
The data type of the pointer and the variable to which the pointer variable is pointing must be
the same.
int a;
#include stdio.h>
int main()
int a;
int *ptr;
a=110;
ptr=&a;
printf(“%d\n”, a);
printf(“%d\n”, *ptr);
printf(“%u\n”, &a);
printf(“%u\n”, ptr);
return 0;
Output:
110
110
3795480300
3795480300
3795480304
Program:
#include <stdio.h>
int main()
int arr[10], n, i;
int *ptr;
scanf("%d", &n);
scanf("%d",&arr[i]);
ptr=&arr[0];
printf("%d\t", *(ptr+i));
return 0;
}
Output:
File handling in C enables us to create, update, read, and delete the files stored on the local
file system through our C program. The following operations can be performed on a file.
There are many functions in the C library to open, read, write, search and close the file. A list
of file functions are given below:
Opening a file:
fp=fopen(filename, mode);
Mode Description
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
a.txt
b.txt
10.a) Define File? Briefly explain various modes and operations on files with examples.
A file is a space in a memory where data is stored. 'C' programming provides various
functions to deal with a file. A mechanism of manipulating with the files is called
as file management. A file must be opened before performing operations on it. A file can be
opened in a read, write or an append mode.
There are 3 ways to declare the function which is intended to receive an array as an argument.
When we pass an array to a function say fun(), it is always treated as a pointer by fun().
Consider the following example:
#include <stdio.h>
void fun (int arr[ ])
{
unsigned int n = sizeof(arr)/sizeof(arr[0]);
printf("\nArray size inside fun() is %d", n);
}
int main()
{
int arr[ ] = {1, 2, 3, 4, 5, 6, 7, 8};
unsigned int n = sizeof(arr)/sizeof(arr[0]);
printf("Array size inside main() is %d", n);
fun(arr);
return 0;
}
Output:
Array size inside main() is 8