GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer...

33
GTU Practical Solution Subject: Computer Programming and Utilization (2110003) [.B.E. 1 st SEM : ALL BRANCHES] 1.1 Write a Program to display “WELCOME TO SILVER OAK INSTITUTE OF TECHNOLOGY” on the screen. #include<stdio.h> #include<conio.h> void main() { printf("WELCOME TO SILVER OAK INSTITUTE OF TECHNOLOGY); getch(); } 1.2 Write a Program to which accepts a character from keyboard & display its ASCII code. #include<stdio.h> #include<conio.h> void main() { char ascii; printf("Give character: "); scanf(“%c”,&ascii); printf("%c ascii value is: %d", ascii, ascii); getch(); } 1.3 Write a program that reads two nos. from key board and gives their addition, subtraction, multiplication, division and modulo. #include<conio.h> #include<stdio.h> void main() { int a,b,add,sub,mul,div,mod; printf("Enter Two Numbers" ); scanf(“%d%d”,&a,&b); printf("ARITHMETIC OPREATION\n" ); add=a+b; sub=a-b; mul=a*b;

Transcript of GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer...

Page 1: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

GTU Practical Solution

Subject: Computer Programming and Utilization (2110003)

[.B.E. 1st SEM : ALL BRANCHES]

1.1 Write a Program to display “WELCOME TO SILVER OAK INSTITUTE OFTECHNOLOGY” on the screen.

#include<stdio.h>#include<conio.h>void main(){printf("WELCOME TO SILVER OAK INSTITUTE OF TECHNOLOGY);getch();}

1.2 Write a Program to which accepts a character from keyboard & display its ASCIIcode.

#include<stdio.h>#include<conio.h>void main(){char ascii;printf("Give character: ");scanf(“%c”,&ascii);printf("%c ascii value is: %d", ascii, ascii);getch();}

1.3 Write a program that reads two nos. from key board and gives their addition,subtraction, multiplication, division and modulo.

#include<conio.h>#include<stdio.h>void main(){

int a,b,add,sub,mul,div,mod;printf("Enter Two Numbers" );scanf(“%d%d”,&a,&b);

printf("ARITHMETIC OPREATION\n" );

add=a+b;sub=a-b;mul=a*b;

Page 2: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

div=a/b;mod=a%b;

printf("\nAddition of two Number=%d",add);printf("\nSubstraction of two Number=%d",sub);printf("\nMultiplication of two Number=%d",mul);printf("\nDivision of two Number=%d",div);printf("\nModule of two Number=%d",mod);

getch();}

1.4 Write a program to convert days into months and days.

#include<stdio.h>void main(){int d;printf("Enter days\n");scanf("%d",&d);printf("%d years,%d months,%ddays\n",d/365,(d%365)/30,(d%365)%30);getch();}

1.5. The distance between two cities (In KM) is input through key board. Write a programto convert and print this distance in meters, feet, inches & centimetres.

#include<stdio.h>#include<conio.h>void main(){float km,m,feet,inch,cm;printf("Enter the distance between two cities(in km)-");scanf("%f",&km);

m = km*1000; //since 1km = 1000mfeet= km*3280.84; //since 1km=3280.84feetinch=km*39370.1; //since 1 km=39370.1inchescm=km*100000; //since 1km = 100000cm

printf("\nDistance in kilometres = %f ",km);printf("\nDistance in metres = %f ",m);printf("\nDistance in feet = %f ",feet);printf("\nDistance in inches = %f ",inch);printf("\nDistance in centimetres = %f ",cm);getch();}

Page 3: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

1.6 Write a program to swap (interchange) values of two variables with and without usingthird variable.

#include<stdio.h>#include<conio.h>void main(){

int x, y, temp;//Using third Variableprintf("Enter the value of x and y\n");scanf("%d%d", &x, &y);

printf("Before Swapping\nx = %d\ny = %d\n",x,y);temp = x;x = y;y = temp;printf("After Swapping\nx = %d\ny = %d\n",x,y);getch();}

