0% found this document useful (0 votes)
9 views95 pages

C Program 60 (Aditya Ingale)

The document contains a series of C programming exercises, each with a specific task such as calculating the surface area and volume of a cylinder, accepting employee details, and performing string operations. Each exercise includes code snippets, input prompts, and expected output examples. The exercises cover various programming concepts including structures, functions, and arithmetic operations.

Uploaded by

mayurmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views95 pages

C Program 60 (Aditya Ingale)

The document contains a series of C programming exercises, each with a specific task such as calculating the surface area and volume of a cylinder, accepting employee details, and performing string operations. Each exercise includes code snippets, input prompts, and expected output examples. The exercises cover various programming concepts including structures, functions, and arithmetic operations.

Uploaded by

mayurmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 95

Aditya……………...

C Program 60
(1 to 60)

Q1. Write a C program to accept dimensions of a cylinder


and display the surface area and volume of cylinder.
Input-
#include <stdio.h>
int main()
{
float radius, height;
float s,v;
printf("Enter value of radius of cylinder:");
scanf("%f",&radius);
printf("Enter value of height of cylinder:");
scanf("%f",&height);
s=2*(22/7)*radius*(radius+height);
v=(22/7)*radius*radius*height;
printf("Surface area of cylinder is:%.2f\n",s);
printf("Volume of cylinder is:%.2f",v);
return 0;
}

Output-
Enter value of radius of cylinder:10
Enter value of height of cylinder:20
Surface area of cylinder is:1800.00
Volume of cylinder is:6000.00
………………………………………………………………………………………………………………………….

Q2. Create a structure employee (id, name, salary). Accept


details of n employees and write a menu driven program to
perform the following operations.
a)Search employee by id
b)Display all employees
Input-
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char name[30];
int id;
int salary;
} Employee;
int main()
{
int i, n=2;
Employee employees[n];
printf("Enter %d Employee Details \n \n",n);
for(i=0; i<n; i++){
printf("Employee %d:- \n",i+1);
printf("Name: ");
scanf("%s",employees[i].name);
printf("Id: ");
scanf("%d",&employees[i].id);
printf("Salary: ");
scanf("%d",&employees[i].salary);
printf("\n");
}
printf("All Employees Details\n");
for(i=0; i<n; i++){
printf("Name \t: ");
printf("%s \n",employees[i].name);
printf("Id \t: ");
printf("%d \n",employees[i].id);
printf("Salary \t: ");
printf("%d \n",employees[i].salary);
printf("\n");
}
return 0;

Output-
Enter 2 Employee Details

Employee 1:-
Name: Aditya
Id: 001
Salary: 50000
Employee 2:-
Name: Shivam
Id: 002
Salary: 60000

All Employees Details


Name : Aditya
Id :1
Salary : 50000

Name : Shivam
Id :2
Salary : 60000
………………………………………………………………………………………………………………………….

Q3. Write a C program to accept radius of a circle and


display the area and circumference of a circle.
Input-
#include<stdio.h>
int main()
{
int radius;
float area,circumference;
printf("Enter radius of circle:");
scanf("%d",&radius);
area = 3.14*radius*radius;
printf("Area of circle:%f\n",area);
circumference=2*3.14*radius;
printf("Circumference of circle: %f",circumference);
return 0;
}

Output-
Enter radius of circle:2
Area of circle:12.560000
Circumference of circle: 12.560000
………………………………………………………………………………………………………………………….

Q4. Write a program to calculate sum of following series up


to n terms. Sum=X+X2/2!+X3/3!+……
(Note: Write separate user defined function to calculate
power and factorial)
Input-
#include <stdio.h>
void main()
{
float x;
float sum=1;
float number_of_rows=1;
int i,n;
printf("Enter the value of x:");
scanf("%f",&x);
printf("Enter number of terms:");
scanf("%d",&n);
for (i=1;i<n;i++)
{
number_of_rows =number_of_rows*x/(float)i;
sum =sum+number_of_rows;
}
printf("The sum is:%f\n",sum);
}

Output-
Enter the value of x:2
Enter number of terms:3
The sum is:5.000000
………………………………………………………………………………………………………………………….

Q5. Write a C program to accept temperatures in Fahrenheit


(F) and display it in Celsius(C) and Kelvin (K) (Hint:
C=5.0/9(F-32), K = C + 273.15)
Input-
#include<stdio.h>
void main()
{
float fahrenheit,convert;
printf("Enter temperature in fahrenheit:");
scanf("%f",&fahrenheit);
convert=(fahrenheit-32)*5/9;
printf("Temperature in celsius:%f",convert);
return 0;
}

Output-
Enter temperature in fahrenheit:45
Temperature in celsius:7.222222
………………………………………………………………………………………………………………………….

Q6. Write a menu driven program to perform the following


operations on strings using standard library functions:
1. Length of String
2. Copy String
3. Connect Two Strings
4. Compare two 4 strings
Input-
#include<conio.h>
#include<string.h>
#include<stdio.h>
void lengthofstring()
{
int length;
char string[20];
printf("\nEnter String: ");
scanf("%s",&string);
length=strlen(string);
printf("\nLength of string is: %d",length);
}
void copystring()
{
char string2[20];
char string[20];
printf("\nEnter String: ");
scanf("%s",&string);
strcpy(string2,string);
printf("\nCopied string is: %s",string2);
}
void add()
{
char string2[20];
char string[20];
char string3[20];
printf("\nEnter 1st String: ");
scanf("%s",&string);
printf("\nEnter 2nd String: ");
scanf("%s",&string2);
printf("\nAddition of 2 string: %s",strcat(string,string2));
}
void compare()
{
char string2[20];
char string[20];
printf("\nEnter 1st String: ");
scanf("%s",&string);
printf("\nEnter 2nd String: ");
scanf("%s",&string2);
if(strcmp(string,string2)==0)
{
printf("\nBoth are equal");
}
else
{
printf("\nBoth are different");
}
}
void main()
{
int c;
do
{
printf("\n\n1. Length of string\n2. Copy String \n3. Connect Two Strings
\n4. Compare two strings\n5. Exit\nEnter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:lengthofstring();break;
case 2:copystring();break;
case 3:add();break;
case 4:compare();break;
}
}while(c<5);
}

Output-
1. Length of string
2. Copy String
3. Connect Two Strings
4. Compare two strings
5. Exit
Enter your choice:1

Enter String: Aditya

Length of string is: 6

1. Length of string
2. Copy String
3. Connect Two Strings
4. Compare two strings
5. Exit
Enter your choice:2

Enter String: Aditya

Copied string is: Aditya

1. Length of string
2. Copy String
3. Connect Two Strings
4. Compare two strings
5. Exit
Enter your choice:3

Enter 1st String: Aditya

Enter 2nd String: Ingale

Addition of 2 string: AdityaIngale

1. Length of string
2. Copy String
3. Connect Two Strings
4. Compare two strings
5. Exit
Enter your choice:4

Enter 1st String: Aditya

Enter 2nd String: Ingale

Both are different

1. Length of string
2. Copy String
3. Connect Two Strings
4. Compare two strings
5. Exit
Enter your choice:5
………………………………………………………………………………………………………………………….

Q7. Write a C program to accept two numbers and print


arithmetic and harmonic mean of the two numbers
(Hint: AM= (a+b)/2 ,HM = ab/(a+b) )
Input-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
float AM, HM;
printf("Enter the 1st number:");
scanf("%d",&a);
printf("Enter the 2nd number:");
scanf("%d",&b);

AM=((a+b))/2;
HM=a*b/((a+b));
printf("Arithmetic Mean is:%f",AM);
printf("\nHarmonic Mean is:%f",HM);

getch();
}

