Posts

Showing posts from August, 2019

Assignment 5 Question 1

#include <stdio.h> int collatz(int n, int count) { if(n==1){ return count; } if(n%2==0){ n=n/2; count++; }else if(n%2!=0){ n=3*n+1; count++; }     return collatz(n,count); } int main() { int n,flag; scanf("%d", &n); flag=collatz(n,0); printf("%d", flag); return 0; }

Assignment 4 question 3

#include <stdio.h> int main() { char string[100]; int i; for(i=0;i<100;i++) { scanf("%c", &string[i]); } int flag=0; int a=0; for(i=1;i<100;i++){     if (*(string + a)==*(string+i))     {     a++;     i++;     flag++;     for(;*(string+a)==*(string+i);i++,a++)     {     flag++; } if(*(string+a)==*(string+i)){ break; }     } if (i==100){ a++; i=a+1; continue; } } if(flag==0){ printf("NO"); }else{ for(i=0;i<flag;i++) { printf("%c", string[i]); }} return 0; }

Assignment 4 Question 2

#include <stdio.h> int copy(int n){     int ptr[n];  int i=0;  int j=0;  int flag=0;  flag=n;  while(i<n){   scanf("%d", &ptr[i]);  j=0;   while(j<i){    if(*(ptr+j)==*(ptr+i)){     flag--;     break;    }    j++;   }   i++;  }  return flag; }   int main() {  int n;  scanf("%d", &n);  int b;  b=copy(n);  printf("%d", b);  return 0; }

Assignment 4 Question 1

Self done solution #include <stdio.h> int read(int arr[]){  int i,n;  scanf("%d",&n);  i=0;  while(i<n){   scanf("%d", &arr[i]);   i++;  }  return n; } int lowest(int* ptr,int* ptr1, int n, int n3){  int i=0;  int j=0;  int *ptr2;  int low;  ptr2=ptr1;  while(i<n){   if(*ptr==*ptr1){    i++;    ptr= ptr+1;    ptr1=ptr2;    j=0;   }   if (j==n3){    break;   }   ptr1 = ptr1 + 1;   j++;  }  low=*ptr;  return low; } int main(){  int a[20];  int n1,low1;  n1=read(a);  int b[20];  int n2;  n2=read(b);  low1=lowest(a,b,n1,n2);  if (low1==0){   printf("NO");  }else{   printf("%d", low1);          }  return 0; } previous year solution  #include<stdio.h>        #define MAX 20        int read_array(int arr[])        {         int i, n;         scanf("%d", &n);         for (i = 0; i < n; i++)         scanf("%d", &arr[i]);         return n;        }       int present(int arr[], int n, int elt) {

Assignment 3 Question 3

/* Name :- Vinayak Sharma SMVDU, Jammu ECE code description - counting the number of words. */ #include <stdio.h> int word_test(char a){ int count=0;     if ((a != ' ') && (a != ',') && (a != '\t') && (a != '.') && (a != ';')){     count=1;    }    return count; } int main() {     int c;     int flag=0;     int i=0;       c = getchar();         while ( c != EOF ) {     flag = flag + word_test(c);             if ((c == ' ') || (c == ',') || (c == '\t') || (c == '.') || (c == ';')){     if (flag > 0){     i++;     flag=0;    }    }            c = getchar();     }     printf("%d", i);     return 0; }

Assignment 3 Question 2

/* Name :- Vinayak Sharma SMVDU, Jammu ECE code description - To find the moving average of 3 numbers */ #include <stdio.h>  float avg(float a,float b,float c){   float x;   x= (a + b + c)/3;   return x;  }  int main()  {   float a;   float b;   float c;   float n;   float result;     scanf("%f %f %f", &a,&b,&c);     result = avg(a,b,c);   printf("%.1f", result );     scanf("%f", &n);     while(n != -1){   a=b;   b=c;   c=n;     result = avg(a,b,c);   printf(" %.1f", result );     scanf("%f", &n); } return 0;  }

Assignment 3 Question 1

/* Name :- Vinayak Sharma SMVDU, Jammu ECE code description - This code has a function for checking wether the enetered number is even or not. */ #include <stdio.h> int find_even(int k){ int res; int c=0; res = k%2; if (res == 0){ c = 1; }else { c=0; } return c; } int main() { int a=0; int b=0; int flag=0; int final=0; scanf("%d", &a); scanf("%d", &b); while (b != -1){ flag = flag + find_even(b); if (flag == a){ final = b; break; } scanf("%d", &b); } if (final != 0){ printf("%d", b); }else{ printf("-1"); } return 0; }

Assignment 2 Question 3

