Saturday, 12 March 2022

NPTEL - WEEK 4 ASSIGNMENT 4 - QUESTION 03

 WEEK 4 - ASSIGNMENT 4 - QUESTION 03

QUESTION :

Given a threshold floating point number and an array of floating point numbers strictly between 0 and 1.
    Modify the array with the following rules: If the number is greater than threshold value, change it to 1 and if less 
    than or equal to threshold value, change to 0.
    
    Output the sum of the modified array.
    Constraint: Array contains atmost 20 elements.

    Input: First line contains the threshold value.
           Next line gives the size of an array.
           Next line provides the content of an array.
          
    Output: Sum of modified array.

Example:   Input:
            0.3
            4
            0.1 0.9 0.11 0.98
           
           Modified array: 0 1 0 1

           Output : 2


SOLUTION :

#include<stdio.h>

int main()
{
    float num[20], tshld;
    int sum=0, n;

    scanf("%f", &tshld);
    scanf("%d", &n);

    for(int i=0; i<n; i++)
    {
        scanf("%f", &num[i]);
    }

    for(int i=0; i<n; i++)
    {
        if(num[i] > tshld)
        {
            num[i] = 1.0;
        }
        else if(num[i] <= tshld)
        {
            num[i] = 0.0;
        }
    }
    for(int i=0; i<n; i++)
    {
        sum += num[i];
    }
    printf("%d", sum);
    return 0;
}


NPTEL - WEEK 4 - ASSIGNMENT 4 - QUESTION 01

WEEK 4 - ASSIGNMENT 4 - QUESTION 01

QUESTION :

Given two arrays of integers output the largest number in the
first array not present in the second one.

Input: 

The first line contains the size of the first array.
Next line give the contents of the first array.
Next line contains the size of the second array.
Next line give the contents of the second array.

Output Format:
Output must be a single number which is the largest number occurring
in the first array that does not occur in the second. In case there is
no such number, output 0.

Variable Constraints:
The sizes of the arrays are smaller than 20.
Each array entry is an integer which fits an int data type.

Example:
Input:
3
2 3 4
4
1 4 5 7

Output: 3

Input
1
1
2
1 2

Output: 0

SOLUTION :

#include<stdio.h>

int main()
{
    int arr1[10], max, count, arr2[10], n=0, m=0;

    scanf("%d", &n);
    for(int i=0; i<n; i++)
    {
        scanf("%d", &arr1[i]);
    }

    scanf("%d", &m);
    for(int j=0; j<m; j++)
    {
        scanf("%d", &arr2[j]);
    }

    while(1)
    {
        max = 0;
        count = 0;
        for(int i=0; i<n; i++)
        {
            if(max < arr1[i])
            {
                max = arr1[i];
                count = i;
            }
        }
        for(int j=0; j<m; j++)
        {
            if(max == arr2[j])
            {
               arr1[count] = 0;
              
            }
           
        }
        if(arr1[count] != 0)
        {
            break;
        }
        if(max == 0)
        {
           break;
        }
    }
   
    printf("%d", max);
    return 0;
}

« iCode Buzz »

Friday, 11 March 2022

C Program To Print Fibonacci Series upto n terms Using a FUNCTION

 FIBONACCI SERIES

Code :

#include<stdio.h>
int fibonacci(int n);

int main()
{
    int n, term;

    printf("\t    FIBONACCI SERIES\n");
    printf("-------------------------------------\n");
    printf("Enter number of mximum terms : ");
    scanf("%d", &n);

    printf("\nFibonacci series upto %d terms,\n\n", n);
    for(int i=0; i<=n; i++)
    {
        printf("%d ", fibonacci(i));
    }
   
    printf("\n\n*************************************\n");
    return 0;
}

int fibonacci(int n)
{
    if(n == 0)
    {
        return 0;
    }
    else if(n == 1)
    {
        return 1;
    }
    else
    {
        return fibonacci(n-1) + fibonacci(n-2);
    }
}


C Program To Calculate Force of Attraction exerted by Earth on a Body Using a FUNCTION

 CALCULATE FORCE