Output-
Enter the 1st number:10
Enter the 2nd number:20
Arithmetic Mean is:15.000000
Harmonic Mean is:6.000000
………………………………………………………………………………………………………………………….

Q8. Create a structure Student (id, name, marks). Accept


details of n students and write amenu driven program to
perform the following operations.
a) Search student by id
b) Display all students
Input-
#include <stdio.h>
struct student
{
char name[50];
int ID;
float marks;
} s;
int main()
{
printf("Enter Information:\n");
printf("Enter name:");
fgets(s.name,sizeof(s.name),stdin);
printf("Enter ID:");
scanf("%d",&s.ID);
printf("Enter marks:");
scanf("%f",&s.marks);

printf("Showing Information:\n");
printf("ID:%d\n",s.ID);
printf("Name:");
printf("%s",s.name);
printf("Marks:%.2f\n",s.marks);

return 0;
}

Output-
Enter Information:
Enter name:Aditya
Enter ID:001
Enter marks:80
Showing Information:
ID:1
Name:Aditya
Marks:80.00
………………………………………………………………………………………………………………………….
Q9. Write a C program to accept dimensions length (l),
breadth(b) and height(h) of a cuboids and display surface
area and volume
(Hint : surface area=2(lb+lh+bh ), volume=lbh )
Input-
#include <stdio.h>
int main()
{
float length,breadth,height;
float surfacearea, volume;

printf("Enter length of the cuboid:");


scanf("%f",&length);
printf("Enter breadth of the cuboid:");
scanf("%f",&breadth);
printf("Enter height of the cuboid:");
scanf("%f",&height);
printf("______________________________________________");
surfacearea=2*(length*breadth+length*height+breadth*height);
volume=length*breadth*height;
printf("\nSurface area of cuboids is:%.2f\n",surfacearea);
printf("Volume of cuboids is:%.2f",volume);
return 0;
}

Output-
Enter length of the cuboid:10
Enter breadth of the cuboid:20
Enter height of the cuboid:30
__________________________________
Surface area of cuboids is:2200.00
Volume of cuboids is:6000.00
………………………………………………………………………………………………………………………….

Q10.Write a program which accepts a sentence from the


user and alters it as follows: Every space is replaced by *,
case of all alphabets is reversed, digits are replaced by ?
Input-
#include<stdio.h>
#include<ctype.h>
#include<string.h>
void Stral(char str[])
{
int i;
for(i=0;i<=strlen(str)-1;i++)
{
if(str[i]==' ')
str[i]='*';
if(islower(str[i]))
str[i]=toupper(str[i]);
else
str[i]=tolower(str[i]);
if(isdigit(str[i]))
str[i]='?';
}
printf("The Replacement is.....\n");
printf(" %s \n",str);
}
void main()
{
char str[100];
printf("Enter any sentence:");
fgets(str,100,stdin);
Stral(str);
}

Output-
Enter any sentence:Iam I an m a good boy54665444545
The Replacement is.....
i*AM*A*GOOD*BOY???????????
………………………………………………………………………………………………………………………….

Q11. . Write a C Program to accept a character from the


keyboard and display its previous and next character in
order. Ex. If character entered is ‘d’, display “The previous
character is c”, “The next character is e”.
Input-
#include<stdio.h>
int main()
{
char letter;
printf("Enter the character:");
scanf("%c",&letter);
printf("Previous character of %c is %c", letter,letter-1);
printf("\nNext character of %c is %c",letter,letter+1);
return 0;
}

Output-
Enter the character:g
Previous character of g is f
Next character of g is h
………………………………………………………………………………………………………………………….

Q12. Write a program to accept a string and then count the


occurrences of a specific character of a string.
Input-
#include<stdio.h>
int main()
{
char s[100],c;
int i,count=0;
printf("Enter the string:");
gets(s);
printf("Enter character to be searched:");
c=getchar();
for(i=0;s[i];i++)
{
if(s[i]==c)
{
count++;
}
}
printf("character '%c' occurs %d times ",c,count);
return 0;
}

Output-
Enter the string:adityaingale
Enter character to be searched:a
character 'a' occurs 3 times
……………………………………………………………………………………………………………………….

Q13. Write a C program to accept the x and y coordinates of


two points and compute the distance between the two
points.
Input-
#include <stdio.h>
int main()
{
float x1, y1, x2, y2,distance;
printf("Enter x1:");
scanf("%f", &x1);
printf("Enter y1:");
scanf("%f", &y1);
printf("Enter x2:");
scanf("%f", &x2);
printf("Enter y2:");
scanf("%f", &y2);
distance=((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1));
printf("Distance between the said points: %.3f",sqrt(distance));
printf("\n");
return 0;
}

Output-
Enter x1:10
Enter y1:10
Enter x2:20
Enter y2:30
Distance between the said points: 22.361
………………………………………………………………………………………………………………………….

Q14. Write a program to calculate Multiplication of two


matrices of order m*n.
Input-
#include<stdio.h>
int main(){
int m[10][10],n[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("Enter the number of row=");
scanf("%d",&r);
printf("Enter the number of column=");
scanf("%d",&c);
printf("Enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&m[i][j]);
}
}
printf("Enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&n[i][j]);
}
}

