Skip to content
Related Articles

Related Articles

Program to print DNA sequence
  • Difficulty Level : Basic
  • Last Updated : 18 Apr, 2018

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




// 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 lobe
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--)
            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 lobe
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++)
            cout << " ";
        cout << first;
        for (int j = 4 - i; j >= 1; j--)
            cout << "--";
        cout << second << endl;
    }
}
  
// Function to print 'n' parts of DNA
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]);
    }
}
  
// Driver function
int main()
{
  
    int n = 8;
      
    // combinations stored in the array
    string DNA[] = { "ATTAATTA", "TAGCTAGC", "CGCGATAT",
                   "TAATATGC", "ATCGTACG", "CGTAGCAT" };
    printDNA(DNA, n);
      
    return 0;
}


Java




// 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 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.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 lobe
static 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 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 (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


Python3




# Python  Program to print the 
# 'n' lobes of DNA pattern
import math
  
# Function to print upper half 
# of the DNA or the upper lobe
def 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 lobe
def 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 DNA
def 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 code
n = 8
  
# combinations stored in the array
DNA = [ "ATTAATTA", "TAGCTAGC", "CGCGATAT",
      "TAATATGC", "ATCGTACG", "CGTAGCAT" ]
  
printDNA(DNA, n)
  
# This code is contributed by Gitanjali.


C#




// C# Program to print the 'n' lobes of
// DNA pattern
using 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
// PHP implementation to print the 
// 'n' lobes of DNA pattern
  
// Function to print upper half 
// of the DNA or the upper lobe
function 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 lobe
function 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 DNA
function 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.
?>



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



My Personal Notes arrow_drop_up
Recommended Articles
Page :