#include<stdio.h>#include<conio.h>void main(){

int x, y, temp;// Without Using third Variableint a, b;

printf("Enter two integers to swap\n");scanf("%d%d", &a, &b);a = a + b;b = a - b;a = a - b;printf("After Swapping”);printf("a = %d\nb = %d\n",a,b);getch();}

PRACTICAL SET-2:

2.1 Write a Program to convert the Celsius to Fahrenheit. F=(C*1.8)+32

#include<stdio.h>#include<conio.h>void main(){float fh,cl;printf("Enter temperature value in Celsius: ");scanf("%f", &cl);fh = (cl * 1.8)+ 32;printf("Converted Fahrenheit value: %f",fh);getch();}

Page 4: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

2.2 Write a Program to find out simple interest. I=(P*R*N)/100

#include<stdio.h>#include<conio.h>void main(){float p,n,r,i;printf("Enter the value of principle:");scanf("%f",&p);printf("Enter the value of rate:");scanf("%f",&r);printf("Enter the value of time:");scanf("%f",&n);i=(p*n*r)/100;printf("\nInterest: %f ",i);getch();}

2.3 Write a program to solve Quadratic Equation.

#include<stdio.h>#include<math.h>void main(){float a,b,c;float d,root1,root2;

printf("Enter a, b and c of quadratic equation: ");scanf("%f%f%f",&a,&b,&c);

d = b * b - 4 * a * c;

if(d < 0){printf("Roots are complex number.\n");

printf("Roots of quadratic equation are: ");printf("%.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a));printf("%.3f%+.3fi",-b/(2*a),-sqrt(-d)/(2*a));

return 0;}else if(d==0){printf("Both roots are equal.\n");

root1 = -b /(2* a);printf("Root of quadratic equation is: %.3f ",root1);

return 0;}else{printf("Roots are real numbers.\n");

Page 5: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

root1 = ( -b + sqrt(d)) / (2* a);root2 = ( -b - sqrt(d)) / (2* a);printf("Roots of quadratic equation are: %.3f%.3f",root1,root2);}getch();}

PRACTICAL SET-3:

3.1. Write a program to reverse sequence of digit.

#include <stdio.h>int main(){int n, reverse = 0;

printf("Enter a number to reverse\n");scanf("%d",&n);

while (n != 0){

reverse = reverse * 10;reverse = reverse + n%10;n = n/10;

}

printf("Reverse of entered number is = %d\n", reverse);return 0;}

3.2. Write a program to print first N prime numbers.( no factor other than 1 & itself)

#include<stdio.h>int main(){

int n, i = 3, count, c;

printf("Enter the number of prime numbers required\n");scanf("%d",&n);

if ( n >= 1 ){

printf("First %d prime numbers are :\n",n);printf("2\n");

}for ( count = 2 ; count <= n ; ){

Page 6: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

for ( c = 2 ; c <= i - 1 ; c++ ){

if ( i%c == 0 )break;

}if ( c == i ){

printf("%d\n",i);count++;

}i++;

}return 0;

}

3.3. Write a program to print Armstrong number (or Narcissistic no) between 0 to 999.(Armstrong no= addition of cube of every digit of a no, e.g. 153 is Armstrong due to1^3+5^3+3^3=153)

#include<stdio.h>int main(){int n1, rem;printf("Enter a positive integer: ");scanf("%d", &n);for(i=1;i<=999;i++){

n1=i;num=0;while(n1!=0){

rem=n1%10;num+=rem*rem*rem;n1/=10;

}if(num==i)printf("%d is an Armstrong number.",n);

elseprintf("%d is not an Armstrong number.",n);

}return 0;}

3.4. Write a program to find out whether a number is odd / even by using different if..elsestatements.

#include <stdio.h>int main(){int num;printf("Enter an integer you want to check: ");

Page 7: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

scanf("%d",&num);if((num%2)==0) /* Checking whether remainder is 0 or not. */

printf("%d is even.",num);else

printf("%d is odd.",num);return 0;

}