printf("Multiply of the matrix=\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=m[i][k]*n[k][j];
}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}

Output-
Enter the number of row=3
Enter the number of column=3
Enter the first matrix element=
111
333
222
Enter the second matrix element=
444
222
666
Multiply of the matrix=
12 12 12
36 36 36
24 24 24
………………………………………………………………………………………………………………………….

Q15. A cashier has currency notes of denomination 1, 5 and


10. Write a C program to accept the withdrawal amount
from the user and display the total number of currency
notes of each denomination the cashier will have to give.
Input-
#include<stdio.h>
int main()
{
int w,x,y,z;
printf("Enter withdrow amount:");
scanf("%d",&w);
x=w/10;
w=w%10;
y=w/5;
w=w%5;
z=w;
printf("Note of 10: %d\n",x);
printf("Note of 5: %d\n",y);
printf("Note of 1: %d\n",z);
}

Output-
Enter withdrow amount:156
Note of 10: 15
Note of 5: 1
Note of 1: 1
………………………………………………………………………………………………………………………….

Q16. Write a menu driven program to perform the following


operation on m*n Matrix
1. Calculate sum of upper triangular matrix elements
2. Calculate sum of diagonal elements
Input-
#include<stdio.h>
int main()
{
int m,n,rows,columns,a[10][10],Sum=0;
printf("Enter Number of rows:");
scanf("%d",&m);
printf("Enter Number of columns:");
scanf("%d",&n);
printf("\nPlease Enter the Matrix Elements\n");
for(rows=0;rows<m;rows++)
{
for(columns= 0;columns<n;columns++)
{
scanf("%d",&a[rows][columns]);
}
}
for(rows= 0;rows<m;rows++)
{
for(columns=0;columns<n;columns++)
{
if(columns>rows)
{
Sum=Sum+a[rows][columns];
}
}
}
printf("The Sum of Upper Triangle Matrix=%d",Sum);
return 0;
}

Output-
Enter Number of rows:4
Enter Number of columns:4

Please Enter the Matrix Elements


1111
5555
5468
8763
The Sum of Upper Triangle Matrix=21
………………………………………………………………………………………………………………………….

Q17. Write a C program to accept a character from the user


