Q 1) A
i. Write a ‘C’program to accept an integer and check if it is divisible by 3 and 5.
#include<stdio.h>
int main()
{
int num;
printf("\n Enter number : ");
scanf("%d",&num);
if(num%3==0 && num%5==0)
printf("\n %d is divisible by both 3 and 5",num);
else if(num%3==0)
printf("\n %d is divisible by only 3",num);
else if(num%5==0)
printf("\n %d is divisible by only 5",num);
else
printf("\n %d is not divisible by 3 and 5",num);
return 0;
}
Program Output
Enter number : 3030 is divisible by both 3 and 5
ii Write a function in ‘C’ to calculate sum of digits of an integer. Use this function in main.
#include<stdio.h>
int main()
{
int num;
printf("\n Enter number : ");
scanf("%d",&num);
//calling function
sum_digits(num);
return 0;
}
void sum_digits(int n)
{
int r,sum=0;
int num=n;
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("\n Sum of digits in %d is %d",num,sum);
}
Program Output:
Enter number : 123Sum of digits in 123 is 6
OR
Q 1) Write a ‘C’ program to accept n integers in an array and search for a specific number.
#include<stdio.h>
int main()
{
int n,i,key,arr[30],flag=0;
printf("\n How many integers? ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter element %d:\t",i+1);
scanf("%d",&arr[i]);
}
printf("\n Enter element to search : ");
scanf("%d",&key);
// Searching for key
for(i=0;i<n;i++)
{
if(arr[i]==key)
{
flag=1;
break;
}
}
if(flag==1)
printf("\n %d is found in array",key);
else
printf("\n %d is Not found in array",key);
return 0;
}
Program Output:
How many integers? 5
Enter element 1: 11
Enter element 2: 22
Enter element 3: 33
Enter element 4: 44
Enter element 5: 55
Enter element to search : 44
44 is found in array
0 Comments:
Post a Comment