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

Advanced C - Programs With Solutions

Uploaded by

Samarth Bhagat
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)
934 views

Advanced C - Programs With Solutions

Uploaded by

Samarth Bhagat
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/ 7

VIDYA PRATISHTHAN’S COMMERCE & SCIENCE COLLEGE INDAPUR.

C Programs & Output

Q1. Write a C program to compute sum and average of all elements in an array using pointer.

Program OUTPUT
#include<stdio.h>
void main() Enter Array : 3 4 5 6 7
Sum = 25
{
Average = 5
int a[5],*p,i,sum=0,avg;
p = &a;
printf("Enter Array : ");
for(i=0;i<5;i++)
{
scanf("%d",(p+i));
sum = sum + *(p+i);
}
avg = sum / i;
printf("Sum = %d" ,sum);
printf("\nAverage = %d" ,avg);
}

PROBLEM SOLVING USING COMPUTER & C PROGRAMMING (FYBCS) BY PROF. SARFARAZ SHAIKH 1
VIDYA PRATISHTHAN’S COMMERCE & SCIENCE COLLEGE INDAPUR.

Q2. Write a menu driven program to perform the following operations on strings using standard
library functions:
1. Convert string to uppercase
2. Copy one string to another
3. Compare two strings
4. Concatenate two strings

Program OUTPUT
#include<stdio.h>
#include<string.h> 1.Convert Strin to Uppercase
void main() 2.Copy One String to another
{
char s1[10],s2[10]; 3.Compare Two Strings
int ch; 4.Concatenate Two Strings
printf("\n1.Convert Strin to Uppercase ");
printf("\n2.Copy One String to another "); Enter Choice : 4
printf("\n3.Compare Two Strings "); Enter First String : vp
printf("\n4.Concatenate Two Strings ");
printf("\nEnter Choice : "); Enter Second String : indapur
scanf("%d",&ch); Concatinated String = vpindapur
printf("Enter First String : ");
scanf("%s",s1);
printf("Enter Second String : ");
scanf("%s",s2);
switch(ch)
{
case 1: {printf("String in Uppercase : %s",strupr(s1));
} break;
case 2: {
strcpy(s1,s2);
printf("Copied String = %s",s1);
} break;
case 3: {
if((strcmp(s1,s2)==0))
printf("Both Strings are Equal");
else
printf("Both Strings are Not Equal");
} break;
case 4: {
strcat(s1,s2);
printf("Concatinated String = %s",s1);
} break;
}
}

PROBLEM SOLVING USING COMPUTER & C PROGRAMMING (FYBCS) BY PROF. SARFARAZ SHAIKH 2
VIDYA PRATISHTHAN’S COMMERCE & SCIENCE COLLEGE INDAPUR.

Q3. Write a program to calculate length of string using standard library function.

Program OUTPUT

#include<stdio.h>
Enter String : vpcsc
#include<string.h>
void main()
Length of String = 5
{
char s[10];
printf("Enter String : ");
gets(s);
printf("\nLength of String = %d" ,strlen(s));
getch();
}

Q4. Write a program to create student structure having fields roll no, name. Accept details of one
student and write a function to display the details.

Program OUTPUT
#include<stdio.h>
struct stud Enter Student Details : 11 UMESH
{
int rno; 11 UMESH
char name[10];
};
void display(struct stud s1)
{
printf(" %d %s",s1.rno,s1.name) ;
}
void main()
{
struct stud s;
printf("Enter Student Details : ");
scanf("%d%s",&s.rno,s.name);
display(s);
}

PROBLEM SOLVING USING COMPUTER & C PROGRAMMING (FYBCS) BY PROF. SARFARAZ SHAIKH 3
VIDYA PRATISHTHAN’S COMMERCE & SCIENCE COLLEGE INDAPUR.

Q5. Write a program to interchange two numbers using pointers.

Program OUTPUT
#include<stdio.h>
void main() Enter A : 40
{ Enter B : 50
int *a,*b,*t;
printf("Enter A : "); After Interchange :
scanf("%d",a);
printf("Enter B : "); A = 50
scanf("%d",b); B = 40
*t=*a;
*a=*b;
*b=*t;
printf("After Interchange : \n");
printf("\nA = %d",*a);
printf("\nB = %d",*b);
}

Q6. Write a program to perform the following operations on two strings using standard library
functions: a. Copy b. Compare.

Program OUTPUT
#include<stdio.h>
#include<string.h> Enter First String : vpcsc
void main()
Enter Second String : vpp
{
char s1[10],s2[10]; Both Strings are Not Equal
printf("Enter First String : ");
Enter String : indapur
gets(s1);
printf("Enter Second String : "); Copied String = indapur
gets(s2);
if((strcmp(s1,s2)==0))
printf("Both Strings are Equal");
else
printf("Both Strings are Not Equal");
printf("\nEnter String : ");
gets(s1);
strcpy(s2,s1);
printf("Copied String = %s",s2);
}