and check whether the character is a vowel or consonant.
Input-
#include <stdio.h>
int main()
{
char c;
int lvowel, uvowel;
printf("Enter an alphabet:");
scanf("%c",&c);
lvowel=(c=='a'||c=='e'||c=='i'||c=='o'||c=='u');
uvowel=(c=='A'||c=='E'||c=='I'||c=='O'||c=='U');
if (lvowel||uvowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}

Output-
Enter an alphabet:w
w is a consonant.
…………………………………………………………………………………………………………………………

Q18. Write a program to accept two numbers as range and


display multiplication table of all numbers within that
range.
Input-
#include<stdio.h>
int main()
{
int p,a;
printf("Enter number:");
scanf("%d",&p);
for( a=1;a<=10;++a)
{
printf("%d*%d=%d\n",p,a,p*a);
}

return 0;
}

Output-
Enter number:7
7*1=7
7*2=14
7*3=21
7*4=28
7*5=35
7*6=42
7*7=49
7*8=56
7*9=63
7*10=70
…………………………………………………………………………………………………………………………

Q19. Write a C program to accept the x and y coordinate of


a point and find the 4 quadrant in which the point lies.
Input-
#include <stdio.h>
void main()
{
int a,b;

printf("Enput the values for X coordinate:");


scanf("%d",&a);
printf("Enter the value of y coordinate:");
scanf("%d",&b);
if(a>0 && b>0)
printf("The coordinate point (%d,%d) lies in the First
quandrant.\n",a,b);
else if(a<0 && b>0)
printf("The coordinate point (%d,%d) lies in the Second
quandrant.\n",a,b);
else if(a<0 && b<0)
printf("The coordinate point (%d, %d) lies in the Third
quandrant.\n",a,b);
else if(a>0 && b<0)
printf("The coordinate point (%d,%d) lies in the Fourth
quandrant.\n",a,b);
else if(a==0 && b==0)
printf("The coordinate point (%d,%d) lies at the origin.\n",a,b);
return 0;
}

Output-
Enput the values for X coordinate:45
Enter the value of y coordinate:-45
The coordinate point (45,-45) lies in the Fourth quandrant.
………………………………………………………………………………………………………………………….
Q20. Write a program, which accepts a number n and
displays each digit in words. Example: 6702 Output = Six-
Seven-Zero-Two
Input-
#include<stdio.h>
int main()
{
int digit,num,n,l,r=0;
printf("Enter positive integer number:");
scanf("%d", &n);
while(n>0)
{
l=n%10;
r=r*10+l;
n=n/10;
}
num=r;
printf("\nYou entered:");
while (num>0)
{
digit=num%10;
switch(digit)
{
case 0:
printf("Zero-");
break;
case 1:
printf("One-");
break;
case 2:
printf("Two-");
break;
case 3:
printf("Three-");
break;
case 4:
printf("Four-");
break;
case 5:
printf("Five-");
break;
case 6:
printf("Six-");
break;
case 7:
printf("Seven-");
break;
case 8:
printf("Eight-");
break;
case 9:
printf("Nine-");
break;
}

num=num/10;
}
return 0;
}

Output-
Enter positive integer number:5412

You entered:Five-Four-One-Two-
………………………………………………………………………………………………………………………….

Q21. Write a C program to accept the cost price and selling


price from the user. Find out if the seller has made a profit
or loss and display how much profit or loss has been made.
Input-
#include<stdio.h>
int main()
{
float cost,price,a;
printf("Enter cost of Pen:");
scanf("%f",&cost);
printf("Enter selling price of Pen:");
scanf("%f",&price);
a=cost-price;
if(a>0)
{
printf("profit=%f",a);
}
else if(a<0)
{
a=(-1)*a;
printf("loss=%f",a);
}
else
{
printf("NO profit NO loss");
}
return 0;
}

Output-
Enter cost of Pen:50
Enter selling price of Pen:45
profit=5.000000
………………………………………………………………………………………………………………………….

Q22. Accept radius from the user and write a program


having menu with the following options and corresponding
actions

Option Actions
1. Area of Circle Compute area
of circle and print
2. Circumference of Circle Compute Circumference of circle and print
3. Volume of Sphere Compute Volume of Sphere and print
Input-
#include<stdio.h>
int area()
{
float r,area;
printf("\nEnter radius of circle:");
scanf("%f",&r);
area=(3.142*r*r);
printf("\nArea of Circle:%f",area);
}
int circumference()
{
float r,circumference;
printf("\nEnter radius of circle:");
scanf("%f",&r);
circumference=2*3.14*r;
printf("\nCircumference of Circle:%f",circumference);
}
int volume()
{
float r,volume_of_sphere;
printf("\nEnter radius of sphere:");
scanf("%f",&r);
volume_of_sphere=4/3*3.142*r*r;
printf("\nVolume of Sphere:%f",volume_of_sphere);
}
int main()
{
int a;
do
{
printf("\n(1) Area of Circle\n(2) Circumference of Circle\n(3) Volume
of Sphere\n(4) Exit.\n\t\tEnter choice:");
scanf("%d",&a);
switch(a)
{
case 1:area();break;
case 2:circumference();break;
case 3:volume();break;
}
}
while(a!=4);
}

Output-
(1) Area of Circle
(2) Circumference of Circle
(3) Volume of Sphere
(4) Exit.
Enter choice:1

Enter radius of circle:2

Area of Circle:12.568000
(1) Area of Circle
(2) Circumference of Circle
(3) Volume of Sphere
(4) Exit.
Enter choice:2

Enter radius of circle:3

Circumference of Circle:18.840000
(1) Area of Circle
(2) Circumference of Circle
(3) Volume of Sphere
(4) Exit.
Enter choice:3

Enter radius of sphere:4

Volume of Sphere:50.271999
(1) Area of Circle
(2) Circumference of Circle
(3) Volume of Sphere
(4) Exit.
Enter choice:4

………………………………………………………………………………………………………………………….

Q23. Write a C program to calculate sum of digits of a given


input number.
Input-
#include<stdio.h>
int main()
{
int n,sum=0,m;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("Sum is=%d",sum);
return 0;
}

Output-
Enter a number:8795546
Sum is=44
………………………………………………………………………………………………………………………….
Q24. Accept two numbers from user and write a menu
driven program to perform the following operations
1. swap the values of two variables
2. calculate arithmetic mean and harmonic mean of two
numbers
Input-
#include <stdio.h>

int main()
{
int x, y;
float AM,HM;
printf("Enter Value of x:");
scanf("%d", &x);
printf("Enter Value of y:");
scanf("%d", &y);
int temp=x;
x=y;
y=temp;
printf("\nAfter Swapping:x=%d,y=%d\n",x,y);
AM=((x+y))/2;
HM=x*y/((x+y));
printf("Arithmetic Mean is:%f",AM);
printf("\nHarmonic Mean is:%f",HM);
return 0;
}
Output-
Enter Value of x:5
Enter Value of y:8

After Swapping:x=8,y=5
Arithmetic Mean is:6.000000
Harmonic Mean is:3.000000
………………………………………………………………………………………………………………………….

Q25. Write a C program to accept the value of n and display


sum of all odd numbers up to n.
Input-
#include <stdio.h>
int main()
{
int i,num,odd=0;
printf("Enter the value of number:");
scanf("%d",&num);
for (i=1;i<=num;i++)
{
odd=odd+i;
}
printf("Sum of all odd number is:%d",odd);
return 0;
}

Output-
Enter the value of number:20
Sum of all odd number is:210
………………………………………………………………………………………………………………………….

Q26. Write a program to accept a decimal number and


convert it to binary, octal and hexadecimal number.
Input-
#include<stdio.h>
void convert_to_x_base(int, int);

int main(void)
{
int num, choice, base;
while(1)
{
printf("1.Decimal to binary.\n");
printf("2.Decimal to octal.\n");
printf("3.Decimal to hexadecimal.\n");
printf("4.Exit.\n");
printf("\n\t\t\tEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
base=2;
break;
case 2:
base=8;
break;
case 3:
base=16;
break;
case 4:
printf("Exiting ...");
exit(1);
default:
printf("Invalid choice.\n\n");
continue;
}
printf("Enter a number:");
scanf("%d", &num);
printf("Answer is=");
convert_to_x_base(num, base);
printf("\n\n");
}
return 0;
}
void convert_to_x_base(int num, int base)
{
int rem;
if (num == 0)
{
return;
}
else
{
rem = num % base;
convert_to_x_base(num/base,base);
if(base == 16 && rem >= 10)
{
printf("%c",rem+55);
}
else
{
printf("%d",rem);
}
}
}

Output-
1.Decimal to binary.
2.Decimal to octal.
3.Decimal to hexadecimal.
4.Exit.

Enter your choice:1


Enter a number:45
Answer is=101101

1.Decimal to binary.
2.Decimal to octal.
3.Decimal to hexadecimal.
4.Exit.

Enter your choice:2


Enter a number:20
Answer is=24

1.Decimal to binary.
2.Decimal to octal.
3.Decimal to hexadecimal.
4.Exit.

Enter your choice:3


Enter a number:20
Answer is=14

1.Decimal to binary.
2.Decimal to octal.
3.Decimal to hexadecimal.
4.Exit.

Enter your choice:4


Exiting ...
………………………………………………………………………………………………………………………….

Q27. Write a C program to check whether a input number is


Armstrong number or not.
Input-
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("Enter the number:");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("Armstrong number");
else
printf("Not Armstrong number");
return 0;
}

Output-
Enter the number:45
Not Armstrong number
………………………………………………………………………………………………………………………….

Q28. Write a program to accept a number and count


number of even, odd and zero digits within that number.
Input-
#include <stdio.h>
int main()
{
int odd,even,num,digit,zero=0;
printf("Enter four digit number:");
scanf("%d",&num);
while (num>0)
{
digit=num%10;
num/=10;
if(digit!=0&&digit%2==0)
{
even++;
}
else if(digit==0)
{
zero++;
}
else
{
odd++;
}
}
printf("\nOdd digit:%d\nEven digit:%d\nZeros:%d",odd,even,zero);
return 0;
}

Output-
Enter four digit number:4612

Odd digit:548818657
Even digit:32767
Zeros:0
………………………………………………………………………………………………………………………….

Q29. Write a C program to check whether a input number is


perfect number of not.
Input-
#include<stdio.h>
int main()
{
int n,i=1,sum=0;
printf("Enter a number:");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
return 0;
}

Output-
Enter a number:5
5 is not a perfect number
………………………………………………………………………………………………………………………….

Q30. Write a program having a menu with the following


options and corresponding actions
Options Actions

1. Area of square Accept length ,Compute area of square and print

2. Area of Rectangle Accept length and breadth, Compute area of rectangle and

print

3. Area of triangle Accept base and height , Compute area of triangle and

Print

Input-
#include<stdio.h>
#include<conio.h>
void square()
{
float length,AOS;
printf("\nEnter length:");
scanf("%f",&length);
AOS=(length*length);
printf("\nArea of square:%f",AOS);
}
void rectangle()
{
float length,breadth,AOR;
printf("\nEnter length:");
scanf("%f",&length);
printf("\nEnter breadth:");
scanf("%f",&breadth);
AOR=length*breadth;
printf("\nArea of Rectangle:%f",AOR);
}
void triangle()
{
float height,base,AOT;
printf("\nEnter height:");
scanf("%f",&height);
printf("\nEnter base: ");
scanf("%f",&base);
AOT=(height*base)/2;
printf("\nArea of triangle: %f",AOT);
}
void main()
{
int ch;
do
{
printf("\n1)Area of Square\n2)Area of Rectangle\n3)Area of
triangle\n4)Exit.\n\t\tEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:square();break;
case 2:rectangle();break;
case 3:triangle();break;
}
}while(ch!=4);
}

Output-
1)Area of Square

2)Area of Rectangle

3)Area of triangle

4)Exit.

