0% found this document useful (0 votes)
2 views

'C' Slip Program's - Copy

The document contains a series of C programming exercises with solutions, covering various topics such as macros, pointers, file handling, string manipulation, and dynamic memory allocation. Each exercise includes code snippets, expected outputs, and explanations of the functionality. The exercises are designed to enhance understanding of advanced C programming concepts.

Uploaded by

Sahil Tarle
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

'C' Slip Program's - Copy

The document contains a series of C programming exercises with solutions, covering various topics such as macros, pointers, file handling, string manipulation, and dynamic memory allocation. Each exercise includes code snippets, expected outputs, and explanations of the functionality. The exercises are designed to enhance understanding of advanced C programming concepts.

Uploaded by

Sahil Tarle
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Advanced ‘C’ programming slip programs

Q.1 Write a C program to find maximun of two numbers using macro


Ans:
#include<stdio.h>
#define max(x,y) (x>y?x:y)
int main()
{
int a=10,b=20,large;
large=max(a,b);
printf("\nThe maximum is:%d",large);
return 0;
}
Output : The maximum is:20

Q.2 Write a C program to compute sum & average of all elements in an array using pointer.
Ans:
#include<stdio.h>
int main()
{
int arr[5],*ptr;
int i,n,sum=0;
float avg;
printf("\nEnter how many element you want to enter:");
scanf("%d",&n);
printf("\nEnter array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
ptr=&arr;
for(i=0;i<n;i++)
{
sum=sum+*ptr;
ptr++;
}
avg=sum/n;
printf("\nThe sum of array is:%d",sum);
printf("\nThe average is:%f",avg);
return 0;
}
Output : Enter how many element you want to enter:5
Enter array elements:10 20 30 40 50
The sum of array is:150
The average is:30.000000

Q.3 Write a C program to display the command line arguments in reverse order
Ans:
#include<stdio.h>
int main(int argc,char *argv[])
{
int i;
printf("\nCommand line arguments in reverse order is:");
for(i=argc-1;i>=0;i--)
{
printf("%s\t",argv[i]);
}
return 0;
}
Output :
./a.out 1 2 3 4 5
Command line arguments in reverse order is:5 4 3 2 1
./a.out

Q.4 Write a C program to display the contents of a file a.txt on the screen.

Ans :
#include<stdio.h>
int main()
{
FILE *fp;
char ch;
fp=fopen("a.txt","r");
if(fp==NULL)
{
printf("\n Error in file opening");
}
printf("\nThe data in the file \n");
while(!feof(fp))
{
ch=getc(fp);
printf("%c",ch);
}
fclose(fp);
return 0;
}
Output :
The data in the file
hello world....! are you mad??

Q.5 Write a c program to calculate length of string using standard library function.

Ans :
#include <stdio.h>
#include <string.h>
int main()
{
char str[]="Hello World!!";
printf("\nLength of the string is:%d",strlen(str));
return 0;
}
Output :
Length of the string is:13

Q.6 Write a c program to allocate memory dynamically for n integers. Accept the elements
&calculate their sum & average.

Ans :
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i,*ptr,sum=0;
float avg;
printf("\nEnter the number of integers:");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
printf("\nEnter the integers:");
for(i=0;i<n;i++)
{
scanf("%d",&ptr[i]);
}
for(i=0;i<n;i++)
{
sum=sum+ptr[i];
}
avg=sum/n;
printf("\nThe sum of integers is:%d",sum);
printf("\nThe average is:%f",avg);
return 0;
}
Output :
Enter the number of integers:5
Enter the integers:10 20 30 40 50
The sum of integers is:150
The average is:30.000000

Q.7 Write a c program to calculate sum of two numbers.Pass the numbers as command line
arguments.

