CSE assignment test file
CSE assignment test file
A number is called an Armstrong number if the sum of the cubes of the digits
of the number is equal to the number. For example, 153 = 1^3 + 5^3 + 3^3.
Write a C program that asks the user to enter a number and returns if it is
Armstrong or not (use function).
Ans:
#include<stdio.h>
int main(){
}
if(sum == original){
printf("%d is an Armstrong number.", original);
}
else{
printf("%d is not an Armstrong number.", original);
}
return 0;
}
2. Write a program in C that takes as input a set of numbers and calculates the
mean, variance and standard deviation. (variance is defined as
Σ [(xi - x̅)^2]/n - 1 , where xi = i th number in the set, x̅ is the mean and
n=cardinality of the set; standard deviation is the square root of variance).
Ans:
#include<stdio.h>
int main(){
int max, min, n1, n2;
printf("Enter two numbers: \n");
scanf("%d%d", &n1, &n2);
max = (n1>n2) ? n1:n2;
min = (n1<n2) ? n1:n2;
while(1){
if (n1 % min == 0 && n2 % min == 0){
printf("HCF of %d and %d is %d. \n", n1, n2, min);
break;
}
--min;
}
while(1){
if (max % n1 == 0 && max % n2 == 0){
printf("LCM of %d and %d is %d.", n1, n2, max);
break;
}
++max;
}
return 0;
}
3. Write a C program that calculates the HCF and LCM of two numbers.
Ans:
#include<stdio.h>
int main(){
while(1){
if (n1 % min == 0 && n2 % min == 0){
printf("HCF of %d and %d is %d. \n", n1, n2, min);
break;
}
--min;
}
while(1){
if (max % n1 == 0 && max % n2 == 0){
printf("LCM of %d and %d is %d.", n1, n2, max);
break;
}
++max;
}
return 0;
}
4. Write a C program to display and find the sum of the series 1+11+111+....111
upto n. For eg. If n=4, the series is : 1+11+111+1111. Take the value of 'n' as
input from the user.
Ans:
#include<stdio.h>
int main(){
int i, j = 1, x = 0, sum = 0, n;
printf("Enter the number of elements of the series: ");
scanf("%d", &n);
for (i = 1; i <= n; i++){
x = x + j;
j = j * 10;
sum = sum + x;
if(i == n)
break;
printf("%d+", x);
}
printf("+%d\nSUM: %d1",x, sum);
return 0;
}
5. Write a C program that reads a positive integer n and then prints the
following pattern
*********
_********
__*******
___******
____*****
_____****
______***
_______**
________*
where n is the number of lines.
Ans:
#include<stdio.h>
int main(){
int i, j, k, n;
printf("Enter the number of rows: ");
scanf("%d", &n);
n = n-1;
for (i = 0; i <= n; i++){
for (k = 0; k < i; k++)
printf("_");
for (j = n; j >= i; j--)
printf("*");
printf("\n");
}
return 0;
}
6. Amicable numbers are found in pairs. A given pair of numbers is Amicable
if the sum of the proper divisors (not including itself) of one number is equal to
the other number and vice – versa. For example 220 & 284 are amicable
numbers. First we find the proper divisors of 220:
220:1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110
1+ 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = 284
Now, 284: 1, 2, 4, 71, 142
1 + 2 + 4 + 71 + 142 = 220
Write a C program to check that the input pair of numbers is amicable.
Ans:
#include<stdio.h>
int main(){
int a, b, i, j, sum1 = 0, sum2 = 0;
printf("Enter two integers: \n");
scanf("%d%d",&a,&b);
for(i=1; i<a; i++){
if(a % i == 0){
sum1 = sum1 + i;
}
}
for(j=1; j<b; j++){
if(b % j == 0){
sum2 = sum2 + j;
}
}
if(sum1 == b && sum2 == a){
printf("The numbers are amicable.");
}
else{
printf("The numbers are not amicable.");
}
return 0;
}
#include <stdio.h>
long long convert(int);
int main() {
int n, bin;
printf("Enter a decimal number: ");
scanf("%d", &n);
bin = convert(n);
printf("%d in decimal = %lld in binary", n, bin);
return 0;
}
long long convert(int n) {
long long bin = 0;
int rem, i = 1;
while (n!=0) {
rem = n % 2;
n /= 2;
bin += rem * i;
i *= 10;
}
return bin;
}
9. Write a C program to find the reverse of an integer number.
Ans:
#include<stdio.h>
int main(){
int x, rem, rev = 0;
printf("Enter an integer: ");
scanf("%d", &x);
while (x != 0){
rem = x % 10;
rev = rem + rev * 10;
x /= 10;
}
printf("Reverse number is %d", rev);
return 0;
}
10. Write a C program to sort an array of integers using bubble sort.
Ans:
#include<stdio.h>
int main(){
int i, j, x, swap, arr[] = {2,8,5,7,4,11,9,1,23};
x = 9;
printf("Before sorting: \n");
for (i=0; i<x; i++){
printf("%d ", arr[i]);
}
for (i = 0; i < x-1; i++){
swap = 0;
for (j = 0; j < x-1-i; j++){
if (arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swap = 1;
}
}
if(swap == 0){
break;
}
}
printf("\nAfter loop: \n");
for (i=0;i<x;i++){
printf("%d ", arr[i]);
}
return 0;
}
11. Write a C program to input n numbers in an array, calculate the sum of all
even numbers and all odd numbers in the array and print the larger sum.
Example: If the array contains the following elements: 2, 3, 3, 5, 4, 8, 7, 11, 2
The sum of all even elements is 2+4+8+2=16
Sum of all odd elements is 3+3+5+7+11=29
Therefore, the output should be 29.
Ans:
#include<stdio.h>
int main(){
int n, i, sum1 = 0, sum2 = 0, max;
printf("Enter the size of Array: ");
scanf("%d", &n);
int arr[n];
printf("\nEnter integer numbers: \n");
for (i = 0; i < n; i++){
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++){
if (arr[i] % 2 == 0){
sum1 += arr[i];
}
}
for (i = 0; i < n; i++){
if (arr[i] % 2 != 0){
sum2 += arr[i];
}
}
max = (sum1 > sum2) ? sum1 : sum2;
printf("Larger sum= %d", max);
return 0;
}
12. Take the price and quantity of items as an input. Write a C function to
calculate the sum of the prices. Write another C function to calculate the
discount according to the following rules:
For total less than Rs.1000, discount is 5%.
For total greater than Rs.1000 but less than Rs.5000, discount is 10%.
For total greater than Rs.5000, discount is 15%.
Write another function to print the individual item prices, total, discount and
the final price. Example:
If the prices are as follows:
Item 1: 200
Item 2: 400
Item 3: 200
Item 4: 10
Item 5: 50
And the quantities are:
Item 1: 1
Item 2: 1
Item 3: 3
Item 4: 5
Item 5: 2
Then you should print:
Item Price Quantity Subtotal
Item 1 200 1 200
Item 2 400 1 400
Item 3 200 3 600
Item 4 10 5 50
Item 5 50 2 100
-------------------------------------------------
TOTAL 1350
Discount 10% -135
-------------------------------------------------
GRAND TOTAL 1215
Ans:
#include<stdio.h>
int main(){
int price[5], quantity[5],subtotal[5];
printf("Input price and quantity: \n");
int i;
for (i = 0; i < 5; i ++){
printf("Item %d Price: ", i+1);
scanf("%d", &price[i]);
printf("Item %d Quantity: ", i+1);
scanf("%d", &quantity[i]);
}
for(i = 0; i < 5; i++){
subtotal[i] = price[i]*quantity[i];
}
printf("\nItem \t\tPrice \t\t Quantity \t\t Subtotal \n");
int j;
i = 0;
for (j = 1; j <= 5; j++){
printf("Item %d\t\t%d \t\t %d \t\t\t%d \n", j, price[i],quantity[i],subtotal[i]);
i++;
}
int total = 0;
for(i=0; i<5; i++){
total += subtotal[i];
}
printf("\n--------------------------------------------------------------------------------\n");
printf("Total \t\t\t\t\t\t\t %d\n",total);
int discount,x;
if (total < 1000){
x = 5;
discount = total*0.5;
}
else if (total >= 1000 && total < 5000){
x = 10;
discount = total*0.1;
}
else if (total >= 5000){
x = 15;
discount = total*0.15;
}
printf("Discount \t %d%%\t\t\t\t\t -%d\n",x,discount);
printf("\n--------------------------------------------------------------------------------\n");
printf("Grand Total \t\t\t\t\t\t %d\n",total-discount);
return 0;
}
13. Write a C program to calculate the volume of the following shapes: Cube,
Cuboid, Sphere, Cylinder and Cone. Ask the user which one s/he wants to
calculate, and take the appropriate required inputs. Then print the result. The
input should be taken in the main function and calculations for every solid
should be done in a separate function by passing appropriate arguments.
Example:
If the user chooses the option for cube, only one input is required i.e., the side.
The volume is then calculated and printed. If the user chooses the option for
cuboid, only three inputs are required i.e., length, breadth and height. The
volume is then calculated and printed.
Ans:
#include<stdio.h>
float vol_cube(float r)
{
return (r*r*r);
}
float vol_cuboid(float l, float w, float h)
{
return (l*w*h);
}
float vol_sphere(float r)
{
return (4*22*r*r*r)/(7*3);
}
float vol_cylinder(float r,float h)
{
return (22*r*r*h)/7;
}
float vol_cone(float r, float h)
{
return (22*r*r*h)/(7*3);
}
int main()
{
float v,r,l,w,h;
printf("Of which objects you want to calculate? \n");
int name;
printf("Press 1 for CUBE, 2 for CUBOID, 3 for SPHERE, 4 for cylinder, 5 for CONE.\n");
scanf("%d", &name);
if (name == 1){
printf("Enter length of the cube: ");
scanf("%f",&r);
v=vol_cube(r);
printf("\nVolume of the cube is: %.2f\n",v);
}
else if (name == 2){
printf("Enter length, width and height of the cuboid: \n");
scanf("%f%f%f",&l,&w,&h);
v=vol_cuboid(l,w,h);
printf("Volume of the cuboid is: %.2f\n",v);
}
else if (name == 3){
printf("Enter radius of the sphere: ");
scanf("%f",&r);
v=vol_sphere(r);
printf("Volume of the sphere is: %.2f\n",v);
}
else if (name == 4){
printf("Enter radius and height of the cylinder: \n");
scanf("%f%f",&r,&h);
v=vol_cylinder(r,h);
printf("Volume of the cylinder is: %.2f\n",v);
}
else if (name == 5){
printf("Enter radius and height of the cone: \n");
scanf("%f%f",&r,&h);
v=vol_cone(r,h);
printf("Volume of the cone is: %.2f\n",v);
}
else{
printf("\nPlease enter between 1 to 5\n.");
}
return 0;
}
14.Write a C program to check if a number has three consecutive 5s. If yes,
print YES, else print NO. Example:
Number: 1353554
Result: NO
Number: 345559
Result: YES
Ans:
#include <stdio.h>
int main(){
int num, temp, count;
printf("Enter the number: ");
scanf("%d", &num);
temp = num;
while (temp > 0){
if (temp % 10 == 5)
count++;
else
count = 0;
if(count == 3)
break;
temp /= 10;
}
if (count == 3)
printf("YES");
else
printf("NO");
return 0;
}
Ans:
(a)
#include<stdio.h>
int main(){
int i,j;
for (i=1;i<=5;i++){
for (j=1;j<=i;j++){
printf("%d ",j);
}
printf("\n");
}
return 0;
}
(b)
#include<stdio.h>
int main(){
int i,j;
for (i=1;i<=5;i++){
for (j=1;j<=i;j++){
printf("%d ",i);
}
printf("\n");
}
return 0;
}
16. Write a C program to accept 10 values in an integer array. Display the
number of odd, even and negative numbers.
Ans:
#include<stdio.h>
int main(){
int i,odd = 0, even = 0, neg = 0;
int arr[10];
printf("Enter 10 integers: \n");
for (i=0;i<10;i++){
scanf("%d",&arr[i]);
}
for (i=0;i<10;i++){
if(arr[i] % 2 == 0){
even += 1;
}
}
for (i=0;i<10;i++){
if (arr[i] % 2 != 0){
odd += 1;
}
}
for (i=0;i<10;i++){
if (arr[i] < 0){
neg += 1;
}
}
printf("\n");
printf("Number of Odd integers: %d\n", odd);
printf("Number of Even integers: %d\n", even);
printf("Number of Negative integers: %d\n", neg);
return 0;
}
17. Write a C program to accept the basic salary of an employee from the user.
Calculate the gross salary on the following basis:
Basic HRA DA
1 - 4000 10% 50%
4001 - 8000 20% 60%
8001 - 12000 25% 70%
12000 and above 30% 80%
Ans:
23. Write a C program to take a list of n elements from the user. Store it in an
array. Reverse the list.
Ans:
#include<stdio.h>
int main(){
int arr[5];
int i;
printf("Enter integers: \n");
for (i=0;i<5;i++){
scanf("%d",&arr[i]);
}
printf("List before reverse: \n");
for (i=0;i<=4;i++){
printf("%d ", arr[i]);
}
printf("\nList after reverse: \n");
for (i=4;i>=0;i--){
printf("%d ", arr[i]);
}
return 0;
}
24. Write a C program to check whether a given string is palindrome or not.
Ans:
#include <stdio.h>
int main(){
char str1[20];
int i, length;
int flag = 0;
printf("Enter a string:");
scanf("%s", str1);
length = strlen(str1);
if (flag) {
printf("%s is not a palindrome", str1);
}
else {
printf("%s is a palindrome", str1);
}
return 0;
}