Enter choice:1

Enter length:8

Area of square:64.000000

1)Area of Square

2)Area of Rectangle

3)Area of triangle

4)Exit.

Enter choice:2

Enter length:6

Enter breadth:5
Area of Rectangle:30.000000

1)Area of Square

2)Area of Rectangle

3)Area of triangle

4)Exit.

Enter choice:3

Enter height:6

Enter base: 4

Area of triangle: 12.000000

1)Area of Square

2)Area of Rectangle

3)Area of triangle

4)Exit.

Enter choice:4

………………………………………………………………………………………………………………………….

Q31. Write a C program to calculate x y without using standard


library function.
Input-
#include<stdio.h>
#include<conio.h>
void main()
{
int i,x,y,r=1,t;
printf("enter the value of x:");
scanf("%d",&x);
printf("enter the value of y:");
scanf("%d",&y);
for(i=1;i<=y;i++)
{
t=x;
r=r*t;
}
printf("Result:%d",r);
getch();
}

Output-
enter the value of x:7
enter the value of y:6
Result:117649
………………………………………………………………………………………………………………………….

Q32. Write a program to display union and intersection of two 1D


array.
Input-
#include<stdio.h>
int printUnion(int arr1[], int arr2[], int len1, int len2)
{
int f, i, j, k = 0;
int arr3[100];
for (i = 0; i < len1; i++) {
arr3[k] = arr1[i];
k++;
}
for (i = 0; i < len2; i++) {
f = 0;
for (j = 0; j < len1; j++) {
if (arr2[i] == arr1[j]) {
f = 1;
}
}
if (f == 0) {
arr3[k] = arr2[i];
k++;
}
}
printf("Union of two array is:");
for (i = 0; i < k; i++) {
printf("%d ", arr3[i]);
}
}
void printIntersection(int arr1[], int arr2[], int len1, int len2) {
int arr3[100];
int i, j, k = 0;
for (i = 0; i < len1; i++) {
for (j = 0; j < len2; j++) {
if (arr1[i] == arr2[j]) {
arr3[k] = arr1[i];
k++;
}
}
}
printf("\nIntersection of two array is:");
for (i = 0; i < k; i++) {
printf("%d ", arr3[i]);
}
}
int main() {
int arr1[100];
int arr2[100];
int arr3[100];
int i, j, len1, len2;
printf("Enter size of 1st array:");
scanf("%d", &len1);
printf("Enter 1st array elements:");
for (i = 0; i < len1; i++) {
scanf("%d", &arr1[i]);
}
printf("Enter size of 2nd array:");
scanf("%d", &len2);
printf("Enter 2nd array elements:");
for (i = 0; i < len2; i++) {
scanf("%d", &arr2[i]);
}
printUnion(arr1, arr2, len1, len2);
printIntersection(arr1, arr2, len1, len2);
return 0;
}

Output-
Enter size of 1st array:2
Enter 1st array elements:45
63
Enter size of 2nd array:2
Enter 2nd array elements:45
89
Union of two array is:45 63 89
Intersection of two array is:45
………………………………………………………………………………………………………………………….

Q33. Write a C program to display multiplication table of a given


input number .
Input-
#include <stdio.h>

int main()
{
int number, i = 1;

printf(" Enter the Number:");


scanf("%d", &number);
printf("Multiplication table of %d:\n ", number);
printf("--------------------------\n");
while (i <= 10)
{
printf(" %d x %d = %d \n ", number, i, number * i);
i++;
}

return 0;
}

Output-
Enter the Number:5
Multiplication table of 5:
--------------------------
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
………………………………………………………………………………………………………………………….

Q34. Write a menu driven program to perform the following


operation on m*n Matrix
1. Display transpose of a matrix
2. Calculate sum of all odd elements of matrix
Input-
#include<stdio.h>
#include<conio.h>
int a[20][20],m,n,r,c,ch,sum;
int Accept()
{
printf("\nEnter number of rows:");
scanf("%d",&r);
printf("\nEnter number of columns:");
scanf("%d",&c);
for(m=0;m<r;m++)
{
for(n=0;n<c;n++)
{
printf("\nEnter elements:");
scanf("%d",&a[m][n]);
}
}
}
int display()
{
for(m=0;m<r;m++)
{
for(n=0;n<c;n++)
{
printf("%d ",a[m][n]);
}
printf("\n");
}
}
int transpose()
{
for(m=0;m<r;m++)
{
for(n=0;n<c;n++)
{
printf("%d ",a[n][m]);
}
printf("\n");
}
}
int sumofoddnumber()
{
sum=0;
for(m=0;m<r;m++)
{
for(n=0;n<c;n++)
{
if(a[m][n]%2!=0)
{
sum=sum+a[m][n];
}
}
}
printf("Sum of all odd elements : %d\n",sum);
}
int main()
{
do
{
printf("\n1)Accept matrix\n2)Transpose of matrix\n3)sum of all odd
elements of matrix\n4)Exit\n\t\tEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:Accept();display();break;
case 2:transpose();break;
case 3:sumofoddnumber();break;
}
}while(ch!=4);
}

