C Ipe Practicals Solution
C Ipe Practicals Solution
1
printf("After Sorting on Code:\n");
for(i=0;i<n;i++)
{
printf("%d\t%s\t%d\n",it[i].code,it[i].name,it[i].qty);
}
}
OUTPUT:-
2 Write a C program that accepts choice (Addition, subtraction, multiplication, division or exit) from the
keyboard until user selects exit as a choice. The program will perform and print the result for addition,
multiplication, subtraction and division with Switch statement.
#include<stdio.h>
#include<stdlib.h>
void main()
{
int num1, num2, choice, result;
do
{
printf("\n\n1. Addition\n2. Subtraction\n3. Multiplication");
printf("\n4. Division \n5. Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter two Numbers:");
scanf("%d%d",&num1,&num2);
result=num1+num2;
2
printf("\nAddition=%d",result);
break;
case 2: printf("\nEnter two Numbers:");
scanf("%d%d",&num1,&num2);
result=num1-num2;
printf("\nSubtraction=%d",result);
break;
case 3: printf("\nEnter two Numbers:");
scanf("%d%d",&num1,&num2);
result=num1*num2;
printf("\nMultiplication=%d",result);
break;
case 4: printf("\nEnter two Numbers:");
scanf("%d%d",&num1,&num2);
if(num2==0)
printf("Divide Error..Division is not possible!");
else
{ result=num1/num2;
printf("\nSubtraction=%d",result);
}
break;
case 5: exit(0);
default: printf("\nEnter valid choice:");
}
}while(choice!=5);
}
OUTPUT:-
3
3 Write a C function to exchange two numbers and use it to reverse an array of 10 integers accepted from
user.
#include <stdio.h>
void swap(int [],int,int);
void main()
{
int a[50],i,j,n,temp;
printf("Enter n\n");
scanf("%d",&n);
printf("enter array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nReverse Array:\n");
for(i=0,j=n-1;i<=n/2;i++,j--)
swap(a,i,j);
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
void swap(int a[10],int i,int j)
4
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
OUTPUT:-
4 Write a program to Calculate Sum of Maximum of 10 Positive Numbers & Skip Negative Numbers for
sum and also count how many negative numbers and positive numbers entered by user.
#include <stdio.h>
void main()
{
int i,num,p=0,n=0,sum=0;
for(i=1;i<=10;i++)
{
printf("Enter number %d : ",i);
scanf("%d",&num);
if(num<0)
{
n++;
continue;
}
sum=sum+num;
p++;
}
printf("sum of %d positive numbers : %d\n",p,sum);
printf("Total positive numbers: %d\n",p);
printf("Total negative numbers: %d",n);
}
5
OUTPUT:-
5 Write a c program to print factors of a given number and also find sum of all the factors of a given number.
#include <stdio.h>
void main() {
int num, i, sum=0;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Factors of %d are: ", num);
for (i = 1; i <= num; ++i) {
if (num % i == 0) {
printf("%d ", i);
sum=sum+i;
}
}
printf("\nSum of all factors of %d = %d",num,sum);
}
OUTPUT:-
6
6 Write a program to accept n integer numbers and to print an element which has highest occurrence in the
entered numbers.
#include <stdio.h>
void main()
{
int a[20],i,j,k,n,temp,index,count[20],max;
printf("Enter Size of Array:");
scanf("%d",&n);
printf("Enter all Elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
count[i]=0;
for(j=0;j<n;j++)
{
if(a[i]==a[j])
count[i]++;
}
}
max=count[0];
index=0;
for(i=1;i<n;i++)
{
if(max<count[i])
{
max=count[i];
index=i;
}
}
7
printf("Highest occurance of number %d is %d times \n",a[index],max);
}
OUTPUT:-
}
}
OUTPUT:-
8
8 Write a program to calculate nCr using user defined function. nCr = n! / (r! * (n-r)!)
#include<stdio.h>
double fact(int n1); //Fun Declaration
void main()
{
int n,r;
double nCr;
printf("enter Number n: ");
scanf("%d",&n);
printf("enter Number r: ");
scanf("%d",&r);
nCr=fact(n)/(fact(r)*fact(n-r)); //Fun Call
9
9 Write a C Program to Convert Binary Number to Decimal.
#include <stdio.h>
#include <math.h>
// function prototype
int convert(long long);
void main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
}
// function definition
int convert(long long n) {
int dec = 0, i = 0, rem;
while (n!=0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}
OUTPUT:-
10
10 Write a program to convert Decimal number to Hexa Decimal number.
#include <stdio.h>
void main()
{
int remainder, a[20],i=0,n,b;
printf("Enter the number to convert: ");
scanf("%d",&n);
b=n;
while (n > 0)
{
remainder = n % 16;
if (remainder < 10)
{
a[i] = 48 + remainder;
i++;
}
else
{a[i] = 55 + remainder;
i++;
}
n = n / 16;
}
printf("\nHexadecimal of %d is:",b);
// display integer into character
for (i = i-1; i >= 0; i--)
printf("%c",a[i]);
}
OUTPUT:
11
11 Write C Program to Find Neon Number.
(Ex- If the sum of digits of the square of the number is equal to the same number, then the number is called
Neon number.)
Example:- 9
Square of 9 = 92= 81
Sum of the digits of the square = 8+1= 9
So, 9 is a Neon number.
#include <stdio.h>
void main()
{
int num, digit, sum =0;
//Input a number
printf("Enter a number to check for neon number: \n");
scanf("%d",&num);
}
OUTPUT:-
12
12 Write a program print Armstrong Number between 1 to 1000.
#include<stdio.h>
main()
{
int i,temp,rem,sum,count=0;
printf("Armstrong Number 0 to 999 are \n");
for(i=0;i<=999;i++)
{
temp=i;
sum=0;
while(temp>0)
{
rem=temp%10;
sum=sum+(rem*rem*rem);
temp=temp/10;
}
if(i==sum)
{
printf("\n%d",i);
count++;
}
}
printf("%d",count);
}
13
OUTPUT:-
14
else
{
printf("%d ",j);
}
}
printf("\n");
}
}
OUTPUT:-
15
}
OUTPUT:-
for(i=n;i>0;i--)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
if(i==1 || i==n)
for(j=1;j<=i*2-1;j++)
16
{
printf("*");
}
else
{
for(j=1;j<=i*2-1;j++)
{
if(j==1 || j==i*2-1)
printf("*");
else
printf(" ");
}
}
printf("\n");
}
OUTPUT:-
17
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=1;k<=n-i;k++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d",j);
}
for(m=i-1 ;m>=1;m--)
{
printf("%d",m);
}
printf("\n");
}
}
OUTPUT:-
17 Write a C function to swap two integers’ value accepted from user. (with using Call by Reference)
#include <stdio.h>
void swap(int *a, int *b); //prototype of the function
void main()
{
int a, b;
printf("Enter a:");
18
scanf("%d",&a);
printf("Enter b:");
scanf("%d",&b);
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
// printing the value of a and b in main
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
// The values of actual parameters do change in call by reference, a = 10, b = 20
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b);
// Formal parameters, a = 20, b = 10
}
OUTPUT:-
18 Write a program to sort given numbers in ascending order using 1-D array.
#include <stdio.h>
void main (){
int num[20];
int i, j, a, n;
printf("enter number of elements in an array\n");
scanf("%d", &n);
printf("Enter the elements\n");
19
for (i = 0; i < n; ++i)
scanf("%d", &num[i]);
for (i = 0; i < n; ++i){
for (j = i + 1; j < n; ++j){
if (num[i] > num[j]){
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
printf("The numbers in ascending order is:\n");
for (i = 0; i < n; ++i){
printf("%d\n", num[i]);
}
}
OUTPUT:-
19 Write a program which declares a structure named “Faculty” with Fac_id, name (Nested structure F_name,
M_name,L_name), address, salary and date of joining. Print all faculties details in the order of highest
salary.
#include<stdio.h>
struct name
{
char f[20];
char m[20];
20
char l[20];
};
struct faculty
{
int f_id;
struct name nm;
char add[50];
float salary;
int d,m,y;
};
void main()
{
struct faculty fac[10],t;
int i,j,n;
printf("Enter How many faculty data do you want to enter:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter fac id:");
scanf("%d",&fac[i].f_id);
fflush(stdin);
printf("Enter firstname middle name last name:");
scanf("%s %s %s",fac[i].nm.f,fac[i].nm.m,fac[i].nm.l);
fflush(stdin);
printf("Enter address:");
gets(fac[i].add);
printf("Enter salary:");
scanf("%f",&fac[i].salary);
printf("Enter date of joining:");
scanf("%d %d %d",&fac[i].d,&fac[i].m,&fac[i].y);
}
for(i=0;i<n-1;i++)
{
21
for(j=i+1;j<n;j++)
{
if(fac[i].salary<fac[j].salary)
{
t=fac[i];
fac[i]=fac[j];
fac[j]=t;
}
}
}
printf("After Sorting on Code:\n");
for(i=0;i<n;i++)
{
printf("\nFaculty id:%d\nName: %s %s %s\nAddress: %s\nSalary:%f\nDOJ:
%d:%d:%d\n",fac[i].f_id,fac[i].nm.f,fac[i].nm.m,fac[i].nm.l,fac[i].add,fac[i].salary,fac[i].d,fac[i].m,fac[i
].y);
}
}
OUTPUT:-
22
20 Write C Program to Remove all Characters in a String Except Alphabets.
#include <stdio.h>
void main() {
char line[150];
int i,j;
printf("Enter a string: ");
fgets(line, sizeof(line), stdin); // take input
23
}
}
printf("Output String: ");
puts(line);
}
OUTPUT:-
21 Write two function which takes 2 numbers as parameters and one function returns the GCD of the 2
numbers and another function returns LCM of two numbers. Call the function in main ().
#include<stdio.h>
int GCD(int n1,int n2); //Fun Declaration
int LCM(int n1,int n2);
void main()
{
int a,b;
printf("enter a: ");
scanf("%d",&a);
printf("enter b: ");
scanf("%d",&b);
printf("\nGCD= %d\n\n",GCD(a,b)); //Fun Call
printf("LCM= %d",LCM(a,b)); //Fun Call
}
int GCD(int n1,int n2) //Fun Defination
{ int g,i;
if(n1==0)
return n2;
else if(n2==0)
return n1;
else
{
24
for(i=1;i<=n1 && i<=n2;i++)
{
if(n1%i==0 && n2%i==0)
g=i;
}
return g;
}
}
int LCM(int n1,int n2) //Fun Defination
{ int g,i;
if(n1==0)
return n2;
else if(n2==0)
return n1;
else
{
for(i=1;i<=n1 && i<=n2;i++)
{
if(n1%i==0 && n2%i==0)
g=i;
}
return (n1*n2)/g;
}
}
OUTPUT:-
22 Write two different functions for string, one is to join one string to another string without using string
handling function and one is to copy one string to another string without using inbuilt functions.
#include<stdio.h>
25
void concatenate(char s1[20],char s2[20]);
void Copying(char s1[20],char s2[20]);
void main()
{
char s1[20], s2[20];
printf("Enter string 1:\n");
gets(s1);
printf("Enter string 2:\n");
gets(s2);
printf("Concatenation of two strings:\n");
concatenate(s1,s2);
int i=0,j=0;
while(s1[i] != '\0')
{
i++;
}
while(s2[j] != '\0')
{
s1[i]=s2[j];
i++;
j++;
}
s1[i]='\0';
26
printf("Concatenation of s1 and s2 is = %s\n",s1);
}
OUTPUT:-
23 Write a Program to Check whether String is Palindrome or not without using inbuilt functions.
#include <stdio.h>
#include <string.h>
void main()
27
{
char string[25];
int i, length = 0, flag = 0;
24 Write a Program to count vowels, consonants, digits and white space in a given string.
#include <stdio.h>
void main() {
char line[150];
int vowels, consonant, digit, space,i;
28
// initialize all variables to 0
vowels = consonant = digit = space = 0;
29
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", space);
}
OUTPUT:-
25 Write a program to read two matrices from the user and store the multiplication of two matrices in the
resultant matrix. i.e. C=A * B.
#include<stdio.h>
void main()
{
int a[10][10], b[10][10], c[10][10];
int m,n,p,q;
int i,j,k,sum;
printf("Enter the size M x N for 1st Matrix:");
scanf("%d%d",&m,&n);
printf("Enter the size P x Q for 1st Matrix:");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("Enter 1st Matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("Enter 2nd Matrix:\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
30
scanf("%d",&b[i][j]);
}
printf("Resultant Matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
sum=0;
for(k=0;k<n;k++)
{
sum+=a[i][k]*b[k][j];
}
c[i][j]=sum;
printf("%d ",c[i][j]);
}
printf("\n");
}
}
else
printf("\nMatrix Multiplication is not Possible.!");
}
OUTPUT:-
26 Write a function to Read an integer number and find out the sum of all digits until it comes to a single unit.
#include<stdio.h>
int sum(int);
31
void main()
{
int n;
printf("Enter the Number:");
scanf("%d",&n);
printf("Sum of all digits untill single digit=%d",sum(n));
}
int sum(int m)
{
int s;
do
{
s=0;
while(m>0)
{
s=s+m%10;
m=m/10;
}
m=s;
} while(s>9);
return s;
}
OUTPUT:-
27 Write a Program for Searching a product from array of Structure. Structure contain member like product
code, Product Name, Quantity, Price. Find the Product name, quantity & Price according to the Code that
is given by user.
#include<stdio.h>
struct item
{
int code;
char name[20];
32
int qty;
int p;
}it[10];
void main()
{
int n,i,icode,f=0;
printf("How many item:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Item # %d\n",i);
printf("Code, Name , Quantity, Price:");
scanf("%d%s%d%d",&it[i].code,it[i].name,&it[i].qty,&it[i].p);
}
printf("Enter the item code that you want to search:");
scanf("%d",&icode);
for(i=0;i<n;i++)
{
if(it[i].code==icode)
{
f=1;
printf("Your Item is found...\n");
printf("Code=%d\tName=%s\tQyantity=%d\tPrice=%d",it[i].code,it[i].name,it[i].qty,it[i].p);
break;
}
}
if(f==0)
{
printf("Sorry your Item is Not Found...");
}
}
OUTPUT:-
33
28 Write a program in C to sort an array using Pointer and functions both. At least one Argument to the
function should be a pointer.
#include <stdio.h>
// Function to sort the numbers using pointers
void sort(int n, int* ptr);
void main()
{
int n,i,arr[10];
printf("Enter n: ");
scanf("%d",&n);
printf("Enter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
sort(n, arr);
}
void sort(int n, int* ptr)
{
int i, j, t;
34
t = *(ptr + i);
*(ptr + i) = *(ptr + j);
*(ptr + j) = t;
}
}
}
29 Write a program to Swap three numbers in Cyclic Order Using Call by Reference.
Enter a, b and c respectively: 1 2 3
Value before swapping:
a=1
b=2
c=3
Value after swapping:
a=3
b=1
c=2
#include <stdio.h>
void cyclicSwap(int *a, int *b, int *c);
int main() {
int a, b, c;
35
printf("a = %d \nb = %d \nc = %d\n", a, b, c);
return 0;
}
30 Write a program to make one structure student in which members are roll no, name (nested structure first
name, middle name, last name), DOB (nested structure d, m, y). Write a code for delete record of student
with given first name.
#include<stdio.h>
#include<string.h>
struct date
{
int d,m,y;
};
struct name
36
{
char f[20],m[20],l[20];
};
struct student
{
int roll;
struct name n;
struct date dob;
int flag; //for deleting purpose
}s[10];
void main()
{
int i,n,dd,mm,yy,j,ch,rec=0;
char fname[20];
printf("Enter n:");
scanf("%d",&n);
printf("Enter student data:");
for(i=0;i<n;i++)
{
printf("\nEnter Student %d roll:",i+1);
scanf("%d",&s[i].roll);
fflush(stdin);
printf("Enter first name:");
scanf("%s",s[i].n.f);
printf("Enter middle name:");
scanf("%s",s[i].n.m);
printf("Enter last name:");
scanf("%s",s[i].n.l);
printf("Enter DOB:");
scanf("%d %d %d",&s[i].dob.d,&s[i].dob.m,&s[i].dob.y);
s[i].flag=1; //all member's flag value 1
}
37
printf("\nRoll no=%d\nStudent Name=%s %s
%s\nDOB=%d:%d:%d\n\n",s[i].roll,s[i].n.f,s[i].n.m,s[i].n.l,s[i].dob.d,s[i].dob.m,s[i].dob.y);
}
}
OUTPUT:-
38