Pattern Printing – 16

 
 


#include <stdio.h>

void main()
{
    int numRows = 10;
    int row, col;
    int count = 1;
    for (row = 1; row <= numRows; row++)
    {
        count = row;
        for (col = 1; col <= row; col++)
        {

            int remainder = (count) % 2;
            count++;
            printf("%d ", remainder);

        }
        printf("\n");

    }

}

Pattern Printing 15

Figure 1

To understand and compute above type of pattern we follow the following steps:

  1. We will divide the pattern into two parts:
  2. Now the two parts of the pattern are solved in the following way:- As you can see, the upper part seems like combination of three different patterns that we have studied (marked with three different colors)

    - Solving these three parts individually and analyzing it, we can see that the outer loop in all the three patterns is same.

    
    // n in the user input
    
    // for blue region
    for (i = n; i >= 1; i--)
    {
        for (j = n; j >= i; j--)
            printf("%d", j);
        printf("\n");
    }
    /* -----------------------------------*/
    // for brown region
    for (i = n; i >= 1; i--)
    {
        // loop to print blank spaces
        for (j = n - i; j >= 1; j--)
        	printf(" ");
    
        // loop to print pattern
        for (j = 1; j < (i * 2) - 1; j++)
            printf("%d", i);
        printf("\n");
    }
    /* -----------------------------------*/
    // for red region
    for (i = n; i >= 1; i--)
    {
       // loop to print blank spaces
       for (j = i-2; j >= 0; j--)
    	 printf(" ");
       
       // loop to print pattern
       for (j = i + 1; j <= n; j++)
             printf("%d", j);
       printf("\n");
    }
    
    
    

    Now w.r.t this common part, we can combine all the patterns (removing blank spaces loop mentioned in the program) to form upper part of our pattern.

    - Same way lower part is solved.

    - Combining both upper and lower part, we come up with final program:

    
    #include<stdio.h>
    int main()
    {
        int i, j, k, n;
        
        printf("Enter n: ");
        scanf("%d", &n); // input value i.e. 3, 4
        printf("\n");
    
        // to print upper half
        for (i = n; i >= 1; i--)
        {
            // to print blue region
            for (j = n; j >= i; j--)
                printf("%d", j);
    
            // to print brown region
            for (j = 1; j < (i * 2) - 1; j++)
                printf("%d", i);
    
            // to print red region
            for (j = i + 1; j <= n; j++)
                printf("%d", j);
    
            printf("\n");
        }
    
        // to print lower half
        for (i = 2; i <= n; i++)
        {
            // to print red region
            for (j = n; j >= i; j--)
                printf("%d", j);
    
            // to print brown region
            for (j = 1; j < (i * 2) - 1; j++)
                printf("%d", i);
    
            // to print blue region
            for (j = i + 1; j <= n; j++)
                printf("%d", j);
    
            printf("\n");
        }
        return 0;
    }
    
    /*
    Output:
    
    Enter n: 3
    
    33333
    32223
    32123
    32223
    33333
    
    */
    

Printing Pattern -14

Untitled


/*
Logic:
Firstly we take input of max value and store it in n.
For the case shown in figure n=3

We divide the program in 3 parts ie upper half , centre * and lower half

For upper half:
for each row row_num starts with 0 till n-1:
-Number of starting spaces:2*(row_num)
-print stars: *
-Number of centre spaces : 4*(n-row_num)-1
-print stars: *
-new line

//for centre *
-number of spaces:2*n
-print *
-newline

Similar to upper half lower half is printed.

We have used i for row_num
-n rows in upper half
-print *
-n rows in lower half

*/



#include<stdio.h>
int main(){
int n,i,j;
printf("Enter maximum value\n");
scanf("%d",&n);

//for upper half

for(i=0;i<n;i++){
//print starting space
for(j=0;j<2*i;j++){
printf(" ");
}

//print stars
printf("*");

// print centre spaces
for(j=0;j<4*(n-i)-1;j++)
printf(" ");

//print stars
printf("*");

printf("\n");
}

//for centre *
for(j=0;j<2*n;j++){
printf(" ");
}

printf("*");

printf("\n");

//for lower half
for(i=0;i<n;i++){
//print starting spaces
for(j=0;j<2*(n-i-1);j++){
printf(" ");
}

//print stars
printf("*");

//print centre spaces
for(j=0;j<4*(i+1)-1;j++)
printf(" ");

//print stars
printf("*");

printf("\n");
}

return 0;
}

 

C Practical and Assignment Programs – Reverse given Number

In this video we will understand how to reverse given number. We will also look into the algorithm . And then we will be writing program to reverse give numbers. In this video we will also learn how to effectively improve our coding skills using function. And also see the advantages of functions.  Now this program is written in C programming language but will work in C++. Fell free to provide you  suggestion.


#include<stdio.h>

int reverse(int number){
int ans=0;
while(number!=0){
int units=number%10;
ans=ans*10+units;
number=number/10;
}
return ans;
}

void main(){

int a[]={234,563,11,985,568,235,<wbr />456};
int size=7;
int i=0;
for(i=0;i<7;i++){
int rev;
rev=reverse(a[i]);
printf("Original number: %d  Reversed Number %d \n",a[i],rev);
}

}

Printing Pattern-12

Untitled


/*
Logic:
1.First we take input of number of rows(n).
2.For each row(row num starts from 0 till n-1):
-Number of "-" equals the row number.
-Number of "*" equals (n-row_num)
3. New Line.
*/

#include<stdio.h>
int main(){
int i,j,n;
printf("Enter the number of rows\n");
scanf("%d",&n);
for(i=0;i<n;i++){
for(j=0;j<i;j++){
printf("-");
}
for(j=0;j<n-i;j++){
printf("*");
}
printf("\n");
}
return 0;
}

Printing Pattern – 11

Untitled


/*
Logic:
Firstly we take input of number of rows.
For each row (row num goes from 0 to n-1)
-print spaces (num of spaces = total num of rows minus row num minus 1)
-print numbers starting with row number decrementing upto 0
-print numbers starting with 1 incrementing upto row num
-newline

*/

#include<stdio.h>
int main(){
int n,i,j;
printf("Enter number of rows\n");
scanf("%d",&n);

for(i=0;i<n;i++){

//print spaces
for(j=0;j<n-i-1;j++)
printf(" ");

//print decrementing numbers
for(j=i;j>=0;j--)
printf("%d",j);

//print incrementing numbers
for(j=1;j<=i;j++)
printf("%d",j);

//newline
printf("\n");
}

return 0;
}

Pascal Triangle element

Untitled


#include<stdio.h>

//it calculates factorial of a given number

int factorial(int t){

int fact=1;

while(t>0){

fact*=t;

t--;

}

return fact;

}

&nbsp;

void main(){

printf("Enter the row number\n");

int r;

scanf("%d",&r);

printf("Enter the column number\n");

int c;

scanf("%d",&c);

r--;//since row number must start from 0

c--;//since column number must start from 0

//formula to find any any element from pascals triangle

//          nCr= n!/(r!*(n-r)!)

//where n=row number

//                      r=column number

int nCr=factorial(r)/(factorial(c)*factorial(r-c));

printf("Value of element in pascals triangle is %d \n",nCr);

}

Printing Pattern-9

Capture/*
Logic:
Firstly we take input of max value and store it in n.
For the case shown in figure n=4

We divide the program in 3 parts ie upper half , zero and lower half

For upper half:
for each row row_num starts with 0 till n-1:
-Number of starting spaces:2*(row_num)
-print number: n-row_num
-Number of centre spaces : 4*(n-row_num)-1
-print number: n-row_num
-new line

//for zero
-number of spaces:2*n
-print 0
-newline

Similar to upper half lower half is printed.

We have used i for row_num
-n rows in upper half
-print 0
-n rows in lower half

*/

#include<stdio.h>
int main(){
int n,i,j;
printf("Enter maximum value\n");
scanf("%d",&n);

//for upper half

for(i=0;i<n;i++){
//print starting space
for(j=0;j<2*i;j++){
printf(" ");
}

//print number
printf("%d",n-i);

// print centre spaces
for(j=0;j<4*(n-i)-1;j++)
printf(" ");

//print number
printf("%d",n-i);

printf("\n");
}

//for 0
for(j=0;j<2*n;j++){
printf(" ");
}

printf("0");

printf("\n");

//for lower half
for(i=0;i<n;i++){
//print starting spaces
for(j=0;j<2*(n-i-1);j++){
printf(" ");
}

//print number
printf("%d",i+1);

//print centre spaces
for(j=0;j<4*(i+1)-1;j++)
printf(" ");

//print number
printf("%d",i+1);

printf("\n");
}

return 0;
}

Printing Pattern – 8

Untitled/*
Logic:
1.Take input of number of rows.
2.create a count variable and initialize it to 1.
3.For each row
-Number of elements is equal to row number
the 1st row has 1 element 2nd has 2 and so on…
-for each element print the value of count.
-increment count.
4.new line
*/


#include<stdio.h>
int main(){
int n,i,j,cnt=1;
printf("Enter number of rows\n");
scanf("%d",&n);
for(i=1;i<=n;i++){

// printing each row
for(j=1;j<=i;j++){
printf("%d ",cnt++);
}
printf("\n");
}
return 0;
}

The Battle: Oracle v. Google

A federal court has reportedly ruled in favour of Oracle, and ordered Google to pay the company for its use of the Java API in Android, drawing criticism from various legal scholars.

APIs are one of the most important tools in modern programming, allowing third-party services to pull information automatically from central services like Google, Facebook and Twitter. (Apps like Tweetdeck, for instance, get your tweets by calling on Twitter’s API.) In this case, Google the Android OS on top of a modified version of Java, but kept Java’s API to make it easier for programmers to write for Android. Since many coders were already familiar with the quirks of Java’s API, the decision gave them a head start in writing programs for Android — but from the beginning, Oracle saw that privilege as belonging to them . In May of 2012, a district court ruled that copyrighting the calls would simply tie up “a utilitarian and functional set of symbols,” and gave Google free rein on the API. Oracle appealed the ruling, and two years later, a federal court has overturned. The next step is the Supreme Court, but it could be years before the issue is finally settled.

“The law is already clear that computer languages are mediums of communication and aren’t copyrightable. Even though copyright might cover what was creatively written in the language, it doesn’t cover functions that must all be written in the same way,” said EFF Staff Attorney Julie Samuels. “APIs are similarly functional – they are specifications allowing programs to communicate with each other. As Judge Alsup found, under the law APIs are simply not copyrightable material.”

“Without the compatibility enabled by APIs that are open, we would not have the vibrant computer and Internet environment we experience today, with new products and services routinely changing the way we see and interact with the world,” said EFF Fellow Michael Barclay. “APIs that are open spur the development of software, creating programs that the interface’s original creator might never have envisioned.”

The case is far from over. Google will probably go for a hearing from the full court, or appeal to the Supreme Court. If it does not decide to do that, Google can also focus on banking on a fair use defense, and hope that fair use can prevent hampering innovation. There is widespread confidence that Google can succeed using even this strategy, it is just a sad day for Computer Science if they have to.

Judgement: ORACLE AMERICA, INC. v. GOOGLE INC.

Source: EFF | The Verge

Follow

Get every new post delivered to your Inbox.