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

SPA SEM II 60 Program Solutions

The document contains 26 C programming questions and their solutions. Each question provides a short code snippet to demonstrate how to write a program to perform a specific task, such as calculating simple interest, employee salary, area and perimeter of a rectangle, greatest of three numbers, prime numbers, Armstrong numbers, and other basic programming challenges. The full code solutions are provided for students to practice basic programming concepts.

Uploaded by

Shabana Bano
Copyright
© Attribution Non-Commercial (BY-NC)
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)
87 views

SPA SEM II 60 Program Solutions

The document contains 26 C programming questions and their solutions. Each question provides a short code snippet to demonstrate how to write a program to perform a specific task, such as calculating simple interest, employee salary, area and perimeter of a rectangle, greatest of three numbers, prime numbers, Armstrong numbers, and other basic programming challenges. The full code solutions are provided for students to practice basic programming concepts.

Uploaded by

Shabana Bano
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 43

SPA Program List & Solution Solution of C Programs for FE SEM II (SPA)

1. Write a program to calculate the simple interest taking principal, rate of interest and number of years as inputs from user. #include<stdio.h> #include<conio.h> void main () { float rateOfInterest, years, principal, simpleInterest; clrscr(); printf("Enter the principal amount, rate of interest and no. of years:"); scanf("%f%f%f", &principal, &rateOfInterest, &years); simpleInterest = principal * rateOfInterest*years / 100; printf("The simple interest is %f", simpleInterest) ; getch(); }

2.

Write a program to accept basic salary from the keyboard. Calculate the gross salary that includes basic salary, 50% DA and 40% HRA. #include<stdio.h> #include<conio.h> void main () { float basic,hra,da,gross; clrscr(); printf("Enter the basic salary:"); scanf("%f", &basic); hra=40*basic/100; da = 50*basic/100; gross=basic+da+hra; printf("The total salary is %f",gross); getch(); }

Prof. Ahlam Ansari

Page

SPA Program List & Solution


3. Write a program to accept the length and breadth of a rectangle from the user. Calculate and display the area and perimeter. #include<stdio.h> #include<conio.h> void main () { float length, breadth, area, perimeter; clrscr(); printf("Enter the length and breadth of the rectangle:"); scanf("%f%f",&length,&breadth); area= length * breadth; perimeter= 2* (length + breadth); printf("The area of rectangle is= %f and its perimeter is = %f", area, perimeter); getch(); }

4.

Write the program to accept one it type data and one float type data. Multiply the two numbers and display the result. #include<stdio.h> #include<conio.h> void main () { int n1; float n2, result; clrscr(); printf("Enter one integer and one float type number each:"); scanf("%d %f",&n1,&n2); result=n1*n2; printf("The product is :%f",result); getch(); }

Prof. Ahlam Ansari

Page

SPA Program List & Solution


5. Write a program to accept three numbers from user and display the greatest of three using the conditional operator. #include<stdio.h> #include<conio.h> void main () { int n1, n2, n3, greater; clrscr(); printf("Enter three numbers:"); scanf("%d %d %d", &n1,&n2,&n3); greater=(n1>n2)?((n1>n3)?n1:n3):((n2>n3)?n2:n3); printf("The largest number is :%d",greater); getch(); }

6.

Write a program to accept three numbers and find their average. #include<stdio.h> #include<conio.h> void main () { int a, b, c, sum; float avg; clrscr(); printf("Enter three numbers:"); scanf("%d %d %d",&a,&b,&c); sum= a+b+c; avg=sum/3.0; printf("The average is equal to:%f",avg); getch(); }

7.

Write a program to swap two integers. #include<stdio.h> #include<conio.h>

Prof. Ahlam Ansari

Page

SPA Program List & Solution


void main () { int a, b, temp; clrscr(); printf("Enter two numbers:"); scanf("%d %d",&a,&b); temp=a; a=b; b=temp; printf("After swapping the values are a=%d and b=%d",a,b); getch(); } 8. Write a program to accept a two digit number and display it in reversed form. #include<stdio.h> #include<conio.h> void main () { int a, digit1, digit2; clrscr(); printf("Enter a number:"); scanf("%d",&a); digit1=a%10; a=a/10; digit2=a%10; printf("Reversed no is %d%d",digit1,digit2); getch(); } 9. Write a program to check if the entered number is even or odd. #include<stdio.h> #include<conio.h> void main () { int a, rem; clrscr();