Ans :
#include<stdio.h>
int main(int argc,char *argv[])
{
int a,b,sum;
if(argc!=3)
{
printf("\nInvalid Inputs");
}
a=atoi(argv[1]);
b=atoi(argv[2]);
sum=a+b;
printf("\nThe sum is:%d",sum);
return 0;
}
Output :
./a.out 12 8
The sum is:20

Q.8 Write a C program to perform following operations on two strings using standard
library function. i)copy ii) compare

#include<stdio.h>
#include<string.h>
int main()
{
char s1[50],s2[50],s3[50];
printf("\nEnter string 1:");
scanf("%s",s1);
printf("\nEnter string 2:");
scanf("%s",s2);
strcpy(s3,s1);
printf("\nThe copied string in s3 is : %s",s3);
if(strcmp(s1,s2)==0)
{
printf("\nStrings are equal");
}
else
{
printf("\nStrings are not equal");
}
return 0;
}
Output :
Enter string 1:hello
Enter string 2:world
The copied string in s3 is : hello
Strings are not equal

Q.9 Write a c program to interchange two numbers using pointer

Ans :
#include<stdio.h>
int main()
{
int x=10,y=20,*a,*b,temp;
a=&x;
b=&y;
temp=*a;
*a=*b;
*b=temp;
printf("\nAfter swapping x=%d , y=%d",x,y);
return 0;
}
Output :
After swapping x=20 , y=10

Q.10 Write a C program to copy content of one file to anothor file.

Ans :
#include<stdio.h>
int main()
{
FILE *fp1,*fp2;
char ch;
fp1=fopen("a.txt","r");
fp2=fopen("b.txt","w");
if((fp1==NULL)||(fp2==NULL))
{
printf("\nError in opening files");
}
while(!feof(fp1))
{
ch=fgetc(fp1);
fputc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
printf("\nData transfer succesfully");
return 0;
}
Output :
Data transfer successfully

Q.11 //Write a function in c to compare two strings using standard library function.

Ans :
#include<stdio.h>
#include<string.h>
int main()
{
char s1[50],s2[50];
printf("\nEnter string 1 & string 2:");
scanf("%s%s",s1,s2);
void scompare(char str1[50],char str2[50]);
scompare(s1,s2);
return 0;
}
void scompare(char str1[50],char str2[50])
{
if(strcmp(str1,str2)==0)
{
printf("\nstrings are equal");
}
else
{
printf("\nStrings are not equal");
}
}
Output :
Enter string 1 & string 2:hello bhai
Strings are not equal
Q.12 Write a User defined function to calculate length of a string .use this function
in main.

Ans :
#include<stdio.h>
int main()
{
char str1[50];
printf("\nEnter a string:");
scanf("%s",str1);
void slength(char str2[50]);
slength(str1);
return 0;
}
void slength(char str2[50])
{
int i,count=0;
for(i=0;str2[i]!='\0';i++)
{
count++;
}
printf("\nThe length of string is:%d",count);
}
Output :
Enter a string:Pravin
The length of string is:6

Q.13 //Write a C program to concatenate two strings using standard library function.

Ans ;
#include<stdio.h>
#include<string.h>
int main()
{
char str1[50],str2[50];
printf("\nEnter string 1:");
gets(str1);
printf("\nEnter string 2:");
gets(str2);
strcat(str1,str2);
printf("\nThe concatenated string is:%s",str1);
return 0;
}
Output :
Enter string 1:Hello world
Enter string 2: How are you?
The concatenated string is:Hello world How are you?

Q.14 Write a C program to accept three integers as command line arguments and find
the maximum of three?

Ans :
#include<stdio.h>
int main(int argc,char *argv[])
{
int a,b,c;
if(argc!=4)
{
printf("\nInvalid number of arguments");
}
a=atoi(argv[1]);
b=atoi(argv[2]);
c=atoi(argv[3]);
if(a>b)
{
if(a>c)
{
printf("\n a is maximum");
}
else
{
printf("\n c is maximum");
}
}
else
{
if(b>c)
{
printf("\n b is maximum");
}
else
{
printf("\n c is maximum");
}

}
return 0;
}
Output :
./a.out 5 8 6

b is maximum

Q.15 Write a c program to find area of circle by using PI as macro

Ans :
#include<stdio.h>
#define PI 3.14
int main()
{
float r=3.0,area;
area=PI*r*r;
printf("\nThe area of circle is:%f",area);
return 0;
}
Output :
The area of circle is:28.260000

Q.16 Write a C program to perform following operations on strings using standard


library function. i)copy one string to another ii)convert a string to uppercase.