Output-

1)Accept matrix
2)Transpose of matrix
3)sum of all odd elements of matrix
4)Exit
Enter choice:1

Enter number of rows:2

Enter number of columns:2

Enter elements:5

Enter elements:6

Enter elements:7

Enter elements:9
56
79

1)Accept matrix
2)Transpose of matrix
3)sum of all odd elements of matrix
4)Exit
Enter choice:2
57
69

1)Accept matrix
2)Transpose of matrix
3)sum of all odd elements of matrix
4)Exit
Enter choice:3
Sum of all odd elements : 21

1)Accept matrix
2)Transpose of matrix
3)sum of all odd elements of matrix
4)Exit
Enter choice:4
………………………………………………………………………………………………………………………….

Q35. Write a C program to generate following triangle up to n lines.


1
12
123
Input-
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
printf("%d ", j);
}
printf("\n");
}
return 0;
}

Output-
Enter the number of rows:3
1
12
123
………………………………………………………………………………………………………………………….

Q36. Write a program to calculate sum of following series up to n


terms. Sum=X-X2/2!+X3/3!-…… (Note: Write separate user defined
function to calculate power and factorial)
Input-
#include <stdio.h>
#include <math.h>
double sum(int x, int n)
{
double i, total = 1.0;
for (i = 1; i <= n; i++)
total = total + (pow(x, i) / i);
return total;
}
int main()
{
int x;
int n;
printf("Enter the value of x:");
scanf("%d",&x);
printf("Enter the value of n:");
scanf("%d",&n);
printf("%f", sum(x, n));
return 0;
}

Output-
Enter the value of x:6
Enter the value of n:2
25.000000
…………….………………………………………………………………………………………………………….

Q37. Write a C program to generate following triangle up to n lines.


****
***
**
*
Input-
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i =rows; i>=1; --i)
{
for (j = 1; j <= i; ++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}

Output-
Enter the number of rows: 4
****
***
**
*
………………………………………………………………………………………………………………………….

Q38. Write a menu driven program to perform the following


operation on m*n 4 Matrix
1. Find sum of diagonal elements of matrix
2. Find sum of all even numbers of matrix
Input-
#include<stdio.h>
int main()
{
int i, j, rows, columns, a[10][10], Sum = 0;
printf("\n Please Enter Number of rows and columns : ");
scanf("%d %d", &i, &j);
printf("\n Please Enter the Matrix Elements \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &a[rows][columns]);
}
}
for(rows = 0; rows < i; rows++)
{
Sum = Sum + a[rows][rows];
}
printf("\n The Sum of Diagonal Elements of a Matrix = %d", Sum );
return 0;
}

Output-

Please Enter Number of rows and columns : 2


2

Please Enter the Matrix Elements


55
96

The Sum of Diagonal Elements of a Matrix = 11


………………………………………………………………………………………………………………………….

Q39. Write a C program to generate following triangle up to n lines.


1
23
456
Input-
#include <stdio.h>
int main()
{
int rows, i, j, number = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; ++j)
{
printf("%d ", number);
++number;
}
printf("\n");
}
return 0;
}

Output-
Enter the number of rows: 3
1
23
456
………………………………………………………………………………………………………………………….

Q40. Write a program to calculate addition of two matrices


Input-
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows matrix:");
scanf("%d",&m);
printf("Enter the number of columns matrix:");
scanf("%d",&n);
printf("Enter the elements of first matrix:\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrix:\n");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);
printf("Sum of entered matrices:\n");
for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
return 0;
}

Output-
Enter the number of rows matrix:2
Enter the number of columns matrix:2
Enter the elements of first matrix:
23
56
Enter the elements of second matrix:
69
53
Sum of entered matrices:
8 12
10 9
………………………………………………………………………………………………………………………….

Q41. Write a C program to generate following triangle up to


n lines.
A
AB
ABC
Input-
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,rows;
printf("Enter the number of rows:");
scanf("%d",&rows);
for(i = 1; i<=rows; i++)
{
for(j = 1; j <= (i*1-1); j++)
{
printf("%c", (char) (j+64));
}
printf("\n");
}
return 0;
}

Output-
Enter the number of rows:4

A
AB
ABC
………………………………………………………………………………………………………………………….
Q42. Create a structure employee (eno, ename, salary).
Accept details of n employees and write a menu driven
program to perform the following operations options.
1. Display all employees having salary > 5000
2. Display all employees
Input-
#include <stdio.h>
struct employee
{
char name[30];
int empId;
float salary;
};
int main()
{
struct employee emp;
printf("\nEnter details:\n");
printf("Name:"); gets(emp.name);
printf("ID:"); scanf("%d",&emp.empId);
printf("Salary:"); scanf("%f",&emp.salary);
printf("\nEntered detail is:\n");
printf("Name:%s\n" ,emp.name);
printf("Id:%d\n" ,emp.empId);
printf("Salary:%f\n",emp.salary);
return 0;
}
Output-

Enter details:
Name:Aditya
ID:005
Salary:5 8000000

Entered detail is:


Name:Aditya
Id:5
Salary:800000.000000
………………………………………………………………………………………………………………………….

Q43. Write a C program to generate following triangle up to


n lines.
ABC
AB
A
Input-
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,rows;
printf("Enter the number of rows:");
scanf("%d",&rows);
for(i = 1; i<=rows; i++)
{
for(j = 1; j <= (rows-i+1); j++)
{
printf("%c", (char) (j+64));
}
printf("\n");
}
return 0;
}

Output-
Enter the number of rows:3

ABC
AB
A
………………………………………………………………………………………………………………………….

Q44. Write a menu driven program to perform the following


operation on m*n Matrix
1. Find sum of non diagonal elements of matrix
2. Find sum of all odd numbers of matrix
Input-
#include<conio.h>
#include<stdio.h>
int a[20][20],m,n,r,c,ch,sum;
int Accept()
{
printf("\nEnter number of rows:");
scanf("%d",&r);
printf("\nEnter number of columns:");
scanf("%d",&c);
for(m=0;m<r;m++)
{
for(n=0;n<c;n++)
{
printf("\nEnter elements:");
scanf("%d",&a[m][n]);
}
}
}
int display()
{
for(m=0;m<r;m++)
{
for(n=0;n<c;n++)
{
printf("%d",a[m][n]);
}
printf("\n");
}
}
int sumofoddnumber()
{
sum=0;
for(m=0;m<r;m++)
{
for(n=0;n<c;n++)
{
if(a[m][n]%2!=0)
{
sum=sum+a[m][n];
}
}
}
printf("Sum of all odd elements:%d\n",sum);
}
int main()
{
do
{
printf("\n1)Accept matrix\n2)sum of all odd elements of
matrix\n3)Exit\n\t\tEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:Accept();display();break;
case 2:sumofoddnumber();break;
}
}
while(ch!=3);
}