Prof. Ahlam Ansari

Page

SPA Program List & Solution


printf("Enter a number:"); scanf("%d",&a); rem=a%2; (rem==0)?printf("Even number"):printf("Odd number"); getch(); } 10. Write a program to display the first n natural numbers, where the value of n is taken from user. #include<stdio.h> #include<conio.h> void main() { int i,n; clrscr(); printf("Enter a number: "); scanf("%d",&n); for(i=1;i<=n;i++) { printf("%d\n",i); } getch(); } 11. Write a program to find the factorial of a number. #include<stdio.h> #include<conio.h> void main() { int i,n,fact; clrscr(); printf("Enter a number: "); scanf("%d",&n); for(i=1,fact=1;i<=n;i++) { fact=fact * i;

Prof. Ahlam Ansari

Page

SPA Program List & Solution


} printf("Factorial of this number is:%d\n",fact); getch(); } 12. Write a program to display the multiplication table of a user entered number. The table must be upto 10. #include<stdio.h> #include<conio.h> void main() { int i,n; clrscr(); printf("Enter a number: "); scanf("%d",&n); for(i=1;i<=10;i++) { printf("%d X %d = %d\n",n,i,(n*i)); } getch(); } 13. Write a program to calculate the value of the following series.

#include<stdio.h> #include<conio.h> void main() { int i,n; float sum=0.0; clrscr(); printf("Enter a number: "); scanf("%d",&n); for(i=1;i<=n;i++) {

Prof. Ahlam Ansari

Page

SPA Program List & Solution


sum=sum+1.0/i; } printf("The value of the series is:%f",sum); getch(); }

14. Write a program to calculate the sine of an angle using the following series for x as the angle in radians.

#include<stdio.h> #include<math.h> #include<conio.h> void main() { int i, fact=1,sign=-1; float x,numerator,sum,term; clrscr(); printf("Enter an angle in degrees: "); scanf("%f",&x); x=x*3.14/180; term=x; sum=term; for(i=3;term>=0.0000001;i=i+2) { fact=fact*i*(i-1); numerator=pow(x,i); term=numerator/fact; sum=sum + sign * term; sign=sign*-1; } printf("The value of the series is:%f",sum); getch();

Prof. Ahlam Ansari

Page

SPA Program List & Solution


}
15. Write a program to display the following for the users specified number of lines. * ** *** **** ***** | n lines #include<stdio.h> #include<conio.h> void main() { int i,j,n; clrscr(); printf("Enter the number of lines:"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } getch(); }

16. Write a program to display the following for the user specified number of lines. * ** *** **** ***** ****** ******* ******** | n lines #include<stdio.h> #include<conio.h> void main() { int i,j,n; clrscr(); printf("Enter the number of lines:"); scanf("%d",&n);

Prof. Ahlam Ansari

Page

SPA Program List & Solution


for(i=1;i<=n;i++) { for(j=1;j<=n-i;j++) { printf(" "); } for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } getch(); } 17. Write a program to display the following asking the user for the number of lines 1 12 123 1234 12345 123456 #include<stdio.h> #include<conio.h> void main() { int i,j,n; clrscr(); printf("Enter the number of lines:"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { printf("%d",j); } printf("\n"); } getch(); }

Prof. Ahlam Ansari

Page

SPA Program List & Solution

18. Write a program to display the following asking the user for the number of lines. A ABA ABCBA ABCDCBA ABCDEDCBA ABCDEFEDCBA #include<stdio.h> #include<conio.h> void main() { int i,j,n; clrscr(); printf("Enter the number of lines:"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=n-i;j++) { printf(" "); } for(j=1;j<=i;j++) { printf("%c",(char)(j+64)); } for(j=i-1;j>=1;j--) { printf("%c",(char)(j+64)); } printf("\n"); } getch(); } 19. Write a program to calculate the value of n-1)!. #include<stdio.h> #include<conio.h>

