Vous êtes sur la page 1sur 7

Pattern programs with logics:

Assumptions:

Every patters is generated in 2 dimensional coordinates system XY plane


fourth Quadrant but will be representing Y with it magnitude only.
Once moved in X or Y Direction we cant go back to the previous coordinates.

Pattern 1
1 2 3 4
* 1 *
** 2 * *
*** 3 * * *
**** 4 * * * *

1. Let the Row represented by i.


2. Let the Columns are represented by j.
From the figure we can see that we have four rows but the columns are not
fixed.
So mapping I and j we get

i j
1 1
2 2
3 3
4 4

We got the relation as i==j so the j loop should execute till j becomes equal
to i.

i.e. j<=i

Code:

#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<4;i++)
{
for(j=1;j<=i;j++)
{
printf(" *");
}
printf("\n");
}
}

1
123
12345
1234567

#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<5;i++)
{
for(j=1;j<5-i;j++)
{
printf(" ");
}
for(j=1;j<=2*i-1;j++)
{
printf("%d",j);
}
printf("\n");
}
}

Switch case Statement:


It is used to select a particular group of statements from several group.
The general form of switch statement is

switch(expression1)
{
case expression 2: statement 1;
statement 2;
statement 3;
statement n;

case expression 3: statement 1;


statement 2;
statement 3;
statement n;

case expression m:
statement 1;
statement 2;
statement 3;
statement n;
default: statement 1;
}
The expression 1 is the switch variable. It can be of either type integer or
character.
The expression 2 to expression m will be either an integer of character enclosed in
single quotes and are referred as case labels.
For example
case 1 or case a
Here statement n will be break statement to jump out the switch statement, if not
used the following cases will be executed automatically.

The last statement of switch will be default it is generally used to handle the
values for which we dont have the cases and is generally contains the error
messages.

Program to simulate basic operation of calculator


#include<stdio.h>
void main()
{
int choice;
float a,b,c;
printf("Enter Value of A");
scanf("%f",&a);
printf("Enter Value of B");
scanf("%f",&b);
printf("Enter your choice\n");
printf("1. Additon\n");
printf("2 Subraction \n");
printf("3.Multiplication\n");
printf("4.Division\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : c=a+b;
printf("Sum is %f",c);
break;
case 2 : c=a-b;
printf("Sum is %f",c);
break;
case 3 : c=a*b;
printf("Sum is %f",c);
break;
case 4 : c=a/b;
printf("Sum is %f",c);
break;
default: printf("Wrong Choice");
}
return;
}
Write a program in C language using switch case with the following
options
a. Area of circle
b. Area of Square
#include<stdio.h>
void main()
{
char choice;
float a,b,c;
printf("Enter Value of A");
scanf("%f",&a);
printf("Enter your choice\n");
printf("A. Area of circle\n");
printf("B. Area of square \n");
fflush(stdin);
scanf("%c",&choice);
switch(choice)
{
case 'A' :
case 'a' : c=3.14*a*a;
printf("Area of Circle %f",c);
break;

case 'B':
case 'b': c=a*a;
printf("Area of Square is %f",c);
break;
default : printf("Wrong Choice");
}
return;
}

Write a program using switch case to find the greatest among the given
two numbers

#include<stdio.h>
void main()
{
char choice;
int a,b,c;
printf("Enter Value of a");
scanf("%d",&a);
printf("Enter Value of b");
scanf("%d",&b);
switch(a<b)
{
case 0 : printf("A is grater than B");
break;

case 1 : printf("B is greater than A");


break;
}
return;
}

Break Statement:

The break statement is used to terminate the execution of loops and to exit from
the switch statement. It is a simple statement as
break;
The example of break in switch has been already discussed.
Now, we will discuss the example of use of breaks in loops.

Write a program to find the average of maximum n numbers but terminate


and display the result if the number is a negative number.

# include <stdio.h>
int main(){
float num,average,sum;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)
break; //for loop breaks if num<0.0
sum=sum+num;
}
average=sum/(i-1);
printf("Average=%.2f",average);
return 0;
}

Continue Statement
The continue statement is used to skip the remaining statement of current iteration
loops and directly jump to the next iteration. It is a simple statement as
continue;

Wap a program to find the product of n numbers and skip the particular number if it
is zero.

# include <stdio.h>
int main(){
float num,average,sum=1;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num==0.0)
continue; //for loop skip the iteration if num==0.0
sum=sum*num;
}
printf("Average=%.2f",sum);
return 0;
}

Arrays:

An array is a collection of homogenous data types stored at the contiguous location


referred by the common name.

The Advantages of an array are:


1. We can declare larger number of elements.
2. We can access them randomly.

The Disadvantage of arrays


1. Insertion and deletion at a particular position is very complex.
2. They are static in nature that means size cannot be modified at the run time.
3. The size of array should be known prior to the execution of the program.
4. If allocation is less than we cant store the complete data if we allocate more
a huge amount of memory will be wasted.

Types of arrays

We can classify the arrays into two categories


1. One dimensional array or single dimensional array
2. Multi dimensional Array

Syntax for declaring a single dimensional array

Datatype arrayname [size];

We can initialize the array element at the time of declaration


Example
Int a[5]={1,2,3,5,6};
Int a[]={1,2};
Int a[5]={1};

In example one array elements are assigned the value 1,2,3,5,6 respectively.
In second example we have not specified the size so the size of array will be two
based on the element to be assigned.

In third case first element is assigned a value 1 and remaining element are assigned
value 0. That is the default value for remaining element but if no element is
assigned a value it will be equal to garbage.

Vous aimerez peut-être aussi