/* Name :- Vinayak Sharma SMVDU, Jammu ECE code description - This code runs on the logic of while looping. the loop will take input and then store it into one variable until -1 is hit. it will also keep adding and storing the variable into a seprate variable s if only 1 variable is entered the code will exit displaying 0 , if more than 2 it will display 1. */ #include <stdio.h> int main() {  int curr;  int prev;  int sum=0;  scanf("%d", &prev); if (prev != -1)  // if the first number is -1 we will exit the loop {   sum = 1;       scanf("%d", &curr);      while (curr != -1)  // again if the 2nd number is -1 we will exit the loop  {   if (prev==curr){   sum = sum - 1;   }   sum++;   prev = curr;   scanf("%d", &curr);  } } else   {    sum = 1;   } if (sum == 1){ printf("0"); } if (sum > 1){ printf("1"); }  return 0; }

Assignment 2 Question 2

/* Name :- Vinayak Sharma SMVDU, Jammu ECE code description - finding the bigger increasing or decreasing sequence */ #include <stdio.h> int main() {  int curr;  int prev;  int sum=0;  scanf("%d", &prev); if (prev != -1)  // if the first number is -1 we will exit the loop {   sum = 1;       scanf("%d", &curr);      while (curr != -1)  // again if the 2nd number is -1 we will exit the loop  {   if (prev==curr){   sum = sum - 1;   }   sum++;   prev = curr;   scanf("%d", &curr);  } } else   {    sum = 1;   }  printf("%d", sum);  return 0; }

Assignment 2 Question 1

/* name: vinayak sharma smvdu, jammu ECE code description: finding triangular matrix */ #include <stdio.h> int main() { int j=0; int i=0; int n; int a; int b=0; int req=0; int max=0; int x=0; int y=0; int z=0; int flag1=0; int flag2=0; int res=0; scanf("%d", &n); req = (((n*n) - n)/2);   // both the formula will help us know the number of zeroes we require for getting the triangular matrix max = ((n*n) - n); while (j<n)       // this loop is for numbering the rows and columns { i=0; while (i<n)   // Main input of the matrix will go into this loop { scanf("%d",&a); if (i != j)   // for detecting values outside the diagonal { if (a == 0) { b = b + 1;  // every time we detect a zero we will store it in the b variable } if (!(a == 0))     // for detecting the special case bug { x = i; y = j;

Assignment 1 Question 3

/* name: vinayak sharma smvdu, jammu ECE code description: user enters 3 numbers. the 3 numbers get compared, if a>b>c then result is diplayed as 1 or else 0. */ #include <stdio.h> int main()   {   int a, b, c;   scanf("%d", &a); //this will input a   scanf("%d", &b); //this will input b   scanf("%d", &c); //this will input c     if (( a>b ) && ( b>c ) && ( a>c )) // logical operation comparing the 3 numbers   {     printf("1");   }   else //if the statement holds false then this else statement will be displayed   {     printf("0");   }   return 0; }   

Assignment 1 Question 2

/* name: vinayak sharma smvdu, jammu ECE code description: checking if m is exact multiple of n. output will be 0 if m is not a multiple of n. output will be m/n if m is a multiple of n */ #include <stdio.h> int main() {   int m, n, a;      //decleration of variables   scanf("%d", &m);  //taking the input of m   scanf("%d", &n);  //taking the input of n     if ( m%n == 0)    //logical operation for checking if m is a multiple of n   {     a = m/n;        // m divided by n stored in a variable     printf("%d",a);   }   else              //if the logic holds false this else statement will be carried out    {     printf("0");   }   return 0; }

Assignment 1 Question 1

/* Name :- Vinayak Sharma SMVDU, jammu ECE code description:- This code will compare the entered input numbers. if a number is repeated more than once the output will be 0 and if all the numbers are different the output will be 1. */ #include <stdio.h> int main() {     int a, b, c;     scanf("%d", &a); /*this will input the first number*/     scanf("%d", &b); /*this will input the second number*/     scanf("%d", &c); /*this will input the third number*/         if ((a == b) || (b == c) || (a == c)) /*if statement with OR operator which will check if 2 or more numbers are same*/                                          {                                             printf("0"); /* this will print 0 */                                          }     else   /* if no number is identical this else statement will run and display the result as 1 */             {                 printf("1"); /* this will prin

Assignment 0 Question 2

/* Name :- Vinayak Sharma SMVDU, jammu ECE code description:- This code will take the input of 2 numbers and then calculate their average. Ultimately displaying the result! */ #include <stdio.h> int main() { int a, b, avg; scanf("%d", &a); /* this will take the input of first number */ scanf("%d", &b); /* this will take the input of 2nd number */ if ((a%2 == 0) & (b%2 == 0)) /* this if statement will check wether the numbers entered are even or odd, if odd then the calculation will be carried out. */ { avg = (a + b)/2 ; /* mathematical operation which will conduct the average and then store it in the avg variable */ printf("%d", avg);  /* final statement which will display the average of 2 numbers */ } else { printf("one or both of the numbers entered by the user was a odd number \n"); } return 0; }

Assignment 0 Question 1

#include <stdio.h> int main() { printf("Hello C \n"); /* printf will help in displaying output as Hello C on the output screen */ return 0; }