Ans :
#include<stdio.h>
#include<string.h>
int main()
{
char s1[50],s2[50];
printf("\nenter string 1:");
gets(s1);
strcpy(s2,s1);
printf("\nThe copied string is:%s",s2);
strupr(s1);
printf("\nThe string in uppercase is:%s",s1);
return 0;
}
Output :
enter string 1:hello
The copied string is:hello
The string in uppercase is:HELLO
Q.17 Write a c program to calculate factorial of a given number.

Ans :
#include<stdio.h>
int main()
{
int i,n,fact=1;
printf("\nEnter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("\nThe factorial is:%d",fact);
return 0;
}
Output :
Enter a number:5
The factorial is:120

Q.18 Write a c program to create structure


student(roll_no.,name,percentage)initialize value for one student and dislay the
same.

Ans :
#include<stdio.h>
struct student
{
int roll_no;
char name[20];
float percentage;
};
int main()
{
struct student s1={18,"Pravin",54.50};
printf("%d\t%s\t%f",s1.roll_no,s1.name,s1.percentage);
return 0;
}
Output :
18 Pravin 54.500000

Q.19 Write a c program to display the content of an array in the reverse order using
pointer.
Ans :
#include<stdio.h>
int main()
{
int n,i,arr[20],*ptr;
printf("\nEnter the no. of elements:");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
ptr=&arr[n-1];
printf("\nThe array elements in reverse order is:\n");
for(i=n;i>0;i--)
{
printf("%d\t",*ptr--);
}
return 0;
}
Output :
Enter the no. of elements:5
Enter the elements:5 4 3 2 1
The array elements in reverse order is:
1 2 3 4 5

Q.20 Write a c program to calculate the SQUARE of given number using MICRO.

Ans :
#include<stdio.h>
#define sqr(x) (x*x)
int main()
{
int a=10,square;
square=sqr(a);
printf("\nSquare using micro is:%d",square);
return 0;

}
Output :
Square using micro is:100
Q.21 Write a MENU driven program to perform the following operation on string using
standard library function.i) length ii)copy.

Ans :
#include<stdio.h>
#include<string.h>
int main()
{
char s1[20],s2[20];
int ch;
do
{
printf("\nMENU");
printf("\n 1.Length of string");
printf("\n 2.copy string");
printf("\n 3.Exit");
printf("\nEnter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter string 1:");
scanf("%s",s1);
printf("\nThe length of string is:%d",strlen(s1));
break;
case 2:printf("\nEnter string 1:");
scanf("%s",s1);
strcpy(s2,s1);
printf("\nThe coppied string is:%s",s2);
break;
default:printf("\nInvalid Input");
}
}while(ch!=2);
return 0;
}
Output :
MENU
1.Length of string
2.copy string
3.Exit
Enter your choice :1
Enter string 1:hello
The length of string is:5
MENU
1.Length of string
2.copy string
3.Exit
Enter your choice :2
Enter string 1:hello
The coppied string is:hello

Q.22 Write a C program to accept a string and convert it in to lowercase using standard
library function.

Ans :
#include<stdio.h>
#include<string.h>
int main()
{
char str[50];
printf("\nEnter a string:");
gets(str);
strlwr(str);
printf("\nString in lower case is : %s",str);
return 0;
}
Output ;
Enter a string:HELLO WORLD
String in lower case is : hello world

