Solved Question Paper 3

Preview:

DESCRIPTION

If statement is a powerful decision making statement and is called to control the flow of execution of statements. It is implemented in different forms If Statement If expression Simple If statements statements statements false true C.40 Solved Question Papers Example: Example: If expression If Else Statement statements statements true

Citation preview

����������������� ��� ��� ����������������

�����

�� ������������ ������������ ���������� ������������� ������������ �����������

�������

� �� ������������������������� ��� ���������������������������

�� ���������

�� �������� ������������

�� ��� ��������������������

�� ������������

��!�� ���!������������������ ���������"�����

�����������

�������������������!�� ��������������������� ������� �����������������������"�!����

������������������������� ���� ������������

��������

���������������������General form: if(expression)

{

Statements ;

}

Statements;

Ifexpression

statements

statements

statements

false

true

���� ������������ ������

�������

�������������������Main (){

Int a,b,c,d;float ratio;printf(enter the values");scanf("%d%d%d",&a,&b,&c);if(c-d!=0)

{Ratio=(float(a+b)/float(c-d));Printf("ratio=%f",ratio);

}

��� ������������

���������������������������General form:

If(expression) {

True block }Else

{False block

}Statements;

Ifexpression

statements

statements

False block

false

true

�������

If(c-d!=0) {

Ratio=float(a+b)/float(c-d);Printf("ratio=%f",ratio);

}ElsePrintf("c-d is zero");

}

������������ ������ ���

������������ ������������

General form:

If(condition 1) {

If(condition 2){

Statement 1;

} Else { Statement 2; } Else { Statement 3; } Statement x;

������ ��������������� ������������#��# ���������������������������������

Statement 3

Ifcondition 1

Condition 2

Statement 2

Statement

truefalse

Statement 1

�������

���������������������Main() {

Float a,b,c;Printf("enter the values");Scanf("%F%f%f",&a,&b,&c);Printf("largest value is"); If(a>b) {

If(a>c)

���� ������������ ������

Printf("%f",a);Else

Printf("%f",c); } Else

{If(c>b) Printf("%f",c);

ElsePrintf("%f",b);

} }

�����������

General form: If(condition 1)

Statement 1;Else if(condition 2)

Statement 2;Else if(condition 3)

Statement 3;Else if(condition n)

Statement n;Else

Default statement;Statements;

$�%������� ���������#�!� ������������� ��!������ ��

Cond 1

Cond 2

Cond 3Default

statement

Statement 1

Statement 2

Statement 3

Statements

Next Statement

�������

Main(){Int units, custno;

������������ ������ ����

Float charges;

Printf("enter custno and units");Scanf("%d%d",&custno,&units);

If(units<=200)Charges=0.5 units;

Else if(units<=400)Charges =100+0..6s(units-200);

Else if(units<=600)Charges=390+units-600;

Printf("custno %d", charges =%f", custno,charges); }

������������������������

General form: Switch(expression) { Case 1:

Statements;Break;

Case 2 : Statements;

Break;Default :

Statements:Break;

}Statements;

Block 1

Case2 Block 2

Default

Statements

Switchexpression

yes

no yes

no

Case1

���� ������������ ������

�������

Switch(choice){

Case 1 ''+':C=a+b;Break;

Case '-':C=a-b;Break;

Default:Printf("operation not possible");

}

�������������������������

General form: Conditional expression ? expression 1: expression 2

�������

�Flag(x<0) ? 0:1;Salary= 4x+100 for x<40

300 for x=40 4.5x+150 for x>40

The data can be written as Salary=(x!=40)?(x<40)?(4*x+100)L4.5*x+150):300;

��������������

�������&!�������������� ������ ����������������������������������� ��'����������

#�� �#�������

�������

Main () { Double x,y; Read:

Scanf("%f",&x);If(x<0) goto read;Y=sqrt(x);Printf("%f%f",x,y);Go to read;

}

� !�"�������������� ���� ��#���� �������������$��������

� �� (������ ����� ���!������������� �� �����!����������� ����������� �������#� �

�������������!���������������� �#������������������������� �� ������������������

�����������������������#��������������� �

������������ ������ ���!

�������

int m;main (){

Int I;Float balance;

}

���#������������������� ��� ������������������� �������#�������������

!� �����������!�������������������������� ������� ��� ����������!���������'������

#����� ��� ���� ������ �� "����� #���������#������ �� � ��������� �� ��� � ����

#�������������#��������������� ��!����������������������� ��� �

)�*����#� ���#���������������������������� ���������!� ���� ����"��������� ��

������ �����������#�������������������������� �����������������������������+

�!����������� ��!���������������������

,�!�������������������������#��%��$����%�������%�� ���� �

�#��� ����#������������������������!���������������������� ��� %� �!������!���

����������� -�������.

��������������������������������'!�������"/

'!���#�������#������#�!��!������������������0 �"���������

%������ ���� #����� ������ "����� � � ������ ���� #�!� #�� ���� ��� �������� ��

������� ���������������!�������

-�������.

�������������������������������"/

������� �"����#���������������0 �!�������������0���

!�" ������������� ���� ������������ ��

� �� ,!���������������!������

�� �������!�����������!��

�� �����!�����������!��

�� �������!���������!��

�� �����!���������!��

1����!������������!�������%������!������������� ��� �����#�!����������

��������� ����������� ������������������������ �������#����������� ��������

��� ��

2��������������������!������%������������� �������#�������������!��������

�������

Main() {

Int x,y;X=100;

���" ������������ ������

������������������������Y=200;

Printf("before exchange x:%d y: %d", x,y) ;

Exchange(&x,&y);

Printf("after exchange: x=%d y=%d",x,y);

}

Exchange(a,b)

Int *a,*b;

{

Int t;

T=*a;

*a=*b;

*b=t;

}

&� ��������� ������������������������ ������������������������ �

� �� '�������������!�������� � ���������������������������'���������������!�

�������� ������

-���������������������.

�� ��+ �������������

�� ���+ �������������

�� �!���+ �������������

����������� ��!�� ���!�����!��������� ����

�����������������

'���������������������#�����#��������!��������������!��������� ��!����#�����������

��������!������� �#�����������+ �������������

�������

(�!��������������0 �������������������

3�4 �����.

N[0]=1

N[1]=2

N[2]=3

3��4 n[3]={0,1,2};

3���4 !�������������.

for(i=0;i<3;i++)

scanf("%d",&n[i]);

n[4]= n[0]+n[2];

n[6]= n[i]+3;

������������ ������ ���#

��������

�����������������#�������5��06

������� ����������5786/

'��$���

main(){

Int I;Float x[10], value, total;

Printf("enter 10 real no");For(i=0;i<10;i++){

Scanf("%f", &value);X[i]=value;

}

Total=0.0;For(i=0;i<10;i++)Printf(""x[%2d]=%st",i+1,x[i]);Printf("total=%2f",total);

}

!��� �������������

����"�

�������#���������������� ����!����

����������������������+ ��������������������������0 ������������������ ��

N[2] [3]={0,0,0,1,1,1,};N[2][3]={{0,0,0},{1,1,1}};

(OR)N[2][3]={

{0,0,0},{1,1,1},

};-�������.

����������5������065���!�����06/

'��$���

/*To perform the matrix addition by using two-dimensional arrays */Main ()

{ Int a[50][50], b[50][50],c[100][100],I,j,k; Printf("enter the elements");

���� ������������ ������

For(i=0;i<3;i++)For(j=0;j<3;j++)Scanf("%d",&a[i][j]);For(i=0;i<3;i++)For(j=0;j<3;j++)Scanf("%d",&b[i][j]);C[i][j]=0;For(i=0;i<3;i++)For(j=0;j<3;j++)

{ C[i][j]=a[i][j]+b[i][j];

Printf("\n"); }

Printf("%d",c[i][j]); }

#$��� �������������

��������

�����������5��6�5��69��5��6/

������� �����!�#�5�65765��6/

�������������������������������,�������5765�65�6/

(� )�� ����#��#��� ���������$ �������������������� $�� ������� $�������

�� �� ���!��!���������� ��������������&!����������������������� #����������!��!��

���������� ��������������� ������� �������

-����������!��!��.

�������������������Struct student { Char name[20]; Int stno; (1)

};

:��������������������������.

���������� Struct tagname { Data type n1;

Datatype n2; };

��� ��� ��� ��� 3�4� ��� ������ ��������� ��� ���� ��%� ����� ��� ���!��!�� ��� ��� � ���

���������

������������ ������ ���$

Struct bokkbank

{Char title[20];Char author[15];Int pages;Float price;

};Struct bokbankk bookk1,book2,bok3;�����������������

*��� $����#�����������

�������������������� ��#��������������� ����!�����; ���������;���� ;����

������;�

���������������� Book1.pages=250; Book1.price=28.50;

'�����������0 �!���������3�4��������

������������Scanf("%s",book1.title); Scanf("%d",bok1.pages);

%��#��#��� ������+���� �

�Main () {

Static struct {

Int weight;

Float height; }

Student={60,180,75};----------------------------------

}�'����������������0 ��

Main () {

Struct st_record {

Int weight;

Float height; };Static struct st_record st1={60,180,75};Static struct st_record st2={53,170,60};--------------------------

��!� ������������ ������

������� }

(OR)

Struct st_recrd {

Int weight;Float height;

} st1={60,180,75}; Main (0 { Static struct st_recrd st2={53,170,60}; --------------- ----------------

}������ $�������������#��#��

���!��!����������������� ������������#������������

� � � � � � � � � � � � � � � � � � � � � � �-��3� �� 4�������� ���������!��!��#����� ��� ��� �������� ��� ��� ���������

��� ����������� ��������#�������������������

����$ � $����#��������#��#��

����������������������������� ����������������������������������� ��������

��������������Struct person p1={"ravi", "21", "male", "student"}��������������'���������������������#�!���������������������������������

����������������Strcpy(p1.name, "ravi"); P1.age=21;

Strcpy(p1.gender, "male"); Strcpy(p1.occupation, "student");

$�������"���� �����������������!��!��������������0 �#�!��

/* structure with initialized values */

#include<stdio.h>#include<conio.h>Void main ( ){

Struct organization {

bb Char name[20]; Char designation[20]; Int sal;

};Struct organization emp1={"ramu", "secretary", "8000"};Struct organization emp2={"raju", "manager", "18000"};

������������ ������ ��!

Printf("details of employee one =%c%c%d",emp1.name ,emp1.designation,emp1.sal);Printf("details of employee one =%c%c%d",emp2.name ,emp2.designation,emp2.sal);}

,� ���������#�����������������# ���� ����!"��-���������!"�#����+������ ������$��������

#������ �# ����������������������� �

� �� ,���3�4�������������!���������������������� ������������������������ ������!����������

��#������ ���� ����������������������������#����������������������������#��

�����������$�������#�������$����������!������ ���������������%������ ����������

��������� ��������%����� �������!� ��������� ��������������!��� �

'��$���

#include<stdio.h>Main()

{FILE *f1;Char c;Printf("data input");F1=fpen("input file", "w");

While(c=getchar()!=eof)Putc(,f1);

Fclose(f1);Printf("data utput");F1=fpen("input",: "r");While(c=getc(f1)!=eof)

Printf("%c",c);Fclose(f1);

}

��� ���� ���!�� ��� ���� � ���� � ����� ��� ������� ���� � �� ���� ����"�� ������� ��

�������� � ����������������������< ������������� �����$������!��������� ���

������������ ������ �%����� ��������!��������!��������������������!�����!����� �

1������� ����������������� %������!�������#�!����

.� ����������$����������������� ��������������� �����$���������! ���$� "�

� �� ����������������&!����������������� �#�������3�������4�������������

�������������������������������&!�!� ���������������� ���������������� ������������

��#�%������������������������ ��� �#� !����!���������������������&!��

=������������������&!�%�&!��������������������������������������!�����

'��$���

#include <stdio.h>

��!� ������������ ������

#include <conio.h>

#define MAX_ARY 10

void merge_sort(int x[], int end, int start);int main(void) {int ary[MAX_ARY];int j = 0;

printf("\n\nEnter the elements to be sorted: \n");for(j=0;j<MAX_ARY;j++)scanf("%d",&ary[j]);

/* array before mergesort */printf("Before :");

for(j = 0; j < MAX_ARY; j++)printf(" %d", ary[j]);

printf("\n");merge_sort(ary, 0, MAX_ARY - 1);

/* array after mergesort */printf("After Merge Sort :");for(j = 0; j < MAX_ARY; j++)printf(" %d", ary[j]);

printf("\n");

getch(); }

/* Method to implement Merge Sort*/void merge_sort(int x[], int end, int start) {

int j = 0;const int size = start - end + 1;int mid = 0;int mrg1 = 0;int mrg2 = 0;int executing[MAX_ARY];

if(end == start)return;mid = (end + start) / 2;

������������ ������ ��!�

merge_sort(x, end, mid);

merge_sort(x, mid + 1, start);

for(j = 0; j < size; j++)executing[j] = x[end + j];

mrg1 = 0;mrg2 = mid - end + 1;

for(j = 0; j < size; j++) {if(mrg2 <= start - end)

if(mrg1 <= mid - end)if(executing[mrg1] > executing[mrg2])

x[j + end] = executing[mrg2++];else

x[j + end] = executing[mrg1++];else

x[j + end] = executing[mrg2++];else

x[j + end] = executing[mrg1++];}

}

��������#���������������������� ������%���������������!�����&!���������

'��$���

#include<stdio.h>main(){int x[10],i,n;clrscr();printf("enter no of elements:");scanf("%d",&n);printf("enter %d elements:",n);for(i=1;i<=n;i++)scanf("%d",&x[i]);quicksort(x,1,n);

printf("sorted elements are:");for(i=1;i<=n;i++)printf("%3d",x[i]);getch();

}quicksort(int x[10],int first,int last)

��!� ������������ ������

{int pivot,i,j,t;if(first<last)

{pivot=first;i=first;j=last;while(i<j){while(x[i]<=x[pivot] && i<last)i++;while(x[j]>x[pivot])j--;if(i<j){t=x[i];x[i]=x[j];x[j]=t;}}

t=x[pivot];x[pivot]=x[j];x[j]=t;quicksort(x,first,j-1);quicksort(x,j+1,last);

} }

#����������%������

'�>�?7@%ABC/

D�>�?�7%@A%BEC���������� �������

��>�?�7%7@%@A%AB%BEC

������������������'��������7@% �AB

������������������D����������7% @A%����BE

�����������������������������7 �7@

�����������������'����������7@% �AB

�����������������D������������7% �@A%���BE

������������������������������7 �7@

�����������������'�����������7@ �AB

�����������������D�������������7 @A�����BE

�������������������������������7 7@�����@A

������������ ������ ��!!

�����������������'������������7@��������AB

�����������������D�������������7��������@A���������BE

�������������������������������7���������7@��������@A��������AB

������������������>�?�7%7@%@A%AB%BEC���������������� ������!��������������

/� *���� ���$������0�����$���������������� $����� 1�����#���������� ������

� �� ����!������� �����. ����������������������!��!�����������������!������������������

������������ ����������������������������������� �����������F2 ���!�����������

��� ������!������� ������

10 17 18 5

$����������!�����������#��������!��������

typedef struct node Node;

void reverse(Node** headRef)

{

Node* result = NULL;

Node* current = *headRef;

Node* next;

while (current != NULL)

{

next = current->next; // tricky: note the next node

current->next = result; // move the node onto the result

result = current;

current = next;

if (current == *headRef)

{

//met the beginning node...

break;

}

}

(*headRef)->next = result;

*headRef = result;

}

��!" ������������ ������

����������'��$���

��������� #include<conio.h>#include<stdio.h>

struct node{int data;struct node *next;};struct node *root;void addnode(){int item;struct node *newnode,*p;printf("\n Enter the item");scanf("%d", &item);

newnode=(struct node*) malloc(sizeof(struct node));newnode->data=item;newnode->next=NULL;if(root==NULL){root=newnode;return;}____ _________ ________p=root;while(p->next!=NULL)p=p->next;

p->next=newnode;}void display(){struct node*p=root;while(p!=NULL){printf("\n %d ",p->data);p=p->next;}}void reverse(){struct node *p,*oldnode=NULL,*newnode;p=root;while(p!=NULL)

������������ ������ ��!#

{newnode=p;p=p->next;if(oldnode==NULL){oldnode=newnode;oldnode->next=NULL;}else{newnode->next=oldnode;oldnode=newnode;}}root=oldnode;}void main(){int i;clrscr();for(i=1;i<=10;i++){addnode();}printf("\nbefore reverse");display();printf("\n After reverse");reverse();display();return 0;

}

2� ������ ���$��������������������� �������� �� ����� �������������

� �� ��������!���������������������� ��������� �

34 F���� ��������������������������� � �

3�4 ����� �������������� ������������ ��

3�4 ����� ����������� �����"������������

3 4 ����� ����������� ������������� ��

������ 1������� ���������������������� ����������������������������

������������������� ������ �� ����������� ��%�������������!�� ������������!� ��

�� �� �������������������������������������������������� ����!� ���������F2 ��1����

����������������F2 %� �� ��!��������������� ������ �� ������������� ����

���������� �������������

��!� ������������ ������

������ ���������� ������ �� ����������� %�������!������������������������1

�#���� G!�������������������������������� ������ �� %��!����������� ��������

�������������� ������ ������� �� �

����

6

17

2924

25

23

20

17

186

5 8

10

9

Before deletion

510

9

23

20

25

24

18

After deletion

29

����� ���������� ������ �� ������������ ��%�������!�����������������"����

�������������� ��������� �������������� �������������������+�� ���!������%���������

��� �� !���������������������� ������������ ������������0������� �

18

20

23

25

24 29

17

5

6

7

8

9Before deletion

20

18

17

5

6

9

10

24

25

23

After deletion

10

7

29

��$��������������������� �������� �� ����� �������������

�3*4567-8�

�����. ����

�����. -��������!��!�����

�����. ���� �������!������������

�����. 1���3��4%�����

����7. H�����:IF2

����@. 1������!�������

������������ ������ ��!$

����A. ��������>�

����B. I������!���������� �

����E. < ��� ���

�����8. �����>�������������������� �

������. H������������

������. < ����

������. <����>��������3����%����4�� ����

������. ��������>�

�����7. < ����������� ��

�����@. ���� ��3����%����4�� ����

�����A. ��������>�

�����B. �������� �3����4

�����E. ���������� �3����4

�����8. �������� �3����4

������. D��%�� ����������

������. ����

9���� �����# ����

�����. ����

�����. ����>��!��

�����. '������������������

�����. ���+J ��>���

����7. ���+J���>�!��

����@. ���+J��>�!��

����A. ��!������������� ��

����B. ������K�3�+J �4

����E. �+J��>���������3+J��%��4

��"� ������������ ������

�����8. �+J��>���������3+J��%�4

������. <�!����

������. ����

,���������������!������

�����. ����

�����. ">

�����. �����"L>�!��

�����. ���"+J ��>�

����7. D��

����@. H����>"

����A. ����K"+J �

����B. �>���

����E. �>���

�����8. ���"��L>�!���MM"��L>�!��

������. �����>"

������. �������>>�!��

������. ������>>�!��

������. ������>>�!��

�����7. ����+J��>"+J��

�����@. ����+J��>"+J��

�����A. 1���������+J��>�!��

�����B. �����>�����+J�

�����E. "+J �>�����+J �

�����8. ">�����

������. <�!���

������. ����

������������ ������ ��"

'������ $��������������� �������� �� ����� �������������

:� ����������������

���A��B��E���8�������������������7

-���������8

D������������ �����

���A��B��E��������������������7