3.5. Write a program to find minimum no out of three numbers by using Controlstatement (nested..if ).

#include<stdio.h>#include<conio.h>void main(){int a,b,c;clrscr();

printf("Enter value of a::");scanf("%d",&a);printf("Enter value of b::");scanf("%d",&b);printf("Enter value of c::");scanf("%d",&c);

if(a<b){

if(a<c){

printf("\n a is smallest");}else{

printf("\n c is smallest");}

}else{ if(b<c)

{printf("\n b is smallest");

}else{

printf("\n c is smallest");}

}getch();}

Page 8: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

3.6. Write a program to make simple calculator using switch..case statement.

# include <stdio.h>int main(){char o;float num1,num2;printf("Enter operator either + or - or * or / or %: ");scanf("%c",&o);printf("Enter two operands: ");scanf("%f %f",&num1,&num2);switch(o) {case '+':printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);break;case '-':printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);break;case '*':printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);break;case '/':printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);break;case '%':printf("%.1f % %.1f = %.1f",num1, num2, num1%num2);break;default:/* If operator is other than +, -, * , / or %, error messageis shown */printf("Error! operator is not correct");break;}return 0;}

3.7. Write a program to find sum of all integers greater than 100 & less than 200 and aredivisible by 5.

#include<stdio.h>#include<conio.h>void main(){

int i, sum=0;clrscr();

printf("All nos. between 100 - 200 which is divisible by\n");for(i=101;i<200;i++){

if(i%5==0)

Page 9: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

{printf("%5d",i);sum+=i; }

}printf("\n\nsum = %d",sum);getch();

}

3.8. Write a program to find whether a number is palindrome or not.

#include<stdio.h>int main(){int n, reverse=0, rem,temp;printf("Enter an integer: ");scanf("%d", &n);temp=n;while(temp!=0){

rem=temp%10;reverse=reverse*10+rem;temp/=10;

}/* Checking if number entered by user and it's reversenumber is equal. */if(reverse==n)

printf("%d is a palindrome.",n);else

printf("%d is not a palindrome.",n);return 0;

}

PRACTICAL SET-4:

** ** * ** * * ** * * * *

#include <stdio.h>int main(){

int i, j;for(i=1;i<=5;i++){

for(j=1;j<=i;j++)

Page 10: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

{printf("*");

}printf("\n");

}return 0;

}

12 23 3 34 4 4 45 5 5 5 5

#include<stdio.h>int main(){

int i, j;for(i=1;i<=5;i++){

for(j=1;j<=i;j++){

printf("%d",i);}printf("\n");

}return 0;

}

10 11 0 10 1 0 11 0 1 0 1

#include <stdio.h>int main(){int i, j;for(i=1;i<=4;i++){for(j=i;j>=1;j--){printf("%d",j%2);

}

Page 11: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

printf("\n");}return 0;

}

5 4 3 2 14 3 2 13 2 12 11

#include<stdio.h>#include <stdio.h>int main(){

int i, j;for(i=5;i>=1;i--){

for(j=i;j>=1;j--){

printf("%d",j);}printf("\n");

}return 0;

}

1A B

2 3 4C D E F

5 6 7 8 9

#include<stdio.h>#include<conio.h>void main(){

int rno=0,i,j,k,no1=1,ch1=65,count=0;clrscr();printf("Enter the number of rows you want:");scanf("%d",&rno);for(i=1;i<=rno;i++){

count++;for(k=rno-i;k>=1;k--)

Page 12: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

{printf(" ");

}if(i%2==0){

for(j=1;j<=count;j++){

printf("%c ",ch1);ch1++;

}}else{

for(j=1;j<=count;j++){

printf("%d ",no1);no1++;

}}printf("\n");

}getch();

}

PRACTICAL SET-5:

5.1. Write a program for following Series: 1 + 3 + 5 +… + n

#include<stdio.h>#include<conio.h>void main(){int n,result;int addTotal(int);clrscr();printf("enter any number: ");scanf("%d",&n);result=addTotal(n);printf("\nThe sum 1 to n is %d",result);getch();}int addTotal(int m){int sum=0,i;for(i=1;i<=m;i++){

