Slip Program's
Slip Program's
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
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
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
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
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
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.18