C Language Slips
C Language Slips
Q1. Write a C program to accept dimensions of a cylinder and display the surface area and
volume of cylinder. [15 Marks]
#include <stdio.h>
int main()
{
float radius, height;
float surface_area, volume;
printf("Enter value for radius and height of a cylinder : \n");
scanf("%f%f", &radius, &height);
surface_area = 2 * (22 / 7) * radius * (radius + height);
volume = (22 / 7) * radius * radius * height;
printf("Surface area of cylinder is: %f", surface_area);
printf("\n Volume of cylinder is : %f", volume);
}
Q2. Create a structure employee (id, name, salary). Accept details of n employees and write a
menu driven program to perform the following operations. [25 Marks]
Search employee by id
Display all employees
#include<stdio.h>
struct details
{
char name[30];
int eid;
int salary;
}emp[5];
void emp_search(int r)
{
int id,i;
printf("\nEnter Employee-Id to be Searched : ");
scanf("%d",&id);
printf("----------------------------------------\n");
for(i=0;i<r;i++)
{
if(emp[i].eid==id)
{
printf("Employee Id : %d",emp[i].eid);
printf("\nName : %s",emp[i].name);
printf("\nSalary : %d\n",emp[i].salary);
}
}
}
void display(int r)
{
int i;
printf("\nList of All Employees:\n");
printf("-------------------------------\n");
printf("Emp-Id\tEmp-Name Salary\n");
printf("--------------------------------\n");
for(i=0;i<r;i++)
{
printf("%d\t%s\t %d\n",emp[i].eid,emp[i].name,emp[i].salary);
}
}
int main()
{
int n,i,ch;
printf("/*How Many Employee Record You Want to Add*/\n\nEnter Limit : ");
scanf("\n %d",&n);
for(i=0;i<n;i++)
{
printf("-----------------------------------------");
printf("\n\tEnter Details of Employee-%d",i+1);
printf("\n-----------------------------------------");
printf("\nName of Employee : ");
scanf("%s",emp[i].name);
printf("Employee-Id : ");
scanf("%d",&emp[i].eid);
printf("Salary : ");
scanf("%d",&emp[i].salary);
}
while(1)
{
printf("-----------------------------------------\n");
printf("\t\tMenu\n");
printf("-----------------------------------------");
printf("\n 1:Search Employee by E-ID");
printf("\n 2:List of All Employee");
printf("\n 3:Exit");
printf("\n----------------------------------------\n");
printf("Enter Your Choice : ");
scanf("\n %d",&ch);
switch(ch)
{
case 1: emp_search(n);
break;
case 2: display(n);
break;
case 3: exit(0);
}
}
return 0;
}
‘
Slip2
Q1. Write a C program to accept radius of a circle and display the area and circumference
of a circle. [15 Marks]
#include<stdio.h>
int main() {
int rad;
float PI = 3.14, area, ci;
ci = 2 * PI * rad;
printf("\nCircumference : %f ", ci);
return (0);
}
Q2. Write a program to calculate sum of following series up to n terms. [25 Marks]
Sum=X+X2/2!+X3/3!+……
(Note: Write separate user defined function to calculate power and factorial)
#include<math.h>
#include<stdio.h>
int fact(int index)
{
int f = 1, i;
for(i = 1; i <= index; i ++)
{
f = f*i;
}
return f;
}
// Driver Code
void main()
{
int x = 1;
int n = 2;
double sum = 0, m;
// Sum of n-1 terms starting from 2nd term
int i;
for (i = 1; i < =n; i++) {
m = pow(x, i) / fact(i);
sum = sum + m;
}
printf("\n%.4f", sum);
getch();
}
Slip 3
Q1. 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) [15 Marks]
#include<stdio.h>
#include<conio.h>
int main()
{
float cel,fer,kel;
cel= (fer-32)/1.8 ;
printf("Celsius = %f \n",cel);
return (0) ;
}
Q2. Write a menu driven program to perform the following operations on strings using standard
library functions: [25 Marks]
Length of String 2. Copy String 3. Connect Two Strings 4. Compare two strings
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20],str1[20];
int ch,i,j;
clrscr();
do
{
printf("\n****MENU****");
printf("\n1:Find Length");
printf("\n2:Copy the Strings");
printf("\n3:Compare the Strings");
printf("\n4:Concatenate the Strings");
printf("\n5:Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the string: ");
scanf("%s",&str);
i=strlen(str);
printf("\nThe Length of given string is: %d",i);
break;
case 2:
printf("\nEnter the first string: ");
scanf("%s",&str);
printf("\nEnter the second string: ");
scanf("%s",&str1);
strcpy(str,str1);
printf("\nThe Copied string is: %s",str);
break;
case 3:
printf("\nEnter the first string: ");
scanf("%s",&str);
printf("\nEnter the second string: ");
scanf("%s",&str1);
j=strcmp(str,str1);
if(j==0)
{
printf("\nThe string is same");
}
else
{
printf("\nThe string is not same");
}
break;
case 4:
printf("\nEnter the first string: ");
scanf("%s",&str);
printf("\nEnter the second string: ");
scanf("%s",&str1);
strcat(str,str1);
printf("\nThe Concatenated string is: %s",str);
break;
case 5:
exit(0);
break;
}
}
while(ch!=5);
getch();
}
Slip4
Q1. 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) ) [15 Marks]
#include <stdio.h>
int main()
{
int a,b;
float arithmetic_mean,harmonic_mean;
printf("enter two no. A and B :-");
scanf("%d%d",&a,&b);
arithmetic_mean = (a+b) /2;
harmonic_mean = (a*b) / (a+b);
printf("arithmetic mean = %f and harmonic mean = %f",arithmetic_mean,harmonic_mean);
}
Q2. Create a structure Student (id, name, marks). Accept details of n students and write a menu
driven program to perform the following operations. [25 Marks]
a) Search student by id
b) Display all students
#include<stdio.h>
#include<stdlib.h>
struct details
{
char name[30];
int eid;
int salary;
}emp[5];
void emp_search(int r)
{
int id,i;
printf("\nEnter Employee-Id to be Searched : ");
scanf("%d",&id);
printf("----------------------------------------\n");
for(i=0;i<r;i++)
{
if(emp[i].eid==id)
{
printf("Employee Id : %d",emp[i].eid);
printf("\nName : %s",emp[i].name);
printf("\nSalary : %d\n",emp[i].salary);
}
}
}
void display(int r)
{
int i;
printf("\nList of All Employees:\n");
printf("-------------------------------\n");
printf("Emp-Id\tEmp-Name Salary\n");
printf("--------------------------------\n");
for(i=0;i<r;i++)
{
printf("%d\t%s\t %d\n",emp[i].eid,emp[i].name,emp[i].salary);
}
}
int main()
{
int n,i,ch;
printf("/*How Many Employee Record You Want to Add*/\n\nEnter Limit : ");
scanf("\n %d",&n);
for(i=0;i<n;i++)
{
printf("-----------------------------------------");
printf("\n\tEnter Details of Employee-%d",i+1);
printf("\n-----------------------------------------");
printf("\nName of Employee : ");
scanf("%s",emp[i].name);
printf("Employee-Id : ");
scanf("%d",&emp[i].eid);
printf("Salary : ");
scanf("%d",&emp[i].salary);
}
while(1)
{
printf("-----------------------------------------\n");
printf("\t\tMenu\n");
printf("-----------------------------------------");
printf("\n 1:Search Employee by E-ID");
printf("\n 2:List of All Employee");
printf("\n 3:Exit");
printf("\n----------------------------------------\n");
printf("Enter Your Choice : ");
scanf("\n %d",&ch);
switch(ch)
{
case 1: emp_search(n);
break;
case 2: display(n);
break;
case 3: exit(0);
}
}
return 0;
}
Slip5
Q1. 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 ) [15 Marks]
#include <stdio.h>
int main()
{
float length, width, height;
float SA, Volume;
Q2.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 ? [25 Marks]
# include<stdio.h>
/* Function to get sum of digits */
int main()
{
int c = 0;
char ch, s[100];
printf("Input a string\n");
gets(s);
for(c=0;s[c] != '\0';c++) {
ch = s[c];
if (ch >= 'A' && ch <= 'Z')
s[c] = s[c] + 32;
else if (ch >= 'a' && ch <= 'z')
s[c] = s[c] - 32;
else if(s[c]==' ')
s[c] = '*';
else if(s[c]>0 || s[c]<9)
s[c] = '?';
}
puts(s);
return 0;
}
Slip6
Q1. 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”. [15 Marks]
#include <stdio.h>
int main()
{
char ch;
printf("Enter character:\t");
scanf("%c", &ch);
printf("You entered: %c\n", ch);
printf("Previous character: %c\n", ch - 1);
printf("Next character: %c\n", ch + 1);
}
Q2. Write a program to accept a string and then count the occurrences of a specific character of a
string. [25 Marks]
#include <stdio.h>
#include <string.h>
// Driver code
int main()
{ char str[50]= "geeksforgeeks";
char c ;
int res = 0;
printf("\n enter character ");
scanf("%c",&c);
for (int i=0;i<strlen(str);i++)
// checking character in string
if (str[i] == c)
res++;
printf("occurence of %c=%d",c,res++);
return 0; }
Slip7
Q1. Write a C program to accept the x and y coordinates of two points and compute the
distance between the two points. [15 Marks]
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float distance, a, b, c, d;
printf("\nEnter The Coordinates of Point A:\n");
printf("\nX - Axis Coordinate: \t");
scanf("%f", &a);
printf("\nY - Axis Coordinate: \t");
scanf("%f", &b);
printf("\nEnter The Coordinates of Point B:\n");
printf("\nx - Axis Coordinate:\t");
scanf("%f", &c);
printf("\nY - Axis Coordinate: \t");
scanf("%f", &d);
distance = sqrt((c - a) * (c - a) + (d - b) * (d- b));
printf("\nDistance between Points A and B: %f\n", distance);
return 0;
}
Q2. Write a program to calculate Multiplication of two matrices of order m*n.
[25 Marks]
#include <stdio.h>
int main()
{
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
Slip8
Q1. 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. [15 Marks]
#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);
}
Q2. Write a menu driven program to perform the following operation on m*n
Matrix [25 Marks]
1. Calculate sum of upper triangular matrix elements
2. Calculate sum of diagonal elements
#include <stdio.h>
void main()
{
int fig_code;
float side, base, length, breadth, height, area, radius;
printf("-------------------------\n");
printf(" 1 --> sum of upper\n");
printf(" 2 --> sum of dignola\n");
printf("-------------------------\n");
int i, j, a[10][10], sum, rows, columns;
scanf("%d", &fig_code);
switch(fig_code)
{
case 1:
sum = 0;
for (i = 0; i < rows; i++)
for (j = 0; j < columns; j++) {
// Condition for Upper Triangle
if (i < j) {
sum = sum + a[i][j];
}
}
printf("sum of upper=%d",sum);
break;
case 2:
sum = 0;
for (i = 0; i < rows; i++)
for (j = 0; j < columns; j++) {
// Condition for Upper Triangle
if (i == j) {
sum = sum + a[i][j];
}
}
printf("sum of digonal=%d",sum);
break;
default:
printf("Error in figure code\n");
break;
}
}
Slip 9
Q1. Write a C program to accept a character from the user and check whether the character is a
vowel or consonant. [15 Marks]
#include <stdio.h>
int main()
{
char c;
int isLowercaseVowel, isUppercaseVowel;
printf("Enter an alphabet: ");
scanf("%c",&c);
isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
if (isLowercaseVowel || isUppercaseVowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}
Q2. Write a program to accept two numbers as range and display multiplication table
of all numbers within that range. [25 Marks]
#include <stdio.h>
int main()
{ int i,j;
for(j=1; j<=5; j++)
{
for(i=1; i<=10; i++)
{ //printf("\t ");
printf(" \n %d * %d = %d ",j, i, j*i);
}
printf("\n");
}
return 0;
}
Slip 10
Q1. Write a C program to accept the x and y coordinate of a point and find the quadrant in
which the point lies. [15 Marks]
#include <stdio.h>
void main()
{
int x, y;
Q2. Write a program, which accepts a number n and displays each digit in words.
Example: 6702 Output = Six-Seven-Zero-Two [25 Marks]
#include<stdio.h>
int main()
{
int a,n=1,x,m=0,p;
printf("enter no :");
scanf("%d",&a);
p=a;
while(p!=0)
{
p=p/10;
n=n*10;
}
while(a!=0)
{
n=n/10;
x=a%10;
a=a/10;
m=m+(x*n);
}
while(m!=0)
{
x=m%10;
m=m/10;
switch(x)
{
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;
}
}
}
Slip11
Q1. 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.
[15 Marks]
#include <stdio.h>
int main()
{
int cp,sp, amt;
/* Input cost price and selling price of a product */
printf("Enter cost price: ");
scanf("%d", &cp);
printf("Enter selling price: ");
scanf("%d", &sp);
if(sp > cp)
{
/* Calculate Profit */
amt = sp - cp;
printf("Profit = %d", amt);
}
else if(cp > sp)
{
/* Calculate Loss */
amt = cp - sp;
printf("Loss = %d", amt);
}
else
{
/* Neither profit nor loss */
printf("No Profit No Loss.");
}
return 0;
}
Q2. Accept radius from the user and write a program having menu with the following
options and corresponding actions [25 Marks]
Options Actions
1. 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
#include<stdio.h>
void main()
{
int r,choice;
float area,c,vol,PI= 3.142;
switch(choice)
{
case 1:
area=PI*r*r;
printf("\n The area of the circle is %f \n \n",area);
break;
case 2:
c=2*PI*r;
printf("\n The circumference of the circle is %f \n \n",c);
break;
case 3:
vol=(4/3)*PI*r*r;
printf("\n The volume of the sphere is %f \n \n",vol);
break;
default:
printf("\n You haven made invalid choice \n \n");
}
} // End of the code
Slip12
Q1. Write a C program to calculate sum of digits of a given input number.[15 Marks]
#include <stdio.h>
int main()
{
int num, sum=0;
return 0;
}
Q2. Accept two numbers from user and write a menu driven program to perform
the following operations [25 Marks]
1. swap the values of two variables
2. calculate arithmetic mean and harmonic mean of two numbers
#include<stdio.h>
void main()
{
int n1,n2,t,choice;
float arithmetic_mean,harmonic_mean;
switch(choice)
{
case 1:
t=n1;
n1=n2;
n2=t;
printf("\n swap=%d %d \n \n",n1,n2);
break;
case 2:
arithmetic_mean = (n1+n2) /2;
harmonic_mean = (n1*n2) / (n1+n2);
printf("arithmetic mean = %f and harmonic mean = %f",arithmetic_mean,harmonic_mean);
break;
default:
printf("\n You haven made invalid choice \n \n");
}
} // End of the code
Slip13
Q1. Write a C program to accept the value of n and display sum of all odd numbers
up to n. [15 Marks]
#include <stdio.h>
int main()
{
int i, n, sum=0;
/* Input range to find sum of odd numbers */
printf("Enter upper limit: ");
scanf("%d", &n);
/* Find the sum of all odd number */
for(i=1; i<=n; i+=2)
{
sum += i;
}
printf("Sum of odd numbers = %d", sum);
return 0;
}
Q2. Write a program to accept a decimal number and convert it to binary, octal and
hexadecimal number. [25 Marks]
int main(void)
{
int num, choice,oct=0, base;
int bin = 0;
int i = 0, rem;
char hex_arr[50];
printf("Enter a number: ");
scanf("%d", &num);
while(1)
{
printf("Select conversion: \n\n");
printf("1. Decimal to binary. \n");
printf("2. Decimal to octal. \n");
printf("3. Decimal to hexadecimal. \n");
printf("4. Exit. \n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
base = 2;
while(num != 0)
{
rem = num % 2; // get the remainder
bin = rem * (long long)pow(10, i++) + bin;
num /= 2; // get the quotient
}
printf("%lld", bin);
break;
case 2:
base = 8;
while(num != 0)
{
rem = num % 8; // get the last digit
oct = rem * (long long)pow(10, i++) + oct;
num /= 8; // get the quotient
}
printf("0o");
printf("%lld", oct);
break;
case 3:
base = 16;
while(num != 0)
{
rem = num % 16; // get the right most digit
if (rem < 10)
{
hex_arr[i++] = 48 + rem;
}
else
{
hex_arr[i++] = 55 + rem;
}
num /= 16; // get the quotient
}
printf("0x");
for(int j = i - 1; j >= 0 ; j--) // print the hex_arr in reverse order
{
printf("%c", hex_arr[j]);
}
break;
case 4:
printf("Exiting ...");
exit(1);
default:
printf("Invalid choice.\n\n");
continue;
}
}
}
Slip14
Q1. Write a C program to check whether a input number is Armstrong number or not.
[15 Marks]
#include <stdio.h>
int main()
{
int number, originalNumber, remainder, result = 0;
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}
if(result == number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);
return 0;
}
Q2. Write a program to accept a number and count number of even, odd and zero digits
within that number. [25 Marks]
#include <stdio.h>
int main()
{
int nodd,neven,num,digit,zero=0 ;
printf("Enter four digit number: ");
scanf("%d",&num);
while (num> 0)
{
digit = num % 10; /* separate LS digit from number */
num /= 10;
if(digit != 0 && digit % 2 == 0)
{
neven++;
}
else if(digit==0)
{
zero++;
}
else
{
nodd++;
}
}
printf("\nOdd digit : %d \nEven digit : %d\nZeros : %d", nodd, neven,zero);
return 0;
}
Slip15
Q1. Write a C program to check whether a input number is perfect number of not.
[15 Marks]
#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;
}
Q2. Write a program having a menu with the following options and corresponding actions
[25 Marks]
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
#include <stdio.h>
void main()
{
int fig_code;
float side, base, length, breadth, height, area, radius;
printf("-------------------------\n");
printf(" 1 --> Rectangle\n");
printf(" 2 --> Triangle\n");
printf(" 3 --> Square\n");
printf("-------------------------\n");
printf("Enter the Figure code\n");
scanf("%d", &fig_code);
switch(fig_code)
{
case 1:
printf("Enter the breadth and length\n");
scanf("%f %f", &breadth, &length);
area = breadth * length;
printf("Area of a Reactangle = %f\n", area);
break;
case 2:
printf("Enter the base and height\n");
scanf("%f %f", &base, &height);
area = 0.5 * base * height;
printf("Area of a Triangle = %f\n", area);
break;
case 3:
printf("Enter the side\n");
scanf("%f", &side);
area = side * side;
printf("Area of a Square=%f\n", area);
break;
default:
printf("Error in figure code\n");
break;
}
}
Slip16
Q1. Write a C program to calculate xy without using standard library function.
[15 Marks]
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,i,r=1,t;
printf("Enter a number:");
scanf("%d",&x);
printf("Enter the power:");
scanf("%d",&y);
for(i=1;i<=y;i++)
{
t=x;
r=r*t;
}
printf("Result:%d",r);
getch();
}
Q2. Write a program to display union and intersection of two 1D array. [25 Marks]
#include<stdio.h>
Slip17
Q1. Write a C program to display multiplication table of a given input number [15 Marks]
#include <stdio.h>
int main() {
int num, i = 1;
printf("
Enter any Number:");
scanf("%d", &num);
printf("Multiplication table of %d: ", num);
while (i <= 10) {
printf("
%d x %d = %d", num, i, num * i);
i++;
}
return 0;
}
Q2. Write a menu driven program to perform the following operation on m*n
Matrix [25 Marks]
1. Display transpose of a matrix
2. Calculate sum of all odd elements of matrix
#include <stdio.h>
void main()
{
int a[10][10], transpose[10][10], r, c, i, j,sum=0, fig_code;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
printf("-------------------------\n");
printf(" 1 --> Transpose\n");
printf(" 2 --> Sum of odd elements\n");
printf("-------------------------\n");
printf("Enter the Figure code\n");
scanf("%d", &fig_code);
switch(fig_code)
{
case 1:
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}
printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",transpose[i][j]);
if(j==r-1)
printf("\n\n");
}
break;
case 2:
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
if(a[i][j]%2!=0)
sum=sum+a[i][j];
}
printf("\n sum of odd elements= %d",sum);
break;
default:
printf("Error in figure code\n");
break;
}
}
Slip18
Q1. Write a C program to generate following triangle up to n lines. [15 Marks]
1
12
123
#include <stdio.h>
int main()
{
int i, j, rows;
Q2. Write a program to calculate sum of following series up to n terms. [25 Marks]
Sum=X-X
2
/2!+X3
/3!-……
#include<math.h>
#include<stdio.h>
int fact(int index)
{
int f = 1, i;
for(i = 1; i <= index; i ++)
{
f = f*i;
}
return f;
}
void main()
{
int x = 1;
int n = 3;
double sum = 0,term=-1, m;
// Sum of n-1 terms starting from 2nd term
int i;
for (i = 1; i <= n; i++) {
Q2. Write a menu driven program to perform the following operation on m*n
Matrix [25 Marks]
1. Find sum of diagonal elements of matrix
2. Find sum of all even numbers of matrix
#include <stdio.h>
void main()
{
int a[10][10], transpose[10][10], r, c, i, j,sum=0, fig_code;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
printf("-------------------------\n");
printf(" 1 --> sum of diagonal elements\n");
printf(" 2 --> Sum of even elements\n");
printf("-------------------------\n");
printf("Enter the Figure code\n");
scanf("%d", &fig_code);
switch(fig_code)
{
case 1:
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==j)
{
sum=sum+a[i][j];
}
}
} printf("\n sum of digonal elements= %d",sum);
break;
case 2:
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
if(a[i][j]%2==0)
sum=sum+a[i][j];
}
printf("\n sum of even elements= %d",sum);
break;
default:
printf("Error in figure code\n");
break;
}
}
Slip20
Q1. Write a C program to generate following triangle up to n lines. [15 Marks]
1
23
456
#include <stdio.h>
int main()
{
int rows, i, j, number= 1;
printf("\n");
}
return 0;
}
void main()
{
int a[10][10], transpose[10][10], r, c, i, j,sum=0, fig_code;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
printf("-------------------------\n");
printf(" 1 --> sum of non diagonal elements\n");
printf(" 2 --> Sum of odd elements\n");
printf("-------------------------\n");
printf("Enter the Figure code\n");
scanf("%d", &fig_code);
switch(fig_code)
{
case 1:
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i!=j)
{
sum=sum+a[i][j];
}
}
} printf("\n sum of non digonal elements= %d",sum);
break;
case 2:
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
if(a[i][j]%2!=0)
sum=sum+a[i][j];
}
printf("\n sum of odd elements= %d",sum);
break;
default:
printf("Error in figure code\n");
break;
}
}
Slip 23
Q1. Write a C program to accept n elements of 1D array and then display sum of
all elements of array. [15 Marks]
#include <stdio.h>
int main()
{
int arr[10];
int sum,i;
printf("\nEnter elements : \n");
for(i=0; i<10; i++)
{
printf("Enter arr[%d] : ",i);
scanf("%d",&arr[i]);
}
sum=0;
for(i=0; i<10; i++)
{
sum=sum+arr[i];
}
printf("\nSum of array is : %d" ,sum);
return 0;
}
Q2. 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. [25 Marks]
#include<stdio.h>
#include<stdlib.h>
void main(){
int a[30], i,n, sum = 0;
int count=0;
int *ptr;
float avg;
printf("\n Enter the total number of elements you want to enter : ");
scanf("%d",&n);
printf("\n Enter element in aaray");
for(i = 0;i<n;i++){
scanf("%d",&a[i]); }
for(i = 0;i<n;i++){
if(a[i]!=0){
count++; }
}
ptr = (int *)malloc(count * sizeof(int));
for(i = 0;i<n;i++){
if(a[i]!=0){
*(ptr+i)=a[i];
sum=sum+*(ptr+i); }
}
avg=sum/count;
printf("sum is %d \n",sum);
printf("avg is %f\n",avg);
getch();
}
Slip 24
Q1. Write a C program to find maximum elements of 1D array [15 Marks]
#include <stdio.h>
int main()
{
int i, n;
float arr[100];
return 0;
}
Q2. Create a structure Book (Bno, Bname, Price). Accept details of n Books and write a menu
driven program to perform the following operations options. [25 Marks]
1. Display all Books having price > 500
2. Display Book having maximum price
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct book
{
int bno,bcost,baccno;
char bname[20],bpub[20],bauthor[20];
}p[10];
int main()
{
int n,i,ch,largest ;
char pubname[20],authorname[20];
printf("/*How Many Records of Books You Want to Add*/\n\nEnter Limit : ");
scanf("%d",&n);
printf("------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("\tEnter Details of Book-%d",i+1);
printf("\n------------------------------------------\n");
printf("Book Number : ");
scanf("%d",&p[i].bno);
printf("Book Name : ");
scanf("%s",p[i].bname);
printf("Author Name : ");
scanf("%s",p[i].bauthor);
printf("Publication : ");
scanf("%s",p[i].bpub);
printf("Cost : ");
scanf("%d",&p[i].bcost);
printf("Accession Number : ");
scanf("%d",&p[i].baccno);
printf("------------------------------------------\n");
}
while(1)
{
printf("\n\t\tMENU\n");
printf("------------------------------------------\n");
printf("\n1.All Books Costing Above Rs. 500");
printf("\n2. Books having maximum price");
printf("\n3.Exit");
printf("\n------------------------------------------\n");
printf("\nEnter Your Choice : ");
scanf("%d",&ch);
printf("\n");
switch(ch)
{
case 1:
for(i=0;i<n;i++)
{
if(p[i].bcost>500)
{
printf("Book Number : %d\n",p[i].bno);
printf("Book Name : %s \n",p[i].bname);
printf("Cost : %d\n",p[i].bcost);
printf("Accession Number : %d\n",p[i].baccno);
printf("\n------------------------------------------\n");
}
}
break;
case 2:
for(i=0;i<n;i++)
{ largest = p[0].bcost;
if (largest < p[i].bcost){
largest = p[i].bcost;
}
}
printf("Cost : %d\n",largest);
for(i=0;i<n;i++)
{
if(p[i].bcost==largest)
{
printf("Book Number : %d\n",p[i].bno);
printf("Book Name : %s \n",p[i].bname);
printf("Cost : %d\n",p[i].bcost);
printf("Accession Number : %d\n",p[i].baccno);
printf("\n------------------------------------------\n");
}
}
break;
case 3:
exit(0);
}
}
return 0;
}
Slip 25
Q1. Write a C program to calculate sum of all even elements of a matrix. [15 Marks]
#include<stdio.h>
int main(){
/* 2D array declaration*/
int disp[2][3],sum=0;
/*Counter variables for the loop*/
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
//Displaying sum of all even array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
sum=sum+disp[i][j];
}
}
}
printf("sum=%d=", sum);
return 0;
}
Q2. Write a menu driven program for the following option [25 Marks]
1. Check input number is Armstrong or not
2. Check input number is Perfect or not
#include <stdio.h>
void main()
{
int fig_code;
int number, originalNumber, remainder, result = 0;
int n,i=1,sum=0;
while(1)
{
printf("-------------------------\n");
printf(" 1 --> Armstrong or not\n");
printf(" 2 --> Perfect or not \n");
printf("-------------------------\n");
printf("Enter the Figure code\n");
scanf("%d", &fig_code);
switch(fig_code)
{
case 1:
printf("Enter a three digit integer: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}
if(result == number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);
break;
case 2:
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);
break;
default:
exit(0); }
}}
Slip 26
Q1. Write a C program to calculate length of string without using standard functions.
[15 Marks]
#include <stdio.h>
int main()
{
char s[1000];
int i;
Q2. Write a program to display the elements of an array containing n integers in the
Reverse order using a pointer to the array. [25 Marks]
#include<stdio.h>
void main()
{ int size, i, arr[30];
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();
}
Slip 27
Q1. Write a program to count the occurrences of vowel from a input string. [15 Marks]
#include <stdio.h>
int main()
{
char line[150];
int i, vowels;
vowels = 0;
printf("Enter a line of string: ");
scanf("%[^\n]", line);
for(i=0; line[i]!='\0'; ++i)
{ if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
++vowels;
}
}
printf("Vowels: %d",vowels);
return 0;
}
Q2. Create a structure Item (Ino, Iname, Price). Accept details of n Items and write a
menu driven program to perform the following operations options. [25 Marks]
1. Display all Items having price > 800
2. Display Item record with Ino=2
#include<stdio.h>
#include<stdlib.h>
struct details
{
char name[30];
int eid;
int salary;
}emp[5];
void emp_search(int r)
{
int id,i;
printf("\nEnter Employee-Id to be Searched : ");
scanf("%d",&id);
printf("----------------------------------------\n");
for(i=0;i<r;i++)
{
if(emp[i].eid==id)
{
printf("Employee Id : %d",emp[i].eid);
printf("\nName : %s",emp[i].name);
printf("\nSalary : %d\n",emp[i].salary);
}
}
}
void display(int r)
{
int i;
printf("\nList of All Employees:\n");
printf("-------------------------------\n");
printf("Emp-Id\tEmp-Name Salary\n");
printf("--------------------------------\n");
for(i=0;i<r;i++)
{
printf("%d\t%s\t %d\n",emp[i].eid,emp[i].name,emp[i].salary);
}
}
void greater(int r)
{
int i;
printf("\nDetails of Employee Whose Salary > 10000\n");
printf("------------------------------------------------");
for(i=0;i<r;i++)
{
if(emp[i].salary>800)
{
printf("\n Employee Name : %s",emp[i].name);
printf("\n Employee-Id : %d",emp[i].eid);
printf("\n Salary : %d\n",emp[i].salary);
}
}
}
int main()
{
int n,i,ch;
printf("/*How Many Employee Record You Want to Add*/\n\nEnter Limit : ");
scanf("\n %d",&n);
for(i=0;i<n;i++)
{
printf("-----------------------------------------");
printf("\n\tEnter Details of Employee-%d",i+1);
printf("\n-----------------------------------------");
printf("\nName of Employee : ");
scanf("%s",emp[i].name);
printf("Employee-Id : ");
scanf("%d",&emp[i].eid);
printf("Salary : ");
scanf("%d",&emp[i].salary);
}
while(1)
{
printf("-----------------------------------------\n");
printf("\t\tMenu\n");
printf("-----------------------------------------");
printf("\n 1:Search Employee by E-ID");
printf("\n 2:List of All Employee");
printf("\n 3:Display Employee Name whose Salary > 800 ");
printf("\n 4:Exit");
printf("\n----------------------------------------\n");
printf("Enter Your Choice : ");
scanf("\n %d",&ch);
switch(ch)
{
case 1: emp_search(n);
break;
case 2: display(n);
break;
case 3: greater(n);
break;
case 4: exit(0);
}
}
return 0;
}
Slip 28
Q1. Write a program to accept a string and then count the occurrences of a specific
character of a string. [15 Marks]
#include <stdio.h>
#include <string.h>
// Driver code
int main()
{
char str[50]= "geeksforgeeks";
char c ;
int res = 0;
printf("\n enter character ");
scanf("%c",&c);
for (int i=0;i<strlen(str);i++)
// checking character in string
if (str[i] == c)
res++;
printf("occurence of %c=%d",c,res++);
return 0;
}
Q2. Write a program to accept two numbers as range and display multiplication table
of all numbers within that range. [25 Marks]
#include <stdio.h>
int main()
{ int i,j;
for(j=1; j<=5; j++)
{
for(i=1; i<=10; i++)
{ //printf("\t ");
printf(" \n %d * %d = %d ",j, i, j*i);
}
printf("\n");
}
return 0;
}
Slip 29
Q1. Write a C program to calculate factorial of a number using user defined function.
[15 Marks]
#include <stdio.h>
int fact(int);
int main()
{ int n;
printf("Enter an integer: ");
scanf("%d",&n);
// show error if the user enters a negative integer
printf("Factorial of %d = %llu", n, fact(n));
return 0;
}
int fact(int n)
{
int i;
unsigned long long factorial = 1;
for(i=1; i<=n; ++i)
{
factorial *= i; // factorial = factorial*i;
return factorial;
}
}
Q2. Write a program, which accepts a number n and displays each digit separated by tabs.
Example: 6702 Output = 6 7 0 2 [25 Marks]
#include<stdio.h>
int main()
{
int digit, num,rem;
printf("Enter positive integer number: ");
scanf("%d", &num);
while(num>0)
{
rem=rem*10+num%10;
num=num/10;
}
printf("\nYou have entered: ");
while (rem > 0)
{ digit = rem % 10;
printf("\t %d",digit);
rem=rem/10;
}
return 0;
}
Slip 30
Q1. Write a program to find sum of digits of a given input number using user defined
Function [15 Marks]
# include<stdio.h>
int main()
{
int n ;
printf("\n enter no=");
scanf("%d",&n);
printf(" %d ", getSum(n));
return 0;
}
Q2. Write a program to accept a number and count number of even, odd and zero digits
within that number. [25 Marks]
#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; /* separate LS digit from number */
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;
}