Code :


#include<stdio.h>

float force(float mass);

int main()
{
    float force_val, m;
       
       
    printf("\tCALCULATE FORCE EXERTED BY EARTH\n");
    printf("------------------------------------------------\n");
    printf("Enter mass of the body in kgs : ");
    scanf("%f", &m);
   
    force_val = force(m);
   
    printf("\nThe value of Force in Newton is %.2f\n\n", force_val);
    printf("************************************************\n");
    return 0;
}

float force(float mass)
{
   float result;
   result = mass * 9.8;
   return result;
}


C Program To Convert Temperature Using a Function

 CONVERT TEMPERATURE

Code :

#include<stdio.h>

float convert_tempr(float cels);

int main()
{
    float celsius, fahrenheit;
   
    printf("\t   CONVERT TEMPERATURE\n");   
    printf("-------------------------------------------\n");

    printf("Enter temperature in Celsius : ");
    scanf("%f", &celsius);
   
    fahrenheit = convert_tempr(celsius);
   
    printf("\nEquivalent Fahrenheit temperature = %.2fF\n\n", fahrenheit);
    printf("*******************************************\n");
    return 0;
}

float convert_tempr(float cels)
{
    float fahr;
    fahr = (cels * 9/5) + 32;
   
    return fahr;
}


C Program To Find Average of 3 numbers Using a Function

 AVERAGE CALCULATOR FUNCTION

Code :

#include<stdio.h>

int average(int n1, int n2, int n3);

int main()
{
    int num1,num2,num3, avg;
   
    printf("\t  AVERAGE CALCULATOR\n");
    printf("--------------------------------------\n");
    printf("Enter 3 numbers to calculate average:\n");
    scanf("%d", &num1);
    scanf("%d", &num2);
    scanf("%d", &num3);
   
    avg = average(num1, num2, num3);
   
    printf("\nAverage = %d\n\n", avg);
    printf("**************************************\n");
    return 0;
}

// Function to calculate average.
int average(int n1, int n2, int n3){
    int sum, avg;
    sum = n1 + n2 + n3;
    avg = sum / 3;
    return avg;
}


Sunday, 6 March 2022

C Program To Find max and min term in the given number sequence

 Min term & Max term

Code :

#include<stdio.h>

int main()
{
    int seq, max=0, min=1;
   
    printf("====================================\n");
    printf("\t   Min and Max term\n");
    printf("====================================\n");
   
    printf("Enter sequence of numbers, \n(Terminated by -1) : ");
    scanf("%d", &seq);
    if(seq == -1)
    {
        printf("\n-----------------------------------\n");
        printf("You didn't have entered any sequence !!\n");
        printf("-----------------------------------\n");

        return 0;
    }
    while(seq != -1)
    {
        scanf("%d", &seq);
        if(seq == -1)
        {
           break;
        }
        if(max < seq)
        {
            max = seq;
        }
        if(min > seq)
        {
           min = seq;
        }
    }
    printf("\n-----------------------------------\n");
    printf("  Minimum term in sequence = %d\n", min);
    printf("  Maximum term in sequence = %d\n", max);
    printf("-----------------------------------\n");
    return 0;
}


C Program To Count Occurance of Given number in Given number sequence

 OCCURANCE COUNTER

Code :

#include<stdio.h>

int main()
{
    int n, seq, freq=0;

    printf("===================================\n");
    printf("\tOCCURANCE COUNTER\n");
    printf("===================================\n");

    printf("Enter a number : ");
    scanf("%d", &n);

    printf("\nEnter sequence of numbers\n(Terminated by -1) : ");
    while(seq != -1)
    {
        scanf("%d", &seq);
        if(seq == -1)
        {
            break;
        }
        if(seq == n)
        {
           freq++;
        }
    }
    printf("\n-----------------------------------\n");
    printf("  No of Occurance of %d = %d\n", n, freq);
    printf("-----------------------------------\n");
    return 0;
}


C Program To Count Parity in Given sequence of numbers

 PARITY COUNTER

Code :

#include<stdio.h>

