Write a C program for a function that accepts two numbers to calculate the sum and return the total number of carries.
-
Start the program.
-
Declare three integer variables: num1, num2, sum, and carry. Initialize sum and carry to 0.
-
Read two integer values (num1 and num2) from the user using scanf.
-
Use a for loop that continues as long as num1 > 0 or num2 > 0:
a. Add the last digits of num1 and num2 and store in sum.
b. If sum is greater than or equal to 10, increment carry by 1.
c. Remove the last digit from num1 and num2 by dividing each by 10.
-
After the loop ends, print the value of carry.
-
End the program.
#include<stdio.h>
int main()
{
int num1,num2,sum=0,carry=0;
scanf("%d%d",&num1,&num2);
for(int i=0;num1>0||num2>0;i++){
sum=(num1%10)+(num2%10);
if(sum>=10)
{
carry+=1;
}
num1/=10;
num2/=10;
}
printf("%d",carry);
}
Thus the program for a function that accepts two numbers to calculate the sum and return the total number of carries has been executed successfully
Create a C program to check whether the given number is Armstrong number or not.
-
Start the program.
-
Declare integer variables: num, var, var1, and sum. Initialize sum to 0.
-
Read an integer value num from the user using scanf.
-
Assign the value of num to var.
-
Use a while loop that runs as long as var is not equal to 0:
a. Extract the last digit of var using var % 10 and store it in var1.
b. Add the cube of var1 to sum using pow(var1, 3).
c. Remove the last digit from var by dividing it by 10.
-
After the loop, check if sum is equal to num:
a. If true, print that num is an Armstrong number.
b. If false, print that num is not an Armstrong number.
-
End the program.
#include<stdio.h>
#include<math.h>
int main()
{
int num,var,var1,sum=0;
scanf("%d",&num);
var=num;
while(var !=0)
{
var1=var%10;
sum+=pow(var1,3);
var/=10;
}
if(num==sum)
printf("%d is armstrong number",num);
else
printf("%d is not a armstrong number",num);
}
Thus the program to check whether the given number is Armstrong number or not has been executed successfully
Write a program in C to read n number of values in an array and display it in reverse order.
- Start the program.
- Declare an integer variable n to store the size of the array.
- Read the value of n from the user using scanf.
- Declare an integer array of size n.
- Use a for loop from 0 to n-1 to read n elements into the array using scanf.
- Use another for loop from n-1 down to 0 to print the elements of the array in reverse order.
- End the program.
#include <stdio.h>
int main()
{
int n, i;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=n-1;i>=0;i--)
printf("%d ",a[i]);
return 0;
}
Thus the program to read n number of values in an array and display it in reverse order has been executed successfully
Write a C program to delete first element in an array
- Start the program.
- Declare an integer array of size 11.
- Use a for loop from 0 to 10 to read 11 elements into the array using scanf.
- Use another for loop from index 2 to 10 to print the elements of the array starting from the 3rd element.
- End the program.
#include<stdio.h>
int main()
{
int a[11];
for(int i=0;i<11;i++)
{
scanf("%d",&a[i]);
}
for(int j=2;j<11;j++)
{
printf("%d ",a[j]);
}
}
Thus the program to delete first element in an array has been executed successfully
Write a C Program to Print the sum, count of even & odd Numbers in an Array
-
Start the program.
-
Declare integer variables: num, sum, odd, and even. Initialize sum, odd, and even to 0.
-
Read an integer value num from the user using scanf.
-
Declare an integer array of size num.
-
Use a for loop from 0 to num-1 to read num elements into the array using scanf.
-
Use a for loop from 0 to num-1 to calculate the sum of all array elements and store it in sum.
-
Use another for loop from 0 to num-1 to count:
a. Even numbers and increment the even counter.
b. Odd numbers and increment the odd counter.
-
Print the total sum, the count of even numbers, and the count of odd numbers.
-
End the program.
#include<stdio.h>
int main()
{
int num,sum=0,odd=0,even=0;
scanf("%d",&num);
int a[num];
for(int i=0;i<num;i++)
{
scanf("%d",&a[i]);
}
for(int j=0;j<num;j++)
{
sum+=a[j];
}
for(int i=0;i<num;i++)
{
if(a[i]%2==0)
even+=1;
else
odd+=1;
}
printf("Total Sum: %d\n",sum);
printf("Even numbers: %d\n",even);
printf("Odd numbers: %d\n",odd);
}
Thus the program to Print the sum, count of even & odd Numbers in an Array has been executed successfully