Showing posts with label Programs. Show all posts
Showing posts with label Programs. Show all posts

Chakravyuh [TCS Codevita Question]

#include <stdio.h>
 int main()
 {
    int a[5][5];
    int c=1, i, j, k, n;
    for(i=0;i<3;i++)
    {
        if(a[2][2]==25)
        {
            break;
        }
        for(j=0+i;j<=4-i;j++)
        {
            a[i][j]=c;
            c++;
        }
        n=j-1;
        for(k=1+i;k<=4-i;k++)
        {
            a[k][n]=c;
            c++;
        }
        n=k-1;
        for(j=3-i;j>=0+i;j--)
        {
            a[n][j]=c;
            c++;
        }
        n=j+1;
        for(k=3-i;k>=1+i;k--)
        {
            a[k][n]=c;
            c++;
        }
    }
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            printf("%d\t", a[i][j]);
        }
        printf("\n\n");
    }
    return 0;
 }


 1     2    3    4    5
 16   17   18   19    6
 15   24   25   20    7
 14   23   22   21    8
 13   12   11   10    9

Find length of string without built-in function

#include <stdio.h>
 int main()
 {
   char s[1000];
   int i=0;
   printf("Enter a string: ");
   scanf("%s", s);
   while(s[i] != '\0')
   {
      i++;
   }
   printf("Length of string: %d", i);
   return 0;
 }
Explaination of the Program : Here, in this program, we have declared one char data type variable named s and other variable of integer data type which is initialized to 0. Accepting the string from the user of whom the length is to be found. While loop is used and then condition is the loop will until the null character isn't identified. Inside the loop, we are incrementing the value of i and so we can get the value of the length which i itself at the end of the loop.

Concat two strings without built-in function

#include <stdio.h>
 int main()
 {
   char s1[100], s2[100], i, j;
   printf("Enter first string: ");
   scanf("%s", s1);
   printf("Enter second string: ");
   scanf("%s", s2);
   for(i = 0; s1[i] != '\0'; ++i);
   for(j = 0; s2[j] != '\0'; ++j, ++i)
   {
      s1[i] = s2[j];
   }
   s1[i] = '\0';
   printf("After concatenation: %s", s1);
   return 0;
 }
Explaination Of the Program : Here, we are declaring two char data type variables which can store two strings. We are accepting two strings and storing them in s1 and s2. Now in the first for loop, the condition is that for loop will work it the s1's null character isn't identified. Once, it is identified the loop will get terminated. The first for loop only gets the i counter to the end of the first string. Now in the second for loop, we are going to copy the data 
of the s2 in s1 and the data will get copied at the end of the s1 because the counter has reached there. Adding the null character after the copying is done and then we are printing the string s1 which concates the data of s2 inside s1.

Copy string without built-in function

 #include <stdio.h>
 int main()
 {
   char s1[100], s2[100], i;
   printf("Enter string s1: ");
   scanf("%s",s1);
   for(i = 0; s1[i] != '\0'; i++)
   {
      s2[i] = s1[i];
   }
   s2[i] = '\0';
   printf("String s2: %s", s2);
   return 0;
 }
Explaination of the Program : Here, we declared two variables of data type char which can store two strings. In one string, we are going to enter the string which the users will input. And then we will use for loop, the condition will be till the null character of string isn't identified. Then inside the loop, we are copy the data from one string to another string and the loop gets terminated when the null character is identified. Now we are adding the null character to string two and then printing it's value.

Program : Factorial Using Recursion

#include <stdio.h>
 long int fact(int n);
 int main()
 {
    int n;
    printf("Enter a Positive Integer: ");
    scanf("%d", &n);
    printf("Factorial of %d = %ld", n, fact(n));
    return 0;
 }
 long int fact(int n)
 {
    if (n == 0)
        return 1;   
    else
        return n*fact(n-1);
 }
Enter a positive integer: 6
 Factorial of 6 = 720
Explaination : In this program, we accept a positive integer from the user of whom we are going to find the factorial. The integer number is then passed in the User-defined Function named "fact". The condition is like, if the number gets equal to zero, then the it will return 1 or else it will return number times the fact of (n-1) i.e the function is calls itself. So, again the function "fact" will run for (n-1) and then like this goes on and on until the n values get 0 and it finally returns the value 1 so the functions terminates and returns that value to the main function.