Prof. Ahlam Ansari

Page

10

SPA Program List & Solution


void main() { int i,j,sum=0,n,fact; clrscr(); printf("Enter a number:"); scanf("%d",&n); for(i=1;i<=n;i++) { fact=1; for(j=1;j<=(2*i-1);j++) { fact=fact*j; } sum=sum+fact; } printf("The value of this series= %d",sum); getch(); }

20. Write a program to display first 10 numbers divisible by 5 and 8. #include<stdio.h> #include<conio.h> void main() { int i=0,n=1; clrscr(); while(i<10) { if(n%5==0 && n%8==0) { printf("%d\n",n); i++; } n++;

Prof. Ahlam Ansari

Page

11

SPA Program List & Solution


} getch(); } 21. Write a program to chck if the entered number is prime number or not. #include<stdio.h> #include<conio.h> void main() { int i=2,n; clrscr(); printf("Enter a number:"); scanf("%d",&n); while(n%i!=0) { i++; } if(n==i) { printf("Prime Number"); } else { printf("Not a prime number"); } getch(); } 22. Write a program to check if the entered number is Armstrong or not. #include<stdio.h> #include<conio.h> void main() { int sum=0,digit,x,copy; clrscr(); printf("Enter a number:");

Prof. Ahlam Ansari

Page

12

SPA Program List & Solution


scanf("%d",&x); copy=x; while(x!=0) { digit=x%10; sum=sum+digit*digit*digit; x=x/10; } if(sum==copy) { printf("Armstrong Number"); } else { printf("Not an Armstrong Number"); } getch(); } 23. Write a program to check if the year entered is leap year or not. #include<stdio.h> #include<conio.h> void main() { int year; clrscr(); printf("Enter a year:"); scanf("%d",&year); if(year%4==0 && year%100!=0 || year%100==0 && year%400==0) printf("Leap Year"); else printf("Not a leap year"); getch(); } 24. Write a program to display the factors of a user entered number.

Prof. Ahlam Ansari

Page

13

SPA Program List & Solution


#include<stdio.h> #include<conio.h> void main() { int n,i; clrscr(); printf("Enter a number:"); scanf("%d",&n); printf("Factors are:\n"); i=2; while(i<n) { if(n%i==0) printf("%d\n",i); i++; } getch(); } 25. Write a program to display the user entered number in words. #include<stdio.h> #include<conio.h> void main() { int n,digit,rev=0; clrscr(); printf("Enter a number:"); scanf("%d",&n); while(n!=0) { digit=n%10; rev=rev*10+digit; n=n/10; } while(rev!=0)

Prof. Ahlam Ansari

Page

14

SPA Program List & Solution


{ digit=rev%10; rev=rev/10; switch(digit) { 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; } } getch(); } 26. Write a menu driven program to perform add / subtract / multiply / divide / modulus based on the users choice. #include<stdio.h> #include<conio.h> void main()

Prof. Ahlam Ansari

Page

15

SPA Program List & Solution


{ int no1,no2,result,choice; clrscr(); printf("Enter two numbers:"); scanf("%d %d",&no1,&no2); printf("1.Add\n2.Subtract\n3.Multiply\n4.Divide\n5.Modulus\nEnter choice:"); scanf("%d",&choice); switch(choice) { case 1:result=no1+no2; printf("Sum= %d",result); break; case 2:result=no1-no2; printf("Difference= %d",result); break; case 3:result=no1*no2; printf("Product= %d",result); break; case 4:result=no1/no2; printf("Quotient= %d",result); break; case 5:result=no1%no2; printf("Remainder= %d",result); break; default:printf("Invalid choice"); } getch(); } 27. Write a program to display the month name by accepting the month number form user. #include<stdio.h> #include<conio.h> void main() your

Prof. Ahlam Ansari

Page

16

SPA Program List & Solution


{ int month; clrscr(); printf("Enter a month number:"); scanf("%d",&month); switch(month) { case 1:printf("January"); break; case 2:printf("February"); break; case 3:printf("March"); break; case 4:printf("April"); break; case 5:printf("May"); break; case 6:printf("June"); break; case 7:printf("July"); break; case 8:printf("August"); break; case 9:printf("September"); break; case 10:printf("October"); break; case 11:printf("November"); break; case 12: printf("December"); break; default:printf("Invalid month number"); } getch();

Prof. Ahlam Ansari

Page

17

SPA Program List & Solution


} 28. Write a program to demonstrate the use of break statement Or Write a program to accept 10, 2-digit numbers from user and add them. If the user enters a three digit number stop accepting the numbers and display the sum. #include<stdio.h> #include<conio.h> void main() { int n,total=0,i; clrscr(); for(i=1;i<=10;i++) { printf("Enter a number:"); scanf("%d",&n); if(n>99) break; total=total+n; } printf("Total=%d",total); getch(); } 29. Write a program to demonstrate the user of continue statement Or Write a program to accept 5, 2-digit numbers from user and add them. If the user enters a three digit number, this number should be discarded and again accepted. Also an indication must be given to the user that he has entered a number greater than k99 and hence it is not accepted. The sum must be displayed at the end. #include<stdio.h> #include<conio.h> void main() { int n,total=0,i; clrscr(); for(i=1;i<=5;i++)

Prof. Ahlam Ansari

Page

18

SPA Program List & Solution


{ printf("Enter a number:"); scanf("%d",&n); if(n>99) { printf("Number is greater than 99\n"); i--; continue; } total=total+n; } printf("Total=%d",total); getch(); }

30. Write a program to find GCD and LCM of two numbers. #include<stdio.h> #include<conio.h> void main() { int n1,n2,lcm,gcd; clrscr(); printf("Enter two numbers:"); scanf("%d %d",&n1,&n2); if (n1>n2) { lcm=n1; gcd=n2; } else { lcm=n2; gcd=n1; }

Prof. Ahlam Ansari

Page

19

SPA Program List & Solution


while(lcm%n1!=0 || lcm%n2!=0) { lcm++; } while(n1%gcd!=0 || n2%gcd!=0) { gcd--; } printf("LCM= %d \nGCD=%d",lcm,gcd); getch(); } 31. Write a program to add two numbers using function. #include<stdio.h> #include<conio.h> void main() { int n1,n2,sum; int add (int a, int b); clrscr(); printf("Enter two numbers:"); scanf("%d %d",&n1,&n2); sum=add(n1,n2); printf("Sum=%d",sum); getch(); } int add (int a, int b) { int c; c=a+b; return c; } 32. Write a program to find the factorial of a number using a function. #include<stdio.h> #include<conio.h>

Prof. Ahlam Ansari

Page

20

SPA Program List & Solution


void main() { int no,factorial; int fact (int no); clrscr(); printf("Enter a number:"); scanf("%d",&no); factorial=fact(no); printf("Factorial=%d",factorial); getch(); } int fact (int no) { int i,ans; for(i=1,ans=1;i<=no;i++) { ans=ans*i; } return ans; } 33. Write a program to find n Fibonacci elements, using a recursive function. #include<stdio.h> #include<conio.h> void main() { int n,i; int fibo (int ,int ,int); clrscr(); printf("Enter the number of elements:"); scanf("%d",&n); printf("0\n"); for(i=1;i<=n-1;i++) { printf("%d\n",fibo(0,1,i));

Prof. Ahlam Ansari

Page

21

SPA Program List & Solution


} getch(); } int fibo (int a, int b, int i) { if(i==1) return b; else return (fibo(b,a+b,--i)); } 34. Write a program to find value of y using recursive function, where y=xn. #include<stdio.h> #include<conio.h> void main() { int n,x,y; int exponential (int x, int n); clrscr(); printf("Enter the values of x and n:"); scanf("%d %d",&x,&n); y=exponential(x,n); printf("The value of x raise to n is %d",y); getch(); } int exponential (int x, int n) { if(n==1) return x; else return (x*exponential(x,n-1)); } 35. Write a program to accept n integers from user into an array and display them one in each line. #include<stdio.h>

Prof. Ahlam Ansari

Page

22

SPA Program List & Solution


#include<conio.h> void main() { int n,i,a[100]; clrscr(); printf("Enter the number of elements:"); scanf("%d",&n); for(i=0;i<=n-1;i++) { printf("Enter a value:"); scanf("%d",&a[i]); } printf("The numbers entered are\n"); for(i=0;i<=n-1;i++) { printf("%d\n",a[i]); } getch(); } 36. Write a program to accept n integers from user into an array and display the average of these numbers. #include<stdio.h> #include<conio.h> void main() { int n,i,a[100],sum=0; float avg; clrscr(); printf("Enter the number of elements:"); scanf("%d",&n); for(i=0;i<=n-1;i++) { printf("Enter a value:"); scanf("%d",&a[i]);

Prof. Ahlam Ansari

Page

23

SPA Program List & Solution


} for(i=0;i<=n-1;i++) { sum=sum+a[i]; } avg=sum; avg=avg/n; printf("The average of the numbers entered is %f",avg); getch(); } 37. Write a program to accept n integers from user into an array and display the count of even and odd numbers of these. #include<stdio.h> #include<conio.h> void main() { int n,i,a[100],even=0; clrscr(); printf("Enter the number of elements:"); scanf("%d",&n); for(i=0;i<=n-1;i++) { printf("Enter a value:"); scanf("%d",&a[i]); } for(i=0;i<=n-1;i++) { if (a[i]%2==0) even++; } printf("The count of even numbers is %d and that of odd numbers is %d",even,(n-even)); getch(); }

Prof. Ahlam Ansari

Page

24

SPA Program List & Solution


38. Write a program to find an element in an array and display the index of the element using a function. OR Write a program to implement sequential search algorithm. #include<stdio.h> #include<conio.h> void main() { int n,i,a[100],x,index; int search (int a[], int n, int x); clrscr(); printf("Enter the number of elements:"); scanf("%d",&n); for(i=0;i<=n-1;i++) { printf("Enter a value:"); scanf("%d",&a[i]); } printf("Enter the element to be searched:"); scanf("%d",&x); index=search(a,n,x); if(index==n) printf("Not Found"); else printf("The element %d is found in the index %d",x,index); getch(); } int search (int a[], int n, int x) { int i; for(i=0;i<=n-1;i++) { if(x==a[i]) break; } return i;

Prof. Ahlam Ansari

Page

25

SPA Program List & Solution


} 39. Write a program to sort numbers in ascending order. R Write a program to implement bubble sorting algorithm for sorting numbers in ascending order. #include<stdio.h> #include<conio.h> void main() { int n,i,a[100],x,index; void ascend (int a[], int n); clrscr(); printf("Enter the number of elements:"); scanf("%d",&n); for(i=0;i<=n-1;i++) { printf("Enter a value:"); scanf("%d",&a[i]); } ascend(a,n); getch(); } void ascend (int a[], int n) { int i,j,temp; for(i=0;i<=n-2;i++) { for(j=0;j<=n-2;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } }

Prof. Ahlam Ansari

Page

26

SPA Program List & Solution


} printf("After sorting\n"); for(i=0;i<=n-1;i++) { printf("%d\n",a[i]); } } 40. Write a program to accept an m x n matrix and display it in natural form. #include<stdio.h> #include<conio.h> void main() { int m,n,i,j,a[10][10]; clrscr(); printf("Enter the number of rows and columns:"); scanf("%d %d",&m,&n); for(i=0;i<=m-1;i++) { for(j=0;j<=n-1;j++) { printf("Enter a value:"); scanf("%d",&a[i][j]); } } printf("The entered matrix is:\n"); for(i=0;i<=m-1;i++) { for(j=0;j<=n-1;j++) { printf("%d\t",a[i][j]); } printf("\n"); } getch();

Prof. Ahlam Ansari

Page

27

SPA Program List & Solution


} 41. Write a program to add two matrices of size m x n. #include<stdio.h> #include<conio.h> void main() { int m,n,i,j,a[10][10],b[10][10],c[10][10]; clrscr(); printf("Enter the number of rows and columns:"); scanf("%d %d",&m,&n); printf("Enter the elements of Matrix 1\n"); for(i=0;i<=m-1;i++) { for(j=0;j<=n-1;j++) { printf("Enter a value:"); scanf("%d",&a[i][j]); } } printf("Enter the elements of Matrix 2\n"); for(i=0;i<=m-1;i++) { for(j=0;j<=n-1;j++) { printf("Enter a value:"); scanf("%d",&b[i][j]); } } for(i=0;i<=m-1;i++) { for(j=0;j<=n-1;j++) { c[i][j]=a[i][j]+b[i][j]; }

Prof. Ahlam Ansari

Page

28

SPA Program List & Solution


} printf("The sum of two matrices is:\n"); for(i=0;i<=m-1;i++) { for(j=0;j<=n-1;j++) { printf("%d\t",c[i][j]); } printf("\n"); } getch(); } 42. Write a program to accept a string, copy it into another string and display this new string. #include<conio.h> #include<stdio.h> #include<string.h> void main() { char a[100],b[100]; clrscr(); printf("Enter a string\n"); gets(a); strcpy(b,a); printf("The new string is %s",b); getch(); } 43. Write a program to accept two strings, join them and display the result. #include<conio.h> #include<stdio.h> #include<string.h> void main() { char a[100],b[100];

Prof. Ahlam Ansari

Page

29

SPA Program List & Solution


clrscr(); printf("Enter two strings:\n"); gets(a); gets(b); strcat(a,b); printf("The concatenated string is %s",a); getch(); } 44. Write a program to accept a string and find its length without using the string header file. #include<conio.h> #include<stdio.h> void main() { char a[100]; int len=0; clrscr(); printf("Enter a string:\n"); gets(a); while(a[len]!='\0') { len++; } printf("The length of this string is %d characters.",len); getch(); } 45. Write a program to reverse a user entered string. #include<stdio.h> #include<conio.h> void main() { int n=0; char a[100]; void reverse (char a[100], int n);

Prof. Ahlam Ansari

Page

30

SPA Program List & Solution


clrscr(); printf("Enter a string:"); gets(a); while(a[n]!='\0') { n++; } reverse(a,n); getch(); } void reverse (char a[100], int n) { int i; char temp; for(i=0;i<=(n-1)/2;i++) { temp=a[n-i-1]; a[n-i-1]=a[i]; a[i]=temp; } printf("The reverse of this string is: %s",a); } 46. Write a program to check whether the entered string is palindrome or not (Do not use the string header file). #include<stdio.h> #include<conio.h> void main() { int n=0,i; char a[100],rev[100]; clrscr(); printf("Enter a string:"); gets(a); while(a[n]!='\0')

Prof. Ahlam Ansari

Page

31

SPA Program List & Solution


{ n++; } for(i=0;i<=(n-1);i++) { rev[n-i-1]=a[i]; } for(i=0;i<=n-1;i++) { if(a[i]!=rev[i]) break; } if(i==n) printf("The string is palindrome."); else printf("The string is not palindrome."); getch(); } 47. Write a program to find the sum of column elements of a 2 dimensional MxN array A. #include<stdio.h> #include<conio.h> void main() { int m,n,i,j,a[10][10],b[10]; clrscr(); printf("Enter the number of rows and columns of matrix:"); scanf("%d %d",&m,&n); printf("Enter the values of matrix\n"); for(i=0;i<=m-1;i++) { for(j=0;j<=n-1;j++) { printf("Enter a value:");

Prof. Ahlam Ansari

Page

32

SPA Program List & Solution


scanf("%d",&a[i][j]); } } for(i=0;i<=n-1;i++) { b[i]=0; for(j=0;j<=m-1;j++) { b[i]=b[i]+a[j][i]; } } printf("The entered matrix is:\n"); for(i=0;i<=m-1;i++) { for(j=0;j<=n-1;j++) { printf("%d\t",a[i][j]); } printf("\n"); } printf("The sum of the columns are:\n"); for(i=0;i<=n-1;i++) { printf("%d\t",b[i]); } getch(); } 48. Write a program to count blank spaces, digits, vowels and consonants in the string. #include<conio.h> #include<stdio.h> void main() { char a[100]; int i,len=0,vowels=0,spaces=0,digits=0,consonants=0;

Prof. Ahlam Ansari

Page

33

SPA Program List & Solution


clrscr(); printf("Enter a string:\n"); gets(a); while(a[len]!=0) { len++; } for(i=0;i<=len-1;i++) { if(a[i]=='a' ||a[i]=='e' || a[i]=='i' || a[i]=='o' || a[i]=='u' || a[i]=='A' || a[i]=='E' || a[i]=='I' || a[i]=='O' || a[i]=='U') vowels++; else { if((a[i]>='a' && a[i]<='z') || (a[i]>='A' && a[i]<='Z')) consonants++; else { if(a[i]>='0' &&a[i]<='9') digits++; else { if(a[i]==' ') spaces++; } } } } printf("The total number of vowels are: %d\nThe total number of spaces are:%d\nThe total number of digits are: %d\nThe total number of consonants are: %d", vowels,spaces,digits,consonants); getch(); }

Prof. Ahlam Ansari

Page

34

SPA Program List & Solution


49. Write a program to demonstrate the access of global variable. #include<stdio.h> #include<conio.h> int a=5; void main() { int a=10; printf("%d\n",a); printf("%d\n",::a); a=::a+a; printf("%d\n",a); printf("%d\n",::a); ::a=a; printf("%d\n",a); printf("%d\n",::a); getch(); } 50. Write the program to add two numbers using function. Pass the pointers to the variables as reference to the functions. #include<stdio.h> #include<conio.h> void main() { int a,b; void sum (int *p1, int *p2); clrscr(); printf("Enter two numbers"); scanf("%d %d",&a,&b); sum(&a,&b); getch(); } void sum (int *p1, int *p2) { int c;

Prof. Ahlam Ansari

Page

35

SPA Program List & Solution


c=*p1+*p2; printf("The sum is equal to %d",c); } 51. Write a program to swap two numbers using a function. Pass the values to be swapped to this function using call-by-value method. #include<stdio.h> #include<conio.h> void main() { int a,b; void swap (int a, int b); clrscr(); printf("Enter two numbers:"); scanf("%d %d",&a,&b); printf("The values of a and b in the main function before calling the swap function are %d and %d\n",a,b); swap(a,b); printf("The values of a and b in main function after calling the swap function are %d and %d\n",a,b); getch(); } void swap (int a, int b) { int temp; temp=a; a=b; b=temp; printf("The values of a and b in the swap function after swapping are %d and %d\n",a,b); } 52. Write a program to swap two numbers using a function. Pss the values to be swapped to this function using call-by-reference method. #include<stdio.h> #include<conio.h> void main()

Prof. Ahlam Ansari

Page

36

SPA Program List & Solution


{ int a,b; void swap (int *p1, int *p2); clrscr(); printf("Enter two numbers:"); scanf("%d %d",&a,&b); printf("The values of a and b in the main function before calling the swap function are %d and %d\n",a,b); swap(&a,&b); printf("The values of a and b in main function after calling the swap function are %d and %d\n",a,b); getch(); } void swap (int *p1, int *p2) { int temp; temp=*p1; *p1=*p2; *p2=temp; printf("The values of a and b in the swap function after swapping are %d and %d\n",*p1,*p2); } 53. Write a program to store and display the name, roll number and fees of a student using structure. #include<conio.h> #include<stdio.h> struct student { char name[20]; int roll_no; float fees; }; void main () { struct student s1;

Prof. Ahlam Ansari

Page

37

SPA Program List & Solution


clrscr(); printf("Enter the student's name, roll number and fees paid:"); gets(s1.name); scanf("%d %f",&s1.roll_no,&s1.fees); printf("The student details are as follows:\nName:%s\nRoll

number:%d\nFees:%f\n",s1.name,s1.roll_no,s1.fees); getch(); } 54. Define structure within structure consisting of following elements: i. ii. iii. iv. Employee Code Employee Name Employee Salary and Employee Date_of_joining

Write a C program to read at least 10 records and display them. #include<conio.h> #include<stdio.h> struct employee { char name[20]; int salary,code; struct { int date,month,year; }date_joining; }; void main () { struct employee e[100]; int i; clrscr(); for(i=0;i<=9;i++) { printf("Enter the employee's name, code, salary and date of joining as date, month and year separately:");

Prof. Ahlam Ansari

Page

38

SPA Program List & Solution


scanf("%s onth,&e[i].date_joining.year); } printf("\nEmployee list\nName\tCode\tSalary\tDate of joining\n"); printf("---------------------------------------------------\n"); for(i=0;i<=9;i++) { %d %d %d %d

%d",e[i].name,&e[i].code,&e[i].salary,&e[i].date_joining.date,&e[i].date_joining.m

printf("%s\t%d\t%d\t%d/%d/%d\n",e[i].name,e[i].code,e[i].salary,e[i].date_join ing.date,e[i].date_joining.month,e[i].date_joining.year); } getch(); } 55. Write a program to accept a set of 10 numbers and print the numbers using pointers. #include<stdio.h> #include<conio.h> void main () { int *p,i,a[10]; clrscr(); p=&a; printf("Enter 10 nos.\n"); for(i=0;i<=9;i++) scanf("%d",(p+i)); for(i=0;i<=9;i++) printf("%d\n",*(p+i)); getch(); } 56. Write a program to accept a set of 10 numbers and print the numbers using pointers. Find average of these integers. #include<stdio.h> #include<conio.h> void main ()

Prof. Ahlam Ansari

Page

39

SPA Program List & Solution


{ int *p,i,sum=0,a[10]; float avg; clrscr(); p=a; printf("Enter 10 nos.\n"); for(i=0;i<=9;i++) { scanf("%d",(p+i)); sum=sum+*(p+i); } avg=sum/10.0; for(i=0;i<=9;i++) printf("%d\n",*(p+i)); printf("Average=%f",avg); getch(); } 57. Write a program to accept a set of characters from user until the user presses the full stop( . ) and store it in a text file. Read from the file and display the contents of the file. # include<stdio.h> # include<conio.h> void main() { FILE *fp; char c=' '; clrscr(); fp=fopen("test.txt","w"); printf("Write data to be stored in the file and once completed press the full stop (.):\n"); while(c!='.') { scanf("%c",&c); fputc(c,fp);

Prof. Ahlam Ansari

Page

40

SPA Program List & Solution


} fclose(fp); fp=fopen("test.txt","r"); while(!feof(fp)) { printf("%c",getc(fp)); } fclose(fp); getch(); } 58. Write a program to accept the name and roll number of a student and store it in a text file. Read the stored data and display the same from the file. # include<stdio.h> # include<conio.h> void main() { FILE *fp; char name[20],temp1[20]; int roll,temp2; clrscr(); fp=fopen("Student.txt","w+"); printf("Enter name and roll number of the student:"); scanf("%s %d",name,&roll); fprintf(fp,"%s %d",name,roll); printf("Name\tRoll no.\n"); rewind(fp); fscanf(fp,"%s %d",temp1,&temp2); printf("%s\t%d",temp1,temp2); fclose(fp); getch(); } 59. Write a program to copy the contents from one text file to another. # include<stdio.h> # include<conio.h>

Prof. Ahlam Ansari

Page

41

SPA Program List & Solution


void main() { FILE *fp,*fp1; char c; clrscr(); fp=fopen("test.txt","r"); fp1=fopen("test1.txt","w"); while(!feof(fp)) { c=getc(fp); fputc(c,fp1); } fclose(fp1); fclose(fp); fp1=fopen("test1.txt","r"); printf("Displaying data from the new file:\n"); while(!feof(fp1)) { printf("%c",getc(fp1)); } fclose(fp1); getch(); } 60. Write a program to count the number of characters in a text file. # include<stdio.h> # include<conio.h> void main() { FILE *fp; int n=0; clrscr(); fp=fopen("test.txt","r"); while(!feof(fp)) {

Prof. Ahlam Ansari

Page

42

SPA Program List & Solution


getc(fp); n++; } n--; printf("The total number of characters in the file is %d:\n",n); fclose(fp); getch(); }

Prof. Ahlam Ansari

Page

43

You might also like