C basic programs :=0

NPTEL Introduction to Programming in C  Assignment 1 & 2:-

       Assignment 1:-

             program 1:

You will be given 3 integers as input. The inputs may or may not be
 different from each other.

You have to output 1 if all three inputs
 are different from each other,
and 0 if any input is repeated more
 than once.



Input
-----
Three integers on three lines.



Output
------
1 if the three inputs are different from each other
, 0 if some input is repeated more than once

#include <stdio.h>


int main(){
int a,c,b;
  scanf("%d",&a);
        scanf("%d",&b);
        scanf("%d",&c);
  if((a != b) && (a!= c) && ( b != c )){
               printf("1");
         }else{
               printf("0");
    }
}


           program 2:

You are given two integers, say M and N.

You must check whether M is an exact multiple of N, without using
loops.
You have to output 0 if M is not a multiple of N.
You have to output M/N if M is a multiple of N.

Input-----
Two integers, say M and N.

Output
------
You have to output 0 if M is not a multiple of N.
You have to output M/N if M is a multiple of N.

#include <stdio.h>

void main(){
int m,n;
  scanf("%d %d",&m,&n);
  if( m % n == 0)
        {
                printf("%d",m/n);
        }else{
               printf("0");
        }
}

          program 3:-

A triple of numbers (a,b,c) is called a triangle triple if we can form a triangle of lengths a, b and c.

In this question, you will be given three numbers. You have to output
1 if the three numbers can form a triangle. Otherwise, you have
to output 0.

Input-----Three real numbers.

Output------1 if the three numbers can form a triangle.
                   0 otherwise

#include <stdio.h>

int main(){
 float a , b , c;
  scanf("%f\n%f\n%f",&a,&b,&c);
    if((a < b + c) && (b < a + c) && (c < b + a)){
        printf("1");
    } else{
        printf("0");
    } 
   return 0;
}
Here you need to understand if () it says  ( a < b + c ) as per the inequality theorem

Assignment 2:

       program 1:

Level: Medium

In this assignment, you will be given an NxN matrix. You have to
determine whether the matrix is a triangular matrix. 

The diagonal of the matrix M of size NxN is the set of entries M(0,0),
M(1,1), M(2,2), ..., M(N,N). 

A matrix is upper triangular if every entry below the diagonal is
0. For example,  
1 1 1
0 0 1
0 0 2
is an upper triangular matrix. (The diagonal itself, and the entries
above and below the diagonals can be zeroes or non-zero integers.) 

A matrix is lower triangular if every entry above the diagonal is
0. For example, 
2 0 0
3 1 0
4 2 2
is a lower triangular matrix. 

A matrix is triangular if it is either upper triangular or lower
triangular or both.  

You may not use arrays for this program.

Input
First, you will be given N, which is the size of the matrix.

Then you will be given N rows of integers, where each row consists of
N integers separated by spaces. 

Output

If the input matrix is triangular, then print yes. Otherwise, print
no. 

#include<stdio.h>

int main(){
int n,d;  // n for matrices length // d for data(value)
  int upp=1,down=1; // flags
int i,j; // iteration variables
  scanf("%d",&n);
  for(i = 0 ; i < n ; i++){
          for(j = 0 ; j < n ; j ++){
            scanf("%d",&d);
            if(j >i){
               if(d != 0)
                   upp = 0; // means upper part of matrices is not 0
             }if(j < i){
                 if(d != 0)
                   down = 0; // means lower part of matrices is not 0
             } 
         }
    }
     if(upp || down){
         printf("yes\n");
     }else{
         printf("no\n");
     }
}
This is a bit complex so I will Explain it.
 First for loop is for the ith index of matrices
         jth ---->
ith     0    0   0
   |     1   34  4
   |    -1   23  7

if conditions 
    1st if( j > i)
    always the upper part of matrix excluding diagonal is always greater than diagonal's index.
    always the lower part of matrix excluding diagonal is always smaller than diagonal's index.
     lets's see 
     diagonal of 3x3 matrix is
      
      i=j=0     i=0 , j=1  i=0,j=2        |
      i=1,j=0    i=j=1     i=1,j=2        |     upper is > diagonal
      i=2j=0   i=2,j=1    i=j=2           |     lower is < diagonal

program 2:

You are given a sequence of integers as input, terminated by a
-1. (That is, the input integers may be positive, negative or 0. A -1
in the input signals the end of the input.) 

-1 is not considered as part of the input.  

Find the second largest number in the input. You may not use arrays.

#include <stdio.h>

int main(){
  int lg,slg;
  int d;
  scanf("%d",&d);
  lg = d;       // largest
  slg = d;     // second largest
  while(d != -1)
  {
    if(d > slg && d < lg){
    slg = d;
    }else if(d > lg){
    slg = lg;    // this is main line.... if something is > lg then lg becomes slg..
          lg = d;
    }
    scanf("%d",&d);
  }
  printf("%d",slg);
}

program3

You are given a sorted (either in the increasing or in the decreasing order) sequence of numbers, ending with a -1. You can assume that are at least two numbers before the ending -1. 

Let us call the sequence x0 x1 ... xn -1.

You have to output the number of distinct elements in the sorted sequence.

Kindly do not use arrays in the code. 


#include <stdio.h>



int main(){

 

int d1,d2;     // input is sorted so two variables to store two values

  int count=1;
  scanf("%d %d",&d1,&d2);
  while(d2 != -1){
           if(d1 != d2){
            count++;
             }
              d1 = d2;   // now set d1 (first ) to d2 (second).
              scanf("%d",&d2);  // now get new d2(second).
         }
  printf("%d\n",count);
return 0;
}


Guys if you liked it. Please subscribe and Stay tuned!!!


Comments

Popular posts from this blog

Code to Implement Dynamic Queue in C++ using arrays

NPTEL Introduction to programming in c Assignment 4 solutions

C PROGRAM TO ECHO "*" - C PROGRAM TO GET PASSWORD FROM USER