Q.23 Write a MENU driven program to perform the following operation on string using
standard library function. i) convert string to lowercase ii)compare rwo strings.

Ans :
#include<stdio.h>
#include<string.h>
int main()
{
char s1[20],s2[20];
int ch;
do
{
printf("\nMENU");
printf("\n 1.convert string to lowercase");
printf("\n 2.comapre two strings");
printf("\n 3.Exit");
printf("\nEnter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter string 1:");
scanf("%s",s1);
printf("\nString in lowercase :%s",strlwr(s1));
break;
case 2:printf("\nEnter string 1 & string 2:");
scanf("%s%s",s1,s2);
if(strcmp(s1,s2)==0)
{
printf("\nStrings are equal");
}
else
{
printf("\nStrings are not equal");
}
break;
default:printf("\nInvalid Input");
}
}while(ch!=2);
return 0;
}
Output :
MENU
1.convert string to lowercase
2.comapre two strings
3.Exit
Enter your choice :1
Enter string 1:HELLO
String in lowercase :hello
MENU
1.convert string to lowercase
2.comapre two strings
3.Exit
Enter your choice :2
Enter string 1 & string 2:hello hello
Strings are equal

Q.24 Write a c program to accept and display information of 5


customers(customer_no,customer_name,customer-phone).

Ans :
#include<stdio.h>
struct customer
{
int customer_no;
char customer_name[20];
long customer_phone;
};
int main()
{
struct customer c[5];
int i;
for(i=0;i<5;i++)
{
printf("\nEnter the information of customer no,name & phone:");
scanf("%d%s%d",&c[i].customer_no,c[i].customer_name,&c[i].customer_phone)
;
}
printf("\n###Information of customers###\n");
for(i=0;i<5;i++)
{
printf("%d\t%s\t%d\n",c[i].customer_no,c[i].customer_name,c[i].customer_p
hone);
}
return 0;
}
Output :
Enter the information of customer no,name & phone:1 Pravin 9898989898
Enter the information of customer no,name & phone:2 San's 6565656565
Enter the information of customer no,name & phone:3 Sankya 3232323232
Enter the information of customer no,name & phone:4 pratya 5445454454
Enter the information of customer no,name & phone:5 shubhya 2121212121
###Information of customers###
1 Pravin 9898989898
2 San's 6565656565
3 Sankya 3232323232
4 pratya 5445454454
5 shubhya 2121212121

Q.25 Write a c program to accept and display information (emp_no,emp_name,emp_salary)


of 5 employee using structure.

Ans :
#include<stdio.h>
struct employee
{
int emp_no;
char emp_name[20];
long emp_salary;
};
int main()
{
struct employee e[5];
int i;
for(i=0;i<5;i++)
{
printf("\nEnter the information of employee no,name & salary:");
scanf("%d%s%d",&e[i].emp_no,e[i].emp_name,&e[i].emp_salary);
}
printf("\n###Information of employees###\n");
for(i=0;i<5;i++)
{
printf("%d\t%s\t%d\n",e[i].emp_no,e[i].emp_name,e[i].emp_salary);
}
return 0;
}
Output :
Enter the information of employee no,name & salary:1 narendra 25000
Enter the information of employee no,name & salary:2 pappu 20000
Enter the information of employee no,name & salary:3 elvish 19000
Enter the information of employee no,name & salary:4 dhruv 20000
Enter the information of employee no,name & salary:5 rahul 25000
###Information of employees###
1 narendra 25000
2 pappu 20000
3 elvish 19000
4 dhruv 20000
5 rahul 25000

Q.26 Write a C Program to display the array elements using pointer.

Ans :
#include<stdio.h>
int main()
{
int arr[5],*ptr,i;
printf("\nEnter array elements:");
for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
ptr=arr;
printf("\nArray elements using pointer is:\n");
for(i=0;i<5;i++)
{
printf("%d\t",*ptr++);
}
return 0;
}
Output :
Enter array elements:1 2 3 4 5
Array elements using pointer is:
1 2 3 4 5