Output-

1)Accept matrix
2)sum of all odd elements of matrix
3)Exit
Enter choice:1

Enter number of rows:2

Enter number of columns:2

Enter elements:8

Enter elements:6

Enter elements:9

Enter elements:7
86
97

1)Accept matrix
2)sum of all odd elements of matrix
3)Exit
Enter choice:2
Sum of all odd elements:16

1)Accept matrix
2)sum of all odd elements of matrix
3)Exit
Enter choice:3

………………………………………………………………………………………………………………………….

Q45. Write a C program to accept n elements of 1D array


and then display sum of all elements of array.
Input-
#include <conio.h>
int main()
{
int a[1000],i,n,sum=0;
printf("Enter size of the array:");
scanf("%d",&n);
printf("Enter elements in array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
sum+=a[i];
}
printf("sum of array is:%d",sum);
return 0;
}

Output-
Enter size of the array:5
Enter elements in array:
45545
456
456
456
46
sum of array is:46959
………………………………………………………………………………………………………………………….

Q46. Accept n integers in an array. Copy only the non-zero


elements to another array (allocated using dynamic
memory allocation). Calculate the sum and average of non-
zero elements.
Input-
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr;
int limit;
int i;
int sum;
printf("Enter limit of the array:");
scanf("%d",&limit);
ptr=(int*)malloc(limit*sizeof(int));
for(i=0;i<limit;i++)
{
printf("Enter element %02d:",i+1);
scanf("%d",(ptr+i));
}

printf("\nEntered array elements are:\n");


for(i=0;i<limit;i++)
{
printf("%d\n",*(ptr+i));
}
sum=0;
for(i=0;i<limit;i++)
{
sum+=*(ptr+i);
}
printf("Sum of array elements is: %d\n",sum);
free(ptr);
return 0;
}
Output-
Enter limit of the array:3
Enter element 01:6
Enter element 02:9
Enter element 03:5

Entered array elements are:


6
9
5
Sum of array elements is: 20
………………………………………………………………………………………………………………………….

Q47. Write a C program to find maximum elements of 1D


array
Input-
#include<stdio.h>
int main()
{
int a[1000],i,n,max;
printf("Enter size of the array:");
scanf("%d",&n);
printf("Enter elements in array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
max=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
}
printf("maximum of array is:%d",max);
return 0;
}

Output-
Enter size of the array:6
Enter elements in array:
4
4
8
3
1
4
maximum of array is:8
………………………………………………………………………………………………………………………….

Q48. Create a structure Book (Bno, Bname, Price). Accept


details of n Books and write a menu driven program to
perform the following operations options.
1. Display all Books having price > 500
2. Display Book having maximum price
Input-
#include <stdio.h>
struct book
{
char name[30];
int no;
float price;
};
int main()
{
struct book boo;
printf("\nEnter details\n");
printf("Name:"); gets(boo.name);
printf("NO:"); scanf("%d",&boo.no);
printf("Price:"); scanf("%f",&boo.price);
printf("\nEntered detail is-\n");
printf("Name:%s\n" ,boo.name);
printf("NO:%d\n" ,boo.no);
printf("Price:%f\n",boo.price);
return 0;
}

Output-

Enter details
Name:TH he Life
NO:28
Price:5000

Entered detail is-


Name:The Life
NO:28
Price:5000.000000
………………………………………………………………………………………………………………………….

Q49. Write a C program to calculate sum of all even


elements of a matrix.
Input-
#include <stdio.h>
int main()
{
int i,num,even=0;
printf("Enter the value of Matrix\n");
scanf("%d",&num);
for(i=1;i>=num;i++)
{
if (i%2==0)
even=even+i;
}
printf("Sum of all even numbers=%d\n", even);
}

Output-
Enter the value of Matrix
489
Sum of all even numbers=0
………………………………………………………………………………………………………………………….

Q50. Write a menu driven program for the following option


1. Check input number is Armstrong or not
2. Check input number is Perfect or not
Input-
#include <stdio.h>
#include <math.h>
int isPrime(int num);
int isArmstrong(int num);
int isPerfect(int num);
int main()
{
int num;
printf("Enter any number: ");
scanf("%d", &num);
if(isPrime(num))
{
printf("%d is Prime number.\n", num);
}
else
{
printf("%d is not Prime number.\n", num);
}
if(isArmstrong(num))
{
printf("%d is Armstrong number.\n", num);
}
else
{
printf("%d is not Armstrong number.\n", num);
}
if(isPerfect(num))
{
printf("%d is Perfect number.\n", num);
}
else
{
printf("%d is not Perfect number.\n", num);
}
return 0;
}
int isPrime(int num)
{
int i;
for(i=2; i<=num/2; i++)
{
if(num%i == 0)
{
return 0;
}
}
return 1;
}
int isArmstrong(int num)
{
int lastDigit, sum, originalNum, digits;
sum = 0;
originalNum = num;
digits = (int) log10(num) + 1;

while(num > 0)
{
lastDigit = num % 10;
sum = sum + round(pow(lastDigit, digits));
num = num / 10;
}
return (originalNum == sum);
}
int isPerfect(int num)
{
int i, sum, n;
sum = 0;
n = num;
for(i=1; i<n; i++)
{
if(n%i == 0)
{
sum += i;
}
}
return (num == sum);
}

Output-
Enter any number: 5
5 is Prime number.
5 is Armstrong number.
5 is not Perfect number.
………………………………………………………………………………………………………………………….

Q51. Write a C program to calculate length of string without


using standard functions.
Input-
#include <stdio.h>

void main()
{
char string[50];
int i, length = 0;

printf("Enter a string \n");


gets(string);
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of a string is the number of characters in it \n");
printf("So, the length of %s = %d\n", string, length);
}

Output-
Enter a string
sakshi
The length of a string is the number of characters in it
So, the length of sakshi = 6
………………………………………………………………………………………………………………………….

Q52. Write a program to display the elements of an array


containing n integers in the Reverse order using a pointer to
the array.
Input-
#include<stdio.h>
#define MAX 30
int main()
{
int size, i, arr[MAX];
int *ptr;
ptr = &arr[0];
printf("\nEnter the size of array:");
scanf("%d",&size);
printf("\nEnter %d integers into array:",size);
for(i=0;i<size;i++)
{
scanf("%d", ptr);
ptr++;
}
ptr=&arr[size-1];
printf("\nElements of array in reverse order are:");
for(i=size-1;i>=0;i--)
{
printf("\nElement %d is %d",i,*ptr);
ptr--;
}
getch();
}

Output-

Enter the size of array:5

Enter 5 integers into array:45644


456
456
45
45

Elements of array in reverse order are:


Element 4 is 45
Element 3 is 45
Element 2 is 456
Element 1 is 456
Element 0 is 45644
………………………………………………………………………………………………………………………….

Q53. Write a program to count the occurrences of vowel


from a input string.
Input-
#include <stdio.h>
int main()
{
int c = 0, count = 0;
char s[1000];

printf("Input a string\n");
gets(s);

while (s[c] != '\0')


{
if (s[c] == 'a' || s[c] == 'A' || s[c] == 'e' || s[c] == 'E' || s[c] == 'i' || s[c] == 'I'
|| s[c] =='o' || s[c]=='O' || s[c] == 'u' || s[c] == 'U')
count++;
c++;
}

printf("Number of vowels in the string: %d", count);


return 0;
}

Output-
Input a string
you can do anything
Number of vowels in the string: 6
………………………………………………………………………………………………………………………….

Q54. Create a structure Item (Ino, Iname, Price). Accept


details of n Items and write a menu driven program to
perform the following operations options.
1. Display all Items having price > 800
2. Display Item record with Ino=2 4
Input-
#include <stdio.h>
struct item
{
char iname[30];
int ino;
float price;
};
int main()
{
struct item in;
printf("\nEnter details\n");
printf("iName:"); gets(in.iname);
printf("iNO:"); scanf("%d",&in.ino);
printf("Price:"); scanf("%f",&in.price);
printf("\nEntered detail is-\n");
printf("iName:%s\n" ,in.iname);
printf("iNO:%d\n" ,in.ino);
printf("Price:%f\n",in.price);
return 0;
}

Output-

Enter details
iName:Laptop
iNO:678
Price:67987

Entered detail is-


iName:Laptop
iNO:678
Price:67987.000000
………………………………………………………………………………………………………………………….

Q55. Write a program to accept a string and then count the


occurrences of a specific character of a string.
Input-
include <stdio.h>
#include <string.h>
int main()
{
char s[1000],c;
int i,count=0;
printf("Enter the string:");
gets(s);
printf("Enter character to be searched:");
c=getchar();
for(i=0;s[i];i++)
{
if(s[i]==c)
{
count++;
}
}
printf("character '%c' occurs %d times \n ",c,count);
return 0;
}

Output-
Enter the string:life is very beautiful when you only follow the way of your
dreams.
Enter character to be searched:a
character 'a' occurs 3 times
………………………………………………………………………………………………………………………….

Q56. Write a program to accept two numbers as range and


display multiplication table of all numbers within that
range.
Input-
#include <stdio.h>
int main()
{
int a,b,n;
printf("Enter the number from 1:");
scanf("%d",&n);
printf("Multiplication table from 1 to %d \n",n);
for(b=1;b<=10;b++)
{
for(a=1;a<=n;a++)
{
if (a<=n-1)
printf("%dx%d = %d,",a,b,b*a);
else
printf("%dx%d = %d",a,b,b*a);
}
printf("\n");
}
return 0;
}

Output-
Enter the number from 1:3
Multiplication table from 1 to 3
1x1 = 1,2x1 = 2,3x1 = 3
1x2 = 2,2x2 = 4,3x2 = 6
1x3 = 3,2x3 = 6,3x3 = 9
1x4 = 4,2x4 = 8,3x4 = 12
1x5 = 5,2x5 = 10,3x5 = 15
1x6 = 6,2x6 = 12,3x6 = 18
1x7 = 7,2x7 = 14,3x7 = 21
1x8 = 8,2x8 = 16,3x8 = 24
1x9 = 9,2x9 = 18,3x9 = 27
1x10 = 10,2x10 = 20,3x10 = 30
………………………………………………………………………………………………………………………….

Q57. Write a C program to calculate factorial of a number


using user defined function.
Input-
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}

long int multiplyNumbers(int n) {


if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}

Output-
Enter a positive integer: 6
Factorial of 6 = 720
………………………………………………………………………………………………………………………….

Q58. Write a program, which accepts a number n and


displays each digit separated by tabs. Example: 6702 Output
=6 7 0 2
Input-
#include<stdio.h>
int main()
{
int n;
printf( "Enter a seven digit number: " );
scanf("%d",&n);
printf( "\n:");
printf("%d ",(n/1000000));
n = n - ((n/1000000)*1000000);
printf("%d ", (n/100000));
n = n - ((n/100000)*100000);

printf("%d ", (n/10000));


n = n - ((n/10000)*10000);

printf("%d ", (n/1000));


n = n - ((n/1000)*1000);

printf("%d ", (n/100));


n = n - ((n/100)*100);

printf("%d ", (n/10));


n = n - ((n/10)*10);

printf("%d\n", (n%10));

return 0;
}

Output-
Enter a seven digit number: 7896542

:7 8 9 6 5 4 2
………………………………………………………………………………………………………………………….

Q59. Write a program to find sum of digits of a given input


number using user defined Function
Input-
#include<stdio.h>
int main()
{
int n,sum=0,m;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("Sum is = %d",sum);
return 0;
}

Output-
Enter a number:195635621
Sum is = 38
………………………………………………………………………………………………………………………….

Q60. Write a program to accept a number and count


number of even, odd and zero digits within that number
Input-
#include <stdio.h>
int main()
{
int odd,even,num,digit,zero=0 ;
printf("Enter four digit number:");
scanf("%d",&num);
while(num>0)
{
digit=num%10;
num/=10;
if(digit!=0 && digit%2==0)
{
even++;
}
else if(digit==0)
{
zero++;
}
else
{
odd++;
}
}
printf("\nOdd digit:%d \nEven digit:%d\nZeros:%d",odd,even,zero);
return 0;
}

Output-
Enter four digit number:8562

Odd digit:-1770259039
Even digit:32767
Zeros:0
………………………………………………………………………………………………………………………….

You might also like