if(i%2==1)sum= sum + i;

}return sum;}

Page 13: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

5.2. Write a program for following Series: 13 + 23 + 33 + 43 … + n3

#include<stdio.h>#include<math.h>

int main(){

int n,i;int sum=0;

printf("Enter the n i.e. max values of series: ");scanf("%d",&n);

sum = pow(((n * (n + 1) ) / 2),2);

printf("Sum of the series : ");

for(i =1;i<=n;i++){if (i != n)

printf("%d^3 + ",i);else

printf("%d^3 = %d ",i,sum);}return 0;

}

5.3. Write a Program to print Fibonacci series.(1 1 2 3 5 8 13……)

#include<stdio.h>

int main(){int n, first = 0, second = 1, next, i;

printf("Enter the number of terms\n");scanf("%d",&n);

printf("First %d terms of Fibonacci series are :-\n",n);

for ( i = 0 ; i < n ; i++ ){

if ( i <= 1 )next = i;

else{

next = first + second;first = second;second = next;

}

Page 14: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

printf("%d\n",next);}return 0;

}

5.4. Write a program to print 1+1/2+1/3+1/4+………+1/N series.

#include<stdio.h>#include<conio.h>void main(){double n,sum=0,i;clrscr();

printf("\n Please Give The Value of N: ");scanf("%lf",&n);for(i=1;i<=n;i++){

sum = sum + (1/i);if(i==1)

printf("\n 1 +");elseif(i==n)

printf(" (1/%d) ",i);else

printf(" (1/%d) + ",i);}printf("\n\n THE SUM OF THIS SERIES IS %.2lf",sum);getch();}

PRACTICAL SET-6:

6.1 Write a program to display Sum and average of any 10 no. using 1-D Array.

#include<stdio.h>#include<conio.h>

void main(){int arr[10];int i,sum=0,avg=0;clrscr();

for(i=0;i<10;i++){scanf("%d",&arr[i]);sum = sum + arr[i];

}

Page 15: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

printf("\n Sum of array elements = %d",sum);

avg = sum/10;

printf("\n Average = %d",avg);getch();}

6.2 Write a Program to Find largest odd number from given 1-D Array.

#include<stdio.h>void main(){int array[10],temp, i, num;clrscr();printf("Enter the size of an array \n");scanf("%d", &num);printf("Enter the elements of the array \n");for (i = 0; i<num; i++){

scanf("%d", &array[i]);}printf("\n Odd numbers in the array are::");for (i = 0; i<num; i++){

if (array[i] % 2 != 0){

printf("%d \t", array[i]);}temp=array[0];

}for (i = 0; i<num; i++){

if(array[i]>=temp){temp=array[i];

}}printf("\nThe Largest Odd Number::%d",temp);getch();}

6.3 Write a program to read array of integers and print it in reverse order

#include<stdio.h>#include<conio.h>int main() {int arr[30], i, j, num, temp;

Page 16: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

printf("\nEnter no of elements : ");scanf("%d", &num);//Read elements in an arrayfor (i = 0; i < num; i++){

scanf("%d", &arr[i]);}j = i - 1; // j will Point to last Elementi = 0; // i will be pointing to first elementwhile (i < j) {

temp = arr[i];arr[i] = arr[j];arr[j] = temp;i++; // increment ij--; // decrement j

}//Print out the Result of Insertionprintf("\nResult after reversal : ");for (i = 0; i < num; i++) {

printf("%d \t", arr[i]);}return (0);

}

6.4 Write a program to Sort given Array in Descending Order.

#include<stdio.h>void main (){

int number[30];int i, j, a, n;

printf("Enter the value of N\n");scanf("%d", &n);printf("Enter the numbers \n");

for (i = 0; i < n; ++i)scanf("%d", &number[i]);

/*...sorting begins ... */