PROBLEM SOLVING USING COMPUTER & C PROGRAMMING (FYBCS) BY PROF. SARFARAZ SHAIKH 4
VIDYA PRATISHTHAN’S COMMERCE & SCIENCE COLLEGE INDAPUR.

Q7. Write a C program to compare two strings using standard library function.

Program OUTPUT
#include<stdio.h>
#include<string.h>
Enter First String : vpcsc
void main()
{ Enter Second String : vpcsc
char s1[10],s2[10];
printf("Enter First String : ");
gets(s1); Both Strings are Equal
printf("Enter Second String : ");
gets(s2);
if((strcmp(s1,s2)==0))
printf("\nBoth Strings are Equal");
else
printf("\nBoth Strings are Not Equal");
}

Q8. Write a C program to count the number of vowels and consonants in a string.

Program OUTPUT
#include<stdio.h>
#include<string.h>
Enter String : indapur
void main()
{
char s[10];
Vowels = 3
int i=0,v=0,c=0;
printf("Enter String : "); Consonants = 4
gets(s);
for(i=0;i<strlen(s);i++)
{
if(s[i]=='a' || s[i]=='e' || s[i]=='i' ||
s[i]=='o' || s[i]=='u')
v++;
else
c++;
}
printf("\nVowels = %d" ,v);
printf("\nConsonants = %d" ,c);
}

PROBLEM SOLVING USING COMPUTER & C PROGRAMMING (FYBCS) BY PROF. SARFARAZ SHAIKH 5
VIDYA PRATISHTHAN’S COMMERCE & SCIENCE COLLEGE INDAPUR.

Q9. Write a C program to concatenate two strings using standard library function.

Program OUTPUT
#include<stdio.h>
#include<string.h>
Enter First String : vp
void main()
{ Enter Second String : Indapur
char s1[10],s2[10];
Concatinated String = vpIndapur
printf("Enter First String : ");
gets(s1);
printf("Enter Second String : ");
gets(s2);
strcat(s1,s2);
printf("\nConcatinated String = %s" ,s1);
}

Q10. Write a program to accept details of n employees (id, name, salary).Display all the details.
Also, search for an employee by name.

Program OUTPUT

#include<stdio.h>
#include<string.h> Enter Emp Details :
struct emp 11 ABC 4500
{ int id,salary;
char name[10]; 22 RTY 8769
}; 33 SAR 6000
void main()
{ struct emp e[5]; 44 NML 7000
char nm[10]; 55 DFG 8000
int i,flag=0;
printf("Enter Emp Details : ");
for(i=0;i<5;i++) Enter Emp Name for Search: SAR
scanf("%d%s%d",&e[i].id,e[i].name,&e[i].salary);
printf("\nEnter Emp Name for Search:");
scanf("%s",nm); Given Employee is Present
for(i=0;i<5;i++)
{
if((strcmp(e[i].name,nm)==0))
{ flag=1; break;
}
}
if(flag==1)
printf("\n Given Employee is Present");
else
printf("\n Given Employee is Not Present");
}

PROBLEM SOLVING USING COMPUTER & C PROGRAMMING (FYBCS) BY PROF. SARFARAZ SHAIKH 6
VIDYA PRATISHTHAN’S COMMERCE & SCIENCE COLLEGE INDAPUR.

Q11. Write a C program to create a structure named book (book_name, author_name and price)
and display all book details having price >200 in a proper format.

Program OUTPUT

#include<stdio.h>
#include<conio.h> Enter Book Details :
struct book CPP MAX 300
{
char bname[10],author[10]; JAVA TECH 150
int price; PHP VIS 450
};
void main() JS TECH 120
{ NET VIS 100
struct book b[5];
int i;
printf("Enter Book Details : "); Books having Price >200 :
for(i=0;i<5;i++)
scanf("%s%s%d",b[i].bname,b[i].author,&b[i].price); CPP MAX 300
printf("\nBooks having Price >200 :"); PHP VIS 450
for(i=0;i<5;i++)
{
if(b[i].price>=200)
printf("\n %s %s %d ",b[i].bname,b[i].author,b[i].price);
}
}

Q12. Write a program to accept a string and an index from user and displays the character at that
specific index.

Program OUTPUT

#include<stdio.h>
#include<string.h> Enter String : vpcsc
void main() Enter Index : 2
{
char s[10];
int i; Character at given Index is = c
printf("Enter String : ");
gets(s);
printf("Enter Index : ");
scanf("%d",&i);
printf("\nCharacter at given Index is = %c" ,s[i]);
}

PROBLEM SOLVING USING COMPUTER & C PROGRAMMING (FYBCS) BY PROF. SARFARAZ SHAIKH 7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy