Skip to content

niteeshkr16/C-Module-3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Module-3


3a : C program that function accepts two integers and find the sum of all numbers in range from 1 to m(both inclusive) that are not divisible by n. Return difference between sum of integers not divisible by n with sum of numbers divisible by n.

AIM:

To write a C program that function accepts two integers and find the sum of all numbers in range from 1 to m(both inclusive) that are not divisible by n. Return difference between sum of integers not divisible by n with sum of numbers divisible by n.

ALGORITHM:

  1. Start
  2. Read n and m
  3. Set sum_div = 0, sum_not_div = 0
  4. For each number i from 1 to m:
  • If i % n == 0:
  • Add i to sum_div
  • Else:
  • Add i to sum_not_div
  1. Compute difference = sum_not_div - sum_div
  2. Print difference
  3. End

PROGRAM:

#include <stdio.h>
int check(int m, int n)
{
    int sum1 = 0, sum2 = 0;
    for (int i = 1; i <= m; i++)
    {
        if (i % n == 0)
            sum1 += i;
        else 
            sum2 += i;
    }
    return sum2 - sum1;
}
int main()
{
    int num1, num2, diff;
    scanf("%d %d", &num1, &num2);
    diff = check(num2, num1);
    printf("%d", diff);
    return 0;
}

OUTPUT:

Image

RESULT:

Thus, the program has been executed successfully.


3b : C program to check whether the given number is Armstrong number or not.

AIM:

To write a C program to check whether the given number is Armstrong number or not.

ALGORITHM:

  1. Start
  2. Read num
  3. Set temp = num, sum = 0
  4. While temp ≠ 0:
  • Extract digit: digit = temp % 10
  • Add cube of digit to sum
  • Remove digit: temp = temp / 10
  1. If sum == num:
  • Print "Armstrong number"
  • Else print "Not an Armstrong number"
  1. End

PROGRAM:

#include <stdio.h>
int main()
{
    int num1, temp, arm, a = 0;
    scanf("%d", &num1);
    temp = num1;
    while (temp != 0)
    {
        a = temp % 10;
        arm += a*a*a;
        temp /= 10;
    }
    if (arm == num1)
        printf("%d is a armstrong number", num1);
    else 
        printf("%d is not a armstrong number", num1);
}

OUTPUT:

Image

RESULT:

Thus, the program has been executed successfully.


3c : C program to read n number of elements and check whether the second element is odd or even.

AIM:

To write a C program to read n number of elements and check whether the second element is odd or even.

ALGORITHM:

  1. Start
  2. Read n
  3. Read n elements into array a
  4. If a[1] % 2 ≠ 0:
  • Print "Second element is odd"
  1. Else:
  • Print "Second element is even"
  1. End

PROGRAM:

#include <stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    int a[n];
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    if(a[1]%2!=0)
        printf("The second element %d is an odd number" ,a[1]);
    else
        printf("The second element %d is an even number", a[1]);
}

OUTPUT:

Image

RESULT:

Thus, the program has been executed successfully.


3d : C Program to Print the count of odd Numbers in an Array

AIM:

To write a C Program to Print the count of odd Numbers in an Array

ALGORITHM:

  1. Start
  2. Read n
  3. Initialize odd_count = 0, even_count = 0
  4. Repeat n times:
  • Read a number
  • If number is divisible by 2:
  • Increment even_count
  • Else:
  • Increment odd_count
  1. Print odd_count
  2. End

PROGRAM:

#include<stdio.h>
int main()
{
   int a[100], n, i, ecount=0, ocount=0;
   scanf("%d",&n);
   for(i=0; i<n; i++)
   {
        scanf("%d",&a[i]);
        if(a[i]%2==0) 
            ecount ++;
        else 
            ocount++;
   }
   printf("Odd numbers: %d",ocount);
   return 0;
}

OUTPUT:

Image

RESULT:

Thus, the program has been executed successfully.


3e : C program to generate the Fibonacci series for the given input using do-while loop.

AIM:

To write a C program to generate the Fibonacci series for the given input using do-while loop.

ALGORITHM:

  1. Start
  2. Read n
  3. Initialize f1 = -1, f2 = 1
  4. Repeat n times:
  • Compute f3 = f1 + f2
  • Print f3
  • Update f1 = f2, f2 = f3
  1. End

PROGRAM:

#include <stdio.h>
int main()
{
    int num1, f1 = -1, f2 = 1, f3 = 0;
    scanf("%d", &num1);
    for (int i = 1 ; i <= num1; i++)
    {
        f3 = f1 + f2;
        printf("%d ", f3);
        f1 = f2;
        f2 = f3;
    }
    return 0;
}

OUTPUT:

Image

RESULT:

Thus, the program has been executed successfully.


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published