Program to print DNA sequence

22 Jun, 2022
Given the value of n i.e, the number of lobes. Print the double-helix structure of Deoxyribonucleic acid(DNA). 
Input: n = 8
Output:
   AT
  T--A
 A----T
T------A
T------A
 G----C
  T--A
   GC
   CG
  C--G
 A----T
A------T
T------A
 A----T
  A--T
   GC
   AT
  C--G
 T----A
C------G
C------G
 T----A
  G--C
   AT
   AT
  T--A
 A----T
T------A
T------A
 G----C
  T--A
   GC
 
Explanation : DNA primarily consists of 4 hydrocarbons i.e. cytosine [C], guanine [G], adenine[A], thymine [T].DNA bases pair up with each other, A with T and C with G, to form units called base pairs.Below is the implementation to print the double-helix DNA sequence : 
CPP
Java
Python3
C#
PHP
Javascript
// CPP Program to print the// 'n' lobes of DNA pattern#include <bits/stdc++.h>using namespace std; // Function to print upper half// of the DNA or the upper lobevoid printUpperHalf(string str){    char first, second;    int pos = 0;         // Each half of the DNA is made of    // combination of two compounds    for (int i = 1; i <= 4; i++) {         // Taking the two carbon        // compounds from the string        first = str[pos];        second = str[pos + 1];        pos += 2;                 for (int j = 4 - i; j >= 1; j--)            cout << " ";        cout << first;        for (int j = 1; j < i; j++)            cout << "--";        cout << second << endl;    }} // Function to print lower half// of the DNA or the lower lobevoid printLowerHalf(string str){    char first, second;    int pos = 0;    for (int i = 1; i <= 4; i++) {         first = str[pos];        second = str[pos + 1];        pos += 2;                 for (int j = 1; j < i; j++)            cout << " ";        cout << first;        for (int j = 4 - i; j >= 1; j--)            cout << "--";        cout << second << endl;    }} // Function to print 'n' parts of DNAvoid printDNA(string str[], int n){    for (int i = 0; i < n; i++) {         int x = i % 6;                 // Calling for upperhalf        if (x % 2 == 0)            printUpperHalf(str[x]);        else                     // Calling for lowerhalf            printLowerHalf(str[x]);    }} // Driver functionint main(){     int n = 8;         // combinations stored in the array    string DNA[] = { "ATTAATTA", "TAGCTAGC", "CGCGATAT",                   "TAATATGC", "ATCGTACG", "CGTAGCAT" };    printDNA(DNA, n);         return 0;}
// Java Program to print the// 'n' lobes of DNA pattern import java.io.*; class GFG { // Function to print upper half// of the DNA or the upper lobestatic void printUpperHalf(String str){    char first, second;    int pos = 0;          // Each half of the DNA is made of    // combination of two compounds    for (int i = 1; i <= 4; i++) {          // Taking the two carbon        // compounds from the string        first = str.charAt(pos);        second = str.charAt(pos+1);        pos += 2;                  for (int j = 4 - i; j >= 1; j--)            System.out.print(" ");        System.out.print(first);        for (int j = 1; j < i; j++)            System.out.print("--");        System.out.println(second);    }}  // Function to print lower half// of the DNA or the lower lobestatic void printLowerHalf(String str){    char first, second;    int pos = 0;    for (int i = 1; i <= 4; i++) {          first = str.charAt(pos);        second = str.charAt(pos+1);        pos += 2;                  for (int j = 1; j < i; j++)            System.out.print(" ");        System.out.print(first);        for (int j = 4 - i; j >= 1; j--)            System.out.print("--");        System.out.println(second);    }}  // Function to print 'n' parts of DNAstatic void printDNA(String str[], int n){    for (int i = 0; i < n; i++) {          int x = i % 6;                  // Calling for upperhalf        if (x % 2 == 0)            printUpperHalf(str[x]);        else                      // Calling for lowerhalf            printLowerHalf(str[x]);    }}public static void main (String[] args) {    int n = 8;          // combinations stored in the array    String DNA[] = { "ATTAATTA", "TAGCTAGC", "CGCGATAT",                   "TAATATGC", "ATCGTACG", "CGTAGCAT" };    printDNA(DNA, n);                }} // This code is contributed by Gitanjali
# Python  Program to print the# 'n' lobes of DNA patternimport math # Function to print upper half# of the DNA or the upper lobedef printUpperHalf(str):     first=0    second=0    pos = 0         # Each half of the DNA is made of    # combination of two compounds    for i in range(1,5):          # Taking the two carbon        # compounds from the string        first = str[pos]        second = str[pos+1]        pos += 2                  for  j in range ( 4 - i, 0,-1):            print(" ",end="")        print(first,end="")        for  j in range (1, i):            print("--",end="")        print(second)        # Function to print lower half# of the DNA or the lower lobedef printLowerHalf(str):     first=0    second=0    pos = 0    for i in range(1,5):          first = str[pos]        second = str[pos+1]        pos += 2                  for  j in range(1,i):            print(" ",end="")        print(first,end="")        for  j in range (4 - i, 0,-1):            print("--",end="")        print(second)   # Function to print 'n' parts of DNAdef printDNA( str,  n):     for i in range(0,n):          x = i % 6                  # Calling for upperhalf        if (x % 2 == 0):            printUpperHalf(str[x])        else:                      # Calling for lowerhalf            printLowerHalf(str[x])     # driver coden = 8 # combinations stored in the arrayDNA = [ "ATTAATTA", "TAGCTAGC", "CGCGATAT",      "TAATATGC", "ATCGTACG", "CGTAGCAT" ] printDNA(DNA, n) # This code is contributed by Gitanjali.
// C# Program to print the 'n' lobes of// DNA patternusing System; class GFG {     // Function to print upper half    // of the DNA or the upper lobe    static void printUpperHalf(string str)    {                 char first, second;        int pos = 0;                 // Each half of the DNA is made of        // combination of two compounds        for (int i = 1; i <= 4; i++) {                 // Taking the two carbon            // compounds from the string            first = str[pos];            second = str[pos+1];            pos += 2;                         for (int j = 4 - i; j >= 1; j--)                Console.Write(" ");                     Console.Write(first);                         for (int j = 1; j < i; j++)                Console.Write("--");                             Console.WriteLine(second);        }    }     // Function to print lower half    // of the DNA or the lower lobe    static void printLowerHalf(string str)    {        char first, second;        int pos = 0;                 for (int i = 1; i <= 4; i++) {                 first = str[pos];            second = str[pos+1];            pos += 2;                         for (int j = 1; j < i; j++)                Console.Write(" ");                             Console.Write(first);                         for (int j = 4 - i; j >= 1; j--)                Console.Write("--");                             Console.WriteLine(second);        }    }     // Function to print 'n' parts of DNA    static void printDNA(string []str, int n)    {        for (int i = 0; i < n; i++) {                 int x = i % 6;                         // Calling for upperhalf            if (x % 2 == 0)                printUpperHalf(str[x]);            else                             // Calling for lowerhalf                printLowerHalf(str[x]);        }    }     public static void Main () {         int n = 8;         // combinations stored in the array    string []DNA = { "ATTAATTA", "TAGCTAGC",                     "CGCGATAT", "TAATATGC",                     "ATCGTACG", "CGTAGCAT" };                          printDNA(DNA, n);    }} // This code is contributed by vt_m.
<?php// PHP implementation to print the// 'n' lobes of DNA pattern // Function to print upper half// of the DNA or the upper lobefunction printUpperHalf($str){    $pos = 0;         // Each half of the DNA is made of    // combination of two compounds    for($i = 1; $i <= 4; $i++)    {         // Taking the two carbon        // compounds from the string        $first = $str[$pos];        $second = $str[$pos + 1];        $pos += 2;                 for ($j = 4 - $i; $j >= 1; $j--)            echo " ";        echo $first;        for ($j = 1; $j < $i; $j++)            echo "--";        echo $second."\n";    }} // Function to print lower half// of the DNA or the lower lobefunction printLowerHalf($str){    $pos = 0;    for ($i = 1; $i <= 4; $i++)    {        $first = $str[$pos];        $second = $str[$pos + 1];        $pos += 2;                 for ($j = 1; $j < $i; $j++)            echo " ";        echo $first;        for ($j = 4 - $i; $j >= 1; $j--)            echo"--";        echo $second."\n";    }} // Function to print 'n' parts of DNAfunction printDNA($str, $n){    for ($i = 0; $i < $n; $i++)    {         $x = $i % 6;                 // Calling for upperhalf        if ($x % 2 == 0)            printUpperHalf($str[$x]);        else                     // Calling for lowerhalf            printLowerHalf($str[$x]);    }} // Driver code$n = 8;$DNA = array( "ATTAATTA", "TAGCTAGC",              "CGCGATAT", "TAATATGC",              "ATCGTACG", "CGTAGCAT" );printDNA($DNA, $n); // This code is contributed by mits.?>
<script> // javascript Program to print the// 'n' lobes of DNA pattern// Function to print upper half// of the DNA or the upper lobefunction printUpperHalf(str){    var first, second;    var pos = 0;          // Each half of the DNA is made of    // combination of two compounds    for (var i = 1; i <= 4; i++) {          // Taking the two carbon        // compounds from the string        first = str.charAt(pos);        second = str.charAt(pos+1);        pos += 2;                  for (var j = 4 - i; j >= 1; j--)            document.write(" ");        document.write(first);        for (var j = 1; j < i; j++)            document.write("--");        document.write(second+"<br>");    }}  // Function to print lower half// of the DNA or the lower lobefunction printLowerHalf(str){    var first, second;    var pos = 0;    for (var i = 1; i <= 4; i++) {          first = str.charAt(pos);        second = str.charAt(pos+1);        pos += 2;                  for (var j = 1; j < i; j++)            document.write(" ");        document.write(first);        for (var j = 4 - i; j >= 1; j--)            document.write("--");        document.write(second+"<br>");    }}  // Function to print 'n' parts of DNAfunction printDNA(str , n){    for (var i = 0; i < n; i++) {          var x = i % 6;                  // Calling for upperhalf        if (x % 2 == 0)            printUpperHalf(str[x]);        else                      // Calling for lowerhalf            printLowerHalf(str[x]);    }}var n = 8; // combinations stored in the arrayvar DNA = [ "ATTAATTA", "TAGCTAGC", "CGCGATAT",               "TAATATGC", "ATCGTACG", "CGTAGCAT" ];printDNA(DNA, n); // This code contributed by Princi Singh</script>
Output :  
   AT
  T--A
 A----T
T------A
T------A
 G----C
  T--A
   GC
   CG
  C--G
 A----T
A------T
T------A
 A----T
  A--T
   GC
   AT
  C--G
 T----A
C------G
C------G
 T----A
  G--C
   AT
   AT
  T--A
 A----T
T------A
T------A
 G----C
  T--A
   GC
 Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the input given.
Auxiliary Space: O(1), as we are not using any extra space.
Mithun Kumar
princi singh
Kirti_Mangal
rohitsingh57
pattern-printing
School Programming
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.