int main()
{
    int total_en = 0, even_en = 0, odd_en = 0, seq=0;

    printf("==================================\n");
    printf("\t  PARITY COUNTER\n");
    printf("==================================\n");

    printf("Enter sequence of numbers \n(Terminated by -1) : ");   
    while(seq != -1)
    {
        scanf("%d", &seq);
        if(seq == -1)
        {
           break;
        }
        if(seq%2 == 0)
        {
            even_en++;
        }
        else
        {
            odd_en++;
        }
        total_en++;
    }
   
    printf("\nTotal number of entries = %d\n", total_en);
    printf("No of Evens = %d\n", even_en);
    printf("No of Odds = %d\n", odd_en);
    printf("-----------------------------------\n");
    return 0;
}


C Program To Print All Palindrome Numbers in Given Range

 ALL PALINDROME NUMBERS


Code :

#include<stdio.h>

int main()
{
    int n,s, rev=0,count = 0, temp, rem;

    printf("========================================\n");
    printf("\tPALINDROME NUMBER\n");
    printf("========================================\n");

    printf("Instrunctions : \nPalindrome is an 3 digit number so please enter lower limit above 99.\n\n");
    printf("Enter lower limit of numbers : ");
    scanf("%d", &s);

    printf("Enter upper limit of numbers : ");
    scanf("%d", &n);
    printf("\n");

    if(s < 100)
    {
        s = 100;
    }

    printf("All Palindrome numbers between %d and %d are,\n", s, n);

    for(int i=s; i<=n; i++)
    {
        temp = i;
        rev = 0;
        while(temp != 0)
        {
            rem = temp%10;
            rev = (rev * 10) + rem;
            temp /= 10;
        }

        if(rev == i)
        {
            printf("%d ", i);
            count++;
        }
    }
    if(count == 0)
    {
        printf("\nNO ANY PALINDROME NUMBER IN GIVEN RANGE !!\n");
    }
    printf("\n----------------------------------------\n");
    return 0;
}


C Program To Check Given number is Palindrome or Not

 PALINDROME NUMBER

Code :

#include<stdio.h>

int main()
{
    int num, rev=0,temp, rem;
   
    printf("========================================\n");
    printf("\tPALINDROME NUMBER\n");
    printf("========================================\n");
   
    printf("Enter any number : ");
    scanf("%d", &num);
    temp = num;
    printf("\n");
    while(temp != 0)
    {
        rem = temp%10;
        rev = (rev * 10) + rem;
        temp /= 10;
    }
   
    if(rev == num)
    {
       printf("%d is Palindrome Number.\n", num);
    }
    else
    {
       printf("%d is Not Palindrome Number.\n",num);
    }
    printf("----------------------------------------\n");
    return 0;
}


C Program To Find Sum of first n Odd number terms

 SUM OF N ODD TERMS

Code :

#include<stdio.h>

int main()
{
    int num,n,i=1, sum = 0,count=1;
   
    printf("========================================\n");
    printf("\tSUM OF FIRST N ODD TERMS\n");
    printf("========================================\n");
   
    printf("Enter maximum limit of odd terms : ");
    scanf("%d", &n);
   
    while(count <= n)
    {
        if(i%2 != 0)
        {
            sum += i;
            count++;
        }
        i++;
       
    }
    printf("\nSum of first %d odd terms is, ", n);
    printf("%d\n", sum);
   
    printf("----------------------------------------\n");
    return 0;
}


C Program To Find Sum of first n Even Terms

 SUM OF EVEN TERMS

Code :

#include<stdio.h>

int main()
{
    int num,n,i=1, sum = 0,count=1;
   
    printf("========================================\n");
    printf("\tSUM OF FIRST N EVEN TERMS\n");
    printf("========================================\n");
   
    printf("Enter maximum limit of even terms : ");
    scanf("%d", &n);
   
    while(count <= n)
    {
        if(i%2 == 0)
        {
            sum += i;
            count++;
        }
        i++;
       
    }
    printf("\nSum of first %d even terms is, ", n);
    printf("%d\n", sum);
   
    printf("----------------------------------------\n");
    return 0;
}