Q.27 Write a C program to accept the details of n items(id,name,price)using structure


and display the details.

Ans :
#include<stdio.h>
struct items
{
int id;
char name[20];
float price;
};
int main()
{
int n;
printf("\nEnter number of items:");
scanf("%d",&n);
struct items item[n];
int i;
for(i=0;i<n;i++)
{
printf("\nEnter item details id,name & price :");
scanf("%d%s%f",&item[i].id,item[i].name,&item[i].price);
}
printf("\n###Items Information###\n");
for(i=0;i<n;i++)
{
printf("%d\t%s\t%f\n",item[i].id,item[i].name,item[i].price);
}
return 0;
}
Output :
Enter number of items:3
Enter item details id,name & price :1 pen 10
Enter item details id,name & price :2 notebook 45.50
Enter item details id,name & price :3 book 120.50
###Items Information###
1 pen 10.000000
2 notebook 45.500000
3 book 120.500000

Q.28 //Write a C program to convert a string to upercase using standard library


function.

#include<stdio.h>
#include<string.h>
int main()
{
char str[50];
printf("\nEnter a string:");
gets(str);
strupr(str);
printf("\nString in upper case is : %s",str);
return 0;
}
Output :
Enter a string:hello world
String in upper case is : HELLO WORLD

Q.29 Write a C program to accept and display


information(player_name,team_name,average,highest_score)of 5 cricketers using
structure.

Ans :
#include<stdio.h>
struct cricketer
{
char name[20];
char Team_name[20];
float avg;
int High_score;
};
int main()
{
struct cricketer c[5];
int i;
for(i=0;i<5;i++)
{
printf("\nEnter the information of cricketer player name,team name,average
,and highest score:");
scanf("%s%s%f%d",c[i].name,c[i].Team_name,&c[i].avg,&c[i].High_score);
}
printf("\n###Information of cricketers###\n");
for(i=0;i<5;i++)
{
printf("%s\t%s\t%f\t%d\n",c[i].name,c[i].Team_name,c[i].avg,c[i].High_sco
re);
}
return 0;
}
Output :
Enter the information of cricketer player name,team name,average ,and highest
score:Rohit MI 95.50 264
Enter the information of cricketer player name,team name,average ,and highest
score:Virat RCB 97.52 195
Enter the information of cricketer player name,team name,average ,and highest
score:Dhoni CSK 45.12 97
Enter the information of cricketer player name,team name,average ,and highest
score:Ruturaj CSK 94.50 177
Enter the information of cricketer player name,team name,average ,and highest
score:Hardik MI 65.50 155
###Information of cricketers###
Rohit MI 95.500000 264
Virat RCB 97.519997 195
Dhoni CSK 45.119999 97
Ruturaj CSK 94.500000 177
Hardik MI 65.500000 155

Q.30 Write a C program to accept and display information of one person (either PAN
or AADHAR number)using union.

Ans :
#include<stdio.h>
union person
{
char P_aadhar[12];
};
int main()
{
union person P;
printf("\nEnter aadhar number : ");
scanf("%s",&P.P_aadhar);
printf("\nAadhar number of person is:%s",P.P_aadhar);
return 0;
}
Output :
Enter aadhar number : 123456789123
Aadhar number of person is:123456789123

Q.31 Write a C program to accept two strings & check whether both the strings are equal
or not.

Ans :
#include<stdio.h>
#include<string.h>
int main()
{
char str1[20],str2[20];
printf("\nEnter string 1 :");
scanf("%s",str1);
printf("\nEnter string 2 :");
scanf("%s",str2);
if(strcmp(str1,str2)==0)
{
printf("\nBoth strings are equal");
}
else
{
printf("\nBoth strings are not equal");
}
return 0;
}
Output :
Enter string 1 :hello
Enter string 2 :hello
Both strings are equal

You might also like