for (i = 0; i < n; ++i){

for (j = i + 1; j < n; ++j){

if (number[i] < number[j]){

a = number[i];number[i] = number[j];number[j] = a;

Page 17: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

}}

}printf("The numbers arranged in descending order are givenbelow\n");for (i = 0; i < n; ++i)

{printf("%d\n", number[i]);

}return 0;}

6.5 Write a program to Add two Matrices. i.e. C= A + B

#include<stdio.h>

int main(){

int m,n,c,d,first[10][10],second[10][10],sum[10][10];

printf("Enter the number of rows and columns of matrix\n");scanf("%d%d", &m, &n);printf("Enter the elements of first matrix\n");

for (c = 0; c < m; c++)for (d = 0; d < n; d++)

scanf("%d", &first[c][d]);

printf("Enter the elements of second matrix\n");

for (c = 0; c < m; c++)for (d = 0 ; d < n; d++)

scanf("%d", &second[c][d]);

printf("Sum of entered matrices:-\n");

for (c = 0; c < m; c++) {for (d = 0 ; d < n; d++) {

sum[c][d] = first[c][d] + second[c][d];printf("%d\t", sum[c][d]);

}printf("\n");

}return 0;

}

Page 18: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

6.6 Write a program to Multiply two 3X3 Matrices. i.e. C= A * B

#include<stdio.h>int main() {

int a[10][10], b[10][10], c[10][10], i, j, k;int sum = 0;

printf("\nEnter First Matrix : n");for (i = 0; i < 3; i++) {

for (j = 0; j < 3; j++) {scanf("%d", &a[i][j]);

}}printf("\nEnter Second Matrix:n");for (i = 0; i < 3; i++) {

for (j = 0; j < 3; j++) {scanf("%d", &b[i][j]);

}}//Multiplication Logicfor (i = 0; i <= 2; i++) {

for (j = 0; j <= 2; j++) {sum = 0;for (k = 0; k <= 2; k++) {

sum = sum + a[i][k] * b[k][j];}c[i][j] = sum;

}}printf("\nMultiplication Of Two Matrices : \n");for (i = 0; i < 3; i++) {

for (j = 0; j < 3; j++) {printf(" %d ", c[i][j]);

}printf("\n");

}return (0);

}

PRACTICAL SET-7:

7.1 Write a program to Find String Length without using inbuilt function.

#include<stdio.h>void main(){

char string[50];int i, length = 0;printf("Enter a string \n");

Page 19: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

gets(string);/*keep going through each character of the string till itsend */

for (i = 0; string[i] != '\0'; i++){

length++;}

printf("The length of %s = %d\n", string, length);getch();}

7.2 Write a program to Count no. of Vowels in given String.

#include<stdio.h>void main(){

char sentence[80];int i, vowels = 0, consonants = 0, special = 0;

printf("Enter a sentence \n");gets(sentence);for (i = 0; sentence[i] != '\0'; i++){

if ((sentence[i] == 'a' || sentence[i] == 'e' ||sentence[i] == 'i' || sentence[i] == 'o' || sentence[i] =='u') || (sentence[i] == 'A' || sentence[i] == 'E' ||sentence[i] == 'I' || sentence[i] == 'O' || sentence[i] =='U'))

{//vowels = vowels + 1;++vowels;

}}printf("No. of vowels in %s = %d\n", sentence, vowels);getch();}

7.3 Write a program to append one string to another String.

#include<stdio.h>void main(void){char str1[25],str2[25];int i=0,j=0;printf("\nEnter First String:");gets(str1);printf("\nEnter Second String:");gets(str2);

Page 20: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

while(str1[i]!='\0')i++;while(str2[j]!='\0'){str1[i]=str2[j];j++;i++;

}str1[i]='\0';printf("\nConcatenated String is %s",str1);

}

7.4 Write a program to check whether given String is Palindrome or not.

#include <stdio.h>#include <string.h>int main(){

char string1[20];int i, length;int flag = 0;

printf("Enter a string:");scanf("%s", string1);

length = strlen(string1);

for(i=0;i < length ;i++){if(string1[i] != string1[length-i-1]){

flag = 1;break;

}}if(flag) {

printf("%s is not a palindrome", string1);}else {

printf("%s is a palindrome", string1);}return 0;

}

7.5 Write a program to copy one string to another string

#include<stdio.h>int main(){

char s1[100], s2[100], i;printf("Enter string s1: ");

Page 21: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

scanf("%s",s1);for(i=0; s1[i]!='\0'; ++i){

s2[i]=s1[i];}s2[i]='\0';printf("String s2: %s",s2);return 0;

}

7.6 Write a program to count no of words in a sentence.

#include<stdio.h>#include<string.h>

void main(){

char s[200];int count = 0, i;

printf("enter the string\n");scanf("%[^\n]s", s);for (i = 0;s[i] != '\0';i++){

if (s[i] == ' ')count++;

}printf("number of words in given string are: %d\n",

count + 1);}

7.7 Write a program convert character into TOggLe character.

#include<stdio.h>#include<conio.h>int main(){char ch;printf("Enter any character : ");scanf("%ch", &ch);

if(ch>='A' && ch<='Z')ch=ch+32;

else if(ch>'a' && ch<='z')ch=ch-32;

printf("Convert case of character : %c",ch);getch();

Page 22: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

return 0;}

PRACTICAL SET-8:

8.1 Write a Program using function to Add 1st n numbers and also Find out Average of1st n numbers. (Where n is enter through Keyboard.)

#include<stdio.h>int add(int x){

int sum=0,i=1;for(i=1;i<=x;i++){

sum=sum+i;}

return sum;}void main (){int no,sum=0;float avg;clrscr();printf("Please Enter the No:");scanf("%d",&no);sum=add(no);avg=(float)sum/(float)no;printf("\nSum Up to Given Number is:%d",sum);printf("\nAvg of Sum Up Given Number is:%f",avg);getch();

}

8.2 Write a Program using function to Find out Cube of any given number N.

#include<stdio.h>#include<conio.h>void cube();void main(){int no;clrscr();printf("Enter the number for Cubic::");scanf("%d",&no);cube(no);getch();}void cube(int x){

Page 23: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

int ans;ans=x*x*x;printf("\n Cube of given num:: %d",ans);

}

8.3 Write a Program using function to Find out Factorial of given number with andwithout recursion.

//using recursion#include<stdio.h>int factorial(int);void main(){

int n,ans=0;clrscr();printf("Enter an positive integer: ");scanf("%d",&n);ans=factorial(n);printf("Factorial of %d = %d", n, ans);getch();

}int factorial(int x){

if(x==1){return 1;}else{return (x*factorial(x-1));}

}

//Without using recursion#include<stdio.h>#include<conio.h>void fact(int);void main(){

void fact();clrscr();printf("Enter the value of n:: ");scanf("%d",&n);fact(n);getch();

}void fact(int x){

int i,ans=1;

Page 24: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

for(i=1;i<=x;i++){

ans=ans*i;}printf("\n Factorial of %d is %d",x,ans);

}

8.4 Write a calculator program (add, subtract, multiply, divide). Prepare user definedfunction for each functionality.

#include<stdio.h>#include<conio.h>void Add(int,int);void Subtract(int,int);void Multiply(int,int);void Divide(int,int);void main(){

int a,b;clrscr();print(“Enter two Numbers”);scanf(“%d%d”,&a,&b);Add(a,b);Subtract(a,b);Multiply(a,b);Divide(a,b);getch();

}void Add(int a,int b){

printf(“add:%d”,a+b);}void Subtract(int a,int b){

printf(“Sub:%d”,a-b);}void Multiply(int a,int b){

printf(“Mul:%d”,a*b);}void Divide(int a,int b){

printf(“Div:%d”,a/b);}

8.5 Write a function prime that return 1 if it‘s argument is prime and return 0 otherwise.

#include<stdio.h>

Page 25: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

#include<conio.h>void main(){

int prime(int n);int i,n;clrscr();printf("\n Enter the number::");scanf("%d",&n);i=prime(n);if(i==1)printf("The number is prime");elseprintf("The number is not prime");getch();

}int prime(int n){

int j,check=0;for(j=2;j<n;j++){

if(n%j==0){

return(0);check=1;break;

}}if(check==0)return(1);

}

PRACTICAL SET-9:

9.1 Write a program to print an address of a variable.

#include<stdio.h>#include<conio.h>void main(){

int x,*p;clrscr();x=10;p=&x;printf("\nAddressof variable x is %u",p);

getch();}

9.2 Write a program to change the value of a variable using pointer.

Page 26: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

#include<stdio.h>#include<conio.h>void main(){

int x,*p;clrscr();x=10;p=&x;*p=20;

printf("\n x = %d",x);getch();}

9.3 Write a program using pointer to compare two strings.

#include<stdio.h>int compare_string(char*, char*);

main(){

char first[100], second[100], result;

printf("Enter first string\n");gets(first);

printf("Enter second string\n");gets(second);

result = compare_string(first, second);

if (result == 0)printf("Both strings are same.\n");

elseprintf("Entered strings are not equal.\n");

return 0;}

int compare_string(char *first, char *second){while(*first==*second){

if ( *first == '\0' || *second == '\0' )break;

first++;second++;

}if( *first == '\0' && *second == '\0' )

Page 27: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

return 0;else

return -1;}

9.4 Write a program using pointer to read an array of integer and print elements inreverse order.

#include<stdio.h>#include<conio.h>void main(){int *ptr,i,n;clrscr();printf(“Enter the no of elements:”);scanf(“%d”,&n);

for(i=0; i<n; i++){printf(“Enter %d element : “,i+1);scanf(“%d”,&ptr[i]);

}printf(“Array in reverse order\n”);for(i=n-1; i>=0; i–)printf(“%d\n”,ptr[i]);

getch();}

9.5 Write a program using pointer to copy one string to another string.

#include <stdio.h>void main(){

char s1[100], s2[100], i;printf("Enter string s1: ");scanf("%s",s1);for(i=0; s1[i]!='\0'; ++i){

s2[i]=s1[i];}s2[i]='\0';printf("String s2: %s",s2);getch();

}

PRACTICAL SET-10:

Page 28: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

10.1 Define a structure called cricket that will describe the following information: a.Player name b. Team name c. Batting average

#include<stdio.h>#include<conio.h>#include<string.h>#define total 2struct cricket{

char nm[20],team[20];int avg;

};

int main(){

struct cricket player[total],temp;int i,j;clrscr();for(i=0;i<total;i++){

printf("For player %d\n",i+1);printf("Enter the name of player : ");fflush(stdin);gets(player[i].nm);printf("Enter the team : ");fflush(stdin);gets(player[i].team);printf("Enter the batting average : ");fflush(stdin);scanf("%d",&player[i].avg);

}printf("\nTeam Name Average\n");printf(" \n");for(i=0;i<total;i++){

printf("%-10s %-10s %7d\n", player[i].team,player[i].nm,player[i].avg);

}getch();

}

10.2 Write a function to enter Roll no, marks of the three subject for 3 student and findtotal obtained by each student.

#include<stdio.h>#include<conio.h>void main(){struct student

Page 29: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

{int rollno;char name[20];int m1,m2,m3;

};struct student s[20],t;int i,j,n;clrscr();printf("\n enter the limit");scanf("%d",&n);for(i=0;i<n;i++){printf("\n enter the roll no\n");scanf("%d",&s[i].rollno);printf("\n enter the name \n");scanf("%s",s[i].name);printf("\n enter the mark1=");scanf("%d",&s[i].m1);printf("\n enter the mark2=");scanf("%d",&s[i].m2);printf("\n enter the mark3=");scanf("%d",&s[i].m3);

}for(i=0;i<n;i++){printf("\n rollno=%d",s[i].rollno);printf("\n name=%s",s[i].name);printf("\n mark1=%d",s[i].m1);printf("\n mark2=%d",s[i].m2);printf("\n mark3=%d",s[i].m3);

}getch();}

PRACTICAL SET-11:

11.1 Write a program that uses a table of integers whose size will be specifiedinteractively at run time.

#include<stdio.h>#include<stdlib.h>void main(){

int *p, *table;int size;printf("\nWhat is the size of table?");scanf("%d",&size);printf("\n");

Page 30: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

if((table = (int*)malloc(size *sizeof(int))) == 0){

printf("No space available \n");exit(1);

}printf("\n Address of the first byte is %u \n", table);/* Reading table values*/printf("\nInput table values\n");

for (p=table; p<table + size; p++)scanf("%d",p);

/* Printing table values <span id="IL_AD3" class="IL_AD">inreverse</span> order*/

for (p = table + size -1; p >= table; p --)printf("%d is stored at address %u \n",*p,p);

getch();}

11.2 Write a program to store a character string in block of memory space created bymalloc and then modify the same to store a large string.

#include<stdio.h>#include<stdlib.h>#define NULL 0void main(){

char *buffer;/* Allocating memory */if((buffer = (char *)malloc(10)) == NULL){

printf(“malloc failed.\n”);exit(1);

}

printf(“Buffer of size %d created \n”,_msize(buffer));strcpy(buffer, “HYDERABAD”);printf(“\nBuffer contains: %s \n “, buffer);

/* Realloction */if((buffer = (char *)realloc(buffer, 15)) == NULL){

printf(“Reallocation failed. \n”);exit(1);

}printf(“\nBuffer size modified. \n”);printf(“\nBuffer still contains: %s \n”,buffer);strcpy(buffer, “SECUNDERBAD”);printf(“\nBuffer now contains: %s \n”,buffer);

Page 31: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

/* Freeing memory */free(buffer);

return 0;}

PRACTICAL SET-12:

12.1 Write a program to create a file and perform basic I/O operations on the same.

// Writing to a file

#include <stdio.h>int main(){

int n;FILE *fptr;fptr=fopen("C:\\program.txt","w");if(fptr==NULL){

printf("Error!");exit(1);

}printf("Enter n: ");scanf("%d",&n);fprintf(fptr,"%d",n);fclose(fptr);return 0;

}

// Reading from file

#include <stdio.h>int main(){

int n;FILE *fptr;if ((fptr=fopen("C:\\program.txt","r"))==NULL){

printf("Error! opening file");exit(1);

/* Program exits if file pointer returns NULL. */}fscanf(fptr,"%d",&n);printf("Value of n=%d",n);fclose(fptr);return 0;

}

12.2 Write a program to illustrate the use of fputc(), fputs(), and fgets()

Page 32: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

Program for fgets():-#include<stdio.h>#include<stdlib.h>int main(){FILE *fp;char str[60];

/* opening file for reading */fp = fopen("file.txt" , "r");if(fp == NULL) {

perror("Error opening file");return(-1);

}if( fgets (str, 60, fp)!=NULL ) {

/* writing content to stdout */puts(str);

}fclose(fp);return(0);

}

Program for fputs():-#include<stdio.h>#include<stdlib.h>int main(){

FILE *fptr;char text[100];int i=0;clrscr();printf(“Enter a text:\n”);gets(text);if((fptr = fopen(“TEST”,”w”))==NULL)

{printf(“Cannot open file\n”);exit(1);}

fputs(text,fptr);if(fclose(fptr))

printf(“File close error\n”);getch();return 0;}

Program for fputc():-#include<stdio.h>#include<stdlib.h>int main()

{

Page 33: GTU Practical Solution Subject: Computer … · GTU Practical Solution Subject: Computer Programming and Utilization (2 110003) ... 2.3 Write a program to solve Quadratic Equation.

FILE *fptr;char text[100];int i=0;clrscr();printf(“Enter a text:\n”);gets(text);if((fptr = fopen(“TEST”,”w”))==NULL)

{printf(“Cannot open file\n”);exit(1);}

while(text[i]!=’\0’)fputc(text[i++],fptr);

if(fclose(fptr))printf(“File close error\n”);

getch();return 0;}