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.
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.
- Start
- Read n and m
- Set sum_div = 0, sum_not_div = 0
- For each number i from 1 to m:
- If i % n == 0:
- Add i to sum_div
- Else:
- Add i to sum_not_div
- Compute difference = sum_not_div - sum_div
- Print difference
- End
#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;
}
Thus, the program has been executed successfully.
To write a C program to check whether the given number is Armstrong number or not.
- Start
- Read num
- Set temp = num, sum = 0
- While temp ≠ 0:
- Extract digit: digit = temp % 10
- Add cube of digit to sum
- Remove digit: temp = temp / 10
- If sum == num:
- Print "Armstrong number"
- Else print "Not an Armstrong number"
- End
#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);
}
Thus, the program has been executed successfully.
To write a C program to read n number of elements and check whether the second element is odd or even.
- Start
- Read n
- Read n elements into array a
- If a[1] % 2 ≠ 0:
- Print "Second element is odd"
- Else:
- Print "Second element is even"
- End
#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]);
}
Thus, the program has been executed successfully.
To write a C Program to Print the count of odd Numbers in an Array
- Start
- Read n
- Initialize odd_count = 0, even_count = 0
- Repeat n times:
- Read a number
- If number is divisible by 2:
- Increment even_count
- Else:
- Increment odd_count
- Print odd_count
- End
#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;
}
Thus, the program has been executed successfully.
To write a C program to generate the Fibonacci series for the given input using do-while loop.
- Start
- Read n
- Initialize f1 = -1, f2 = 1
- Repeat n times:
- Compute f3 = f1 + f2
- Print f3
- Update f1 = f2, f2 = f3
- End
#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;
}
Thus, the program has been executed successfully.