vihika singhal

Student at chandigarh university

Structures Using C#

using System; struct Books { public string title; public string author; public string subject; public int book_id; }; public class testStructure { public static void Main(string[] args) { Books Book1; /* Declare Book1 of type Book */ Books Book2; /* Declare Book2 of type Book */ /* book 1 specification */ Book1.title = "C Programming"; Book1.author = "Nuha Ali"; Book1.subject = "C Programming Tutorial"; Book1.book_id = 6495407; /* book 2 specification */ Book2.title = "Telecom Billing"; Book2.author = "Zara Ali"; Book2.subject = "Telecom Billing Tutorial"; Book2.book_id = 6495700; /* print Book1 info */ Console.WriteLine( "Book 1 title : {0}", Book1.title); Console.WriteLine("Book 1 author : {0}", Book1.author); Console.WriteLine("Book 1 subject : {0}", Book1.subject); Console.WriteLine("Book 1 book_id :{0}", Book1.book_id); /* print Book2 info */ Console.WriteLine("Book 2 title : {0}", Book2.title); Console.WriteLine("Book 2 author : {0}", Book2.author); Console.WriteLine("Book 2 subject : {0}", Book2.subject); Console.WriteLine("Book 2 book_id : {0}", Book2.book_id); Console.ReadKey(); } }

Python code to calculate age in years

from datetime import date    def calculateAge(birthDate):     today = date.today()     age = today.year - birthDate.year -           ((today.month, today.day) <          (birthDate.month, birthDate.day))     return age print(calculateAge(date(1997, 2, 3)), "years")

Python program to check whether a number is Prime or not

num = 11 if num > 1:    for i in range(2, num//2):                  # 2 and n / 2, it is not prime         if (num % i) == 0:            print(num, "is not a prime number")            break    else:        print(num, "is a prime number")    else:    print(num, "is not a prime number")

Python program to find second maximum value in Dictionary

new_dict ={"rahul":12, "amma":9, "flipkart":4, "chd":13} max = max(new_dict.values())    max2 = 0 for v in new_dict.values():      if(v>max2 and v             max2 = v    print(max2)

Python3 program to add two numbers

num1 = 15 num2 = 12    # Adding two nos sum = num1 + num2    # printing values print("Sum of {0} and {1} is {2}" .format(num1, num2, sum))

Generating subarrays using recursion

#include <iostream> # include <vector> using namespace std; void printSubArrays(vector<int> arr, int start, int end) {           if (end == arr.size())          return;     else if (start > end)          printSubArrays(arr, 0, end + 1);     else     {         cout << "[";         for (int i = start; i < end; i++){             cout << arr[i] << ", ";         }         cout << arr[end] << "]" << endl;         printSubArrays(arr, start + 1, end);     }      return; } int main() {    vector<int> arr = {1, 2, 3};    printSubArrays(arr, 0, 0);    return 0; }

Class versus Structure C#

using System; struct Books { private string title; private string author; private string subject; private int book_id; public void getValues(string t, string a, string s, int id) { title = t; author = a; subject = s; book_id = id; } public void display() { Console.WriteLine("Title : {0}", title); Console.WriteLine("Author : {0}", author); Console.WriteLine("Subject : {0}", subject); Console.WriteLine("Book_id :{0}", book_id); } }; public class testStructure { public static void Main(string[] args) { Books Book1 = new Books(); Books Book2 = new Books(); Book1.getValues("C Programming", "Nuha Ali", "C Programming Tutorial",6495407); Book2.getValues("Telecom Billing", "Zara Ali", "Telecom Billing Tutorial", 6495700); Book1.display(); Book2.display(); Console.ReadKey(); } }

Iterate characters in reverse order

string_name = "Dehradun"    # slicing the string in reverse fashion  for element in string_name[ : :-1]:     print(element, end =' ') print('\n')   string_name = "Chandigarh"    # Getting length of string ran = len(string_name)    # using reversed() function for element in reversed(range(0, ran)):     print(string_name[element])