ioe solution 2068 c programming
2068 Chaitra
IOE Regular Exam
1. a)How high level programming languages are similar to natural language? Describe.
b)Does algorithm and flowchart design makes the programming faster and easier? Describe with strong supporting point.Also draw a flowchart to check whether a
number is armstrong or not.
Q.1.
[a]ans:-
Highlevel language is the interface between natural language and machine language.
Data and instructions written in human language is translated by translator such as interpreter and compiler.
Since the machine language generated by one compiler is not recognized by another other computer.
So it is necessary to have a C compiler of same type on which the C program has to be executed.
[b]ans:-
Algorithm and flowchart are the program design tool that is helpful for programming.
So they makes the programming faster and easy.The finite serial step used in algorithm may help programmer in decision-making eassier and is blueprint
to writing a a program to solve particular problem while flowchart is a pictorial representation of the step involved in algorithm which once drawn may
help programmer to make user understand the problem easily and clearly.As a result they are the communicating interface between programmer and algorithms.
So they make the programming faster and easier.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,s=0,d;
printf(“Enter the number”);
scanf(“%d”,&n);
while(n!=0)
{
d=n%10;
s=s+pow(n,3);
n=n/10;
}
if(s==n)
printf(“The number is Armstrong”);
else
printf(“The no. is not armstrong”);
getch():
}
2. a)What are urnary operators in C? Why are they named so?
b)what are preprocessor directivess in c? Why are they named so?
c)How are formatted input/output can be performed in C?Explain with example.
Q.2:-
[a]ans:-
Unary operators act on single operands.
C language supports three operators-unary minus,increment,decrement operators.Unit means one.
So its use is to act as on single operands.So it is named so.
[b]ans:-
The preprocessor is a program that processes the source code before it passes through compiler.
It operates under the the control of what is known as preprocessor command lines or directives.
The main work of preprocessor is to is to examined the source code for any preprossor directives and
if there is any error appropriate actions are taken and then the source code is handed to the compiler.
So it is necessary.
[c]ans:-
The main propose of program is to take input from the user and to process the result and to display the result.
For writing a program there is certain rule in the C.The funtion to take input is scanf(“data type”,address);
and printf displays the result using printf(“statement”,the data that is to be displayed”); .
Example:
To find sum of two number:
#include<stdio.h>
#include<conio.h>
void amin()
{
int a,b,sum;
printf(“Enter two number”); //displays to enter two numbers//
scanf(“%d%d”,&a,&b); //Take input from the user//
sum=a+b;
printf(“The sum is %d”,sum); //Displays the sum//
getch();
}
3.Differentiate between conditional operator and if-else statement. Write a program in C that calculates the sum of digits entered by the user successively until the sum reduces to a single digait number.For example,1234=>1+2+3+4+5=15=>1+5=6.
Q.3.Ans:-
Conditional operator:
It is used for making two way decision. It takes three operands. The general form is:
conditional expression?Expression1:Expression2
If-else statement:
The if-else statements extends the idea of the if statement by specifing another section of code that should be executed only if the condition is false
i.e.,conditional braching:
Syntax:-
if(test expression)
{
true block statement;
}
else
{
false block statement;
}
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0,a,b=0;
printf(“Enter any number”);
scanf(“%d”,&n);
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
while(sum>0)
{
a=sum%10;
b=b+a;
sum=sum/10;
}
printf(“Result is%d”,a);
getch();
}
4.Write a program in C to read a string and display it ain a reverse order.Use user defined functions to count the number of character in it and reverse it.
Q4.Ans:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
int count(int);
void reverse(char);
main()
{
int n,c;
char str[100],r[100];
printf(“Enter the string”);
scanf(“%s”,str);
c=count(n);
r=reverse(str);
printf(“The count is %d”,c);
getch();
}
int count(int n)
{
for(i=0;i<n;i++)
{
count++
}
return (count);
}
char reverse(char name)
{
int i;
for(i=0;i<strlen;i++)
{
a[i]=a[strlen-1];
}
printf(“The reverse is %s”,a[i]);
}
5.Write the significance of array in C. Write a program to multiply two 3X3 matrix. Two matrix are input from main() function and pass to a user defined function with arguement with array. The result is also displayed from main() function.
Q.5.Ans:-
During programming it is not possible and feasible to declare and manipulate the many elements of similar types.
Thus to overcome such difficulty array is used,which is collection of similar elements.
#include<stdio.h>
#include<conio.h>
void mul(int[][10],int[][10],int[][10]);
void main
{
int a[10][10],b[10][10],c[10][10],i,k,j;
printf(“Enter 3*3 matrix”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“n a[%d][%d]”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“Enter another 3*3 matrix”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“n b[%d][%d]”,i,j);
scanf(“%d”,&b[i][j]);
}
}
mul(a,b,c);
printf(“Rusult:n”);
printf(“Enter 3*3 matrix”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d”,tc[i][j]);
}
printf(“n”);
}
getch();
}
void mul(inta[][10],intb[][10],intc[][10]);
{
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][j]*b[i][j];
}
}
}
}
6.Why structure variable differ from array? Write a program to input name post and salary of ten employees from main() function and pass to a user defined function(arguements of this function should also have a structure type). This function returns structure variable which keeps the records of only those employees whose salary is greater than 10000. This modified record is also displayed from main() function.
Q.6.Ans:-
Structure can strore more than one type of data, which is not possible using array. Structure uses dynamic memory allocation whereas array uses static
memory allocation. Dot operator is used to access the members of structure whereas array uses subscripts. Structure contains bits field whereas array doesnt.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct employee
{
char name[20];
char post[20];
long int salary;
};
void display(struct employee*);
void main(void)
{
struct employee *p;
int n,i;
clrscr();
p=(struct employee*)calloc(n,sizeof(struct employee));
printf(“Input values of 10 employees:n”);
for(i=0;i<10;i++)
{
printf(“Enter name:”);
gets((p+1)->name);
printf(“Enter post:”);
gets((p+1)->post);
printf(“Enter salary:”);
scanf(“%ld”,&(p+1)->salary);
}
display(p);
getch();
}
void display(struct employee *pe)
{
int i;
for(i=0;i<3;i++)
{
if((pe+1)->salary>10000)
{
printf(“Name:%sn”,(pe+i)->name);
printf(“Post:%sn”,(pe+i)->post);
printf(“Salary:%ld”,(pe+i)->salary;
}
}
}
7.Explain how the pointer can be used in C. How can you access array elements using pointer. Write a C program that reads a string from the user and use the user defined function to copy the contents of the read string into another character array changing lower case into upper case if any. This modified record is also displayed from the main() function.
Q.7.Ans:-
A pointer variable is declared by giving it a type and a name (eg int *p) where the asterik tells the compiler that the variable named ‘p’ is a pointer
variable and the type tells the computer what type the pointer is to point to(integer in this case).
The first thing we must do to use a pointer is to initialize it. Once a variable is declared, we can get its address by preceding its name within the
unary & operator, as in &num. we can “dereference” apointer, i.e., refer to the value of that which it points to, by using the unary ‘*’ operator as in *num.
We can have arrays of pointers since pointers are variables
int *a[15];
which means that a is an array of 15 pointers, which points to integer data type.
member-> *(ptr+i) where i=0,1,2,3,…., max-1
Array pointer
int a[10] a
a[0] *a
a[1] *(a+1)
#include<stdio.h>
#include<conio.h>
void convert(char[]);
void main()
{
char anystring[20];
clrscr();
printf(“Enter a string:”);
gets(anystring);
convert(anystring);
printf(“String after conversion:”);
puts(anystring);
getch();
}
void convert(char anystr[])
{
int i;
i=0;
while(anystr[i]!=”)
{
if(anystr[i]>=’a’&&anystr[i]<=’z’)
{
anystr[i]=anystr[i]-32;
}
i++;
}
}
8.Write a program to input and save records like roll,address and obtained marks of 48 student in binary file and search and display the record of the student whose marks obtained in highest. The information should be organized in a structure.
Q.8.Ans:-
#include<stdio.h>
#include<conio.h>
#include<dos.h>
struct record
{
char name[10],address[10];
int roll;
float mark;
};
int main()
{
struct records,t;
int i;
FILE*fp;
fp=fopen(“Student.data””wb”);
if(fp==NULL)
{
printf(“n File cannot be opened”);
getch();
exit(1);
}
for(i=0;i<48;i++)
{
printf(“n Student %d”,i+1);
printf(“Enter name”);
scanf(“%s”,s.name);
printf(“Enter roll no”);
scanf(“%d”,&s.roll);
printf(“Enter address”);
scanf(“%s”,s.address);
printf(“Enter mark”);
scanf(“%f”,&s.mark);
fwrite(&s,size of(s),1,fp);
fclose(fp);
fp=fopen(“Student.data”,”rb”);
if(fp==NULL)
{
printf(“n File cannot be opened”);
getch();
exit(1);
}
fread(&s,size of(s),1,fp);
t=s;
for(i=0;i<48;i++)
{
fread(&s,size of(s),1,fp);
if(t.marks<s.mark);
t=s;
}
printf(“n Record of student with higher mark”);
printf(“n Name=%s”,t.name);
printf(“n Roll=%d”,t.roll);
printf(“n Address=%s”,t.address);
printf(“n mark=%f”,t.mark);
fclose(fp);
getch();
}
9.Write a program in FORTRAN, to check whether a positive integer entered from the keyboard is a pallindrome or not.
(Hint:A number is palindrome if its reverse is equal to the number itself).
Q9.Ans:-
integer n,rev,dig,temp
rev=0
write(*.*)’enter a number:’
read(*.*)n
temp=n
2 if(n.ne.0) then goto 1
dig=mod(n,10)
rev=rev*10+dig
n=n/10
1 if(rev.eq.temp)then
write(*.*)’the number’,n,’is a palindrome.’
else
write(*.*)’the number’,n,’is not a palindrome.’
end if
end
10.Write FORTRAN program to display greatest and smallest number from list ten element.
Q10.Ans:-
integer num(10),i,j,k,temp
write(*.*)’enter the numbers:’
read(*.*)(num(i),i=1,10,1)
do 1 j=1,10-1,1
do 2 k=1,10-j-1,1
if(num(k).gt.num(k-1))then
temp=num(k)
num(k)=num(k-1)
num(k-1)=temp
endif
2 continue
1 continue
write(*.*)’greatest number is:’
write(*.*)(num(10))
write(*.*)’smallest number is:’
write(*.*)(num(1))
end
