Vous êtes sur la page 1sur 21

1 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

GTU Practical Solution

ce_tmkodinariya

Subject: Computer Programming and Utilization (2110003)


[F.Y.B.E.: ALL BRANCHES]

PRACTICAL SET-1:

1. Write a program to print HELLO FRIENDS!


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

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


4. Write a program to solve Quadratic Equation.
5. Write a program to select & print the largest of the three nos. using Nested-If-Else statement.
Solution:
1. Write a program to print HELLO FRIENDS!
#include<stdio.h>
#include<conio.h>
void main()
{
printf("HELLO FRIENDS!");
getch();
}

2.
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" );

11/3/2016 11:27 AM

2 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

scanf(%d%d,&a,&b);

ce_tmkodinariya

printf("ARITHMETIC OPREATION\n" );
add=a+b;
sub=a-b;
mul=a*b;
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);
}

3. 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,%d days\n",d/365,(d%365)
/30,(d%365)%30);
getch();
}

4. Write a program to solve Quadratic Equation.

11/3/2016 11:27 AM

3 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

ce_tmkodinariya

#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");

root1 = ( -b + sqrt(d)) / (2* a);


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

11/3/2016 11:27 AM

4 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

5. Write a program to select & print the largest of the three nos. using Nested-If-Else statement.

ce_tmkodinariya

#include<conio.h>
#include<stdio.h>
void main()
{
int n1,n2,n3;
clrscr();

printf(" \n\n Enter three Number "


scanf("%d %d %d",&n1,&n2,&n3);

);

if((n1>n2)&&(n1>n3))
{
printf("\n\n\tMaximum No is==%d",n1);
}

else if((n2>n1)&&(n2>n3))
{
printf("\n\n\tMaximum No is ==%d",n2);
}

else
{
printf("\n\n\tMaximum No is==%d",n3);
}
getch();
}

11/3/2016 11:27 AM

5 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

ce_tmkodinariya

PRACTICAL SET-2:
1. Write a program to display multiplication table.
2. Write a program to print 1+1/2+1/3+1/4++1/N series.
3. Write a program to find sum of all integers greater than 100 & less than 200 and are divisible by
5.
4. The distance between two cities (In KM) is input through key board. Write a program to convert
and print this distance in
meters, feet, inches & centimeters.
5.

Write a program to find sum of first N odd numbers.


Ex. 1+3+5+7+..+N.
11/3/2016 11:27 AM

6 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

ce_tmkodinariya

Solution:

1.

Write a program to display multiplication table.

#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,mul;
clrscr();
printf("\n\n-------------Multiplication Table
--------\n\n");
for(i=1;i<=10;i++)
{
for(j=1;j<=10;j++)
{
mul=i*j;
printf("%d*%d=%d ",j,i,mul);
count++;
}
printf("\n");
}
getch();
}

2.

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);
11/3/2016 11:27 AM

7 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

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();
}

3.

ce_tmkodinariya

Write a program to find sum of all integers greater than 100 & less than 200 and are divisible 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
5\n");
for(i=101;i<200;i++)
{
if(i%5==0)
{
printf("%5d",i);
sum+=i;
}
}
printf("\n\nsum = %d",sum);
getch();
}

11/3/2016 11:27 AM

8 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

4. The distance between two cities (In KM) is input through key board. Write a program to convert
ce_tmkodinariya
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 = 1000m
feet= km*3280.84; //since 1km=3280.84feet
inch=km*39370.1; //since 1 km=39370.1inches
cm=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();
}

5.

Write a program to find sum of first N odd numbers.

#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)
{
11/3/2016 11:27 AM

9 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

int sum=0,i;
for(i=1;i<=m;i++)
{
if(i%2==1)
sum= sum + i;
}
return sum;
}

ce_tmkodinariya

PRACTICAL SET-3:
1.

Write a program for use of putchar( ) and getchar( ) function.

2. Program to print Patterns.


Solution:
1.

Write a program for use of putchar( ) and getchar( ) function.

#include <stdio.h>
int main( )
{
int c;
printf( "Enter a value :");
c = getchar( );
printf( "\nYou entered: ");
putchar( c );
}

return 0;

11/3/2016 11:27 AM

10 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

2.

Program to print Patterns.

ce_tmkodinariya

a) 1
b)
0
c) a
12
101
ab
123
21012
abc
1234
3210123
abcd
12345
432101234
abcde
a) Program:
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,mul,k;
clrscr();
printf("\n\n-------Pattern --------\n\n");
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
b) Program:
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,k;
clrscr();
printf("\n\n-------Pattern --------\n\n");
for(i=1;i<=10;i++)
{
for(k=10;k>=i;k--)
{
printf(" ");
}
for(j=i-1;j>=1;j--)
{
printf("%d",j);
}
11/3/2016 11:27 AM

11 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

for(j=0;j<i;j++)
{
printf("%d",j);
//
count++;
}
printf("\n");

ce_tmkodinariya

getch();
}
c) Program:
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,k,c;
clrscr();
printf("\n\n-------Pattern --------\n\n");
for(i=1;i<=10;i++)
{
for(k=10;k>=i;k--)
{
printf(" ");
}

//

for(j=1,c=97;j<i;j++,c++)
{
printf("%c ",c);
count++;
}
printf("\n\n");

getch();
}

11/3/2016 11:27 AM

12 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

ce_tmkodinariya

11/3/2016 11:27 AM

13 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

PRACTICAL SET-4:
1.
2.
3.
4.
5.
6.

Write a program to print Fibonacci series. 1,1,2,3,5,N


Write a program to reverse the digit.
Add, subtract and multiply two nos. using switch statement.
Write a program to add two matrixes.
Write a program to given no in ascending order.
W.A.P to read array of integers and print it in reverse order.

ce_tmkodinariya

Solution:
1.

Write a program to print Fibonacci series. 1,1,2,3,5,N

#include<stdio.h>

int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d",&n);

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


for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
}

2.

return 0;

Write a program to reverse the digit.

11/3/2016 11:27 AM

14 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

#include <stdio.h>

ce_tmkodinariya

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. Add, subtract and multiply two nos. using switch statement.

# include <stdio.h>
int main()
{
char o;
float num1,num2;
printf("Enter operator either + 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;
default:
/* If operator is other than +, -, * or /, error
message is shown */
printf("Error! operator is not correct");
11/3/2016 11:27 AM

15 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

break;
}
return 0;
}

ce_tmkodinariya

4. Write a program to add two matrixes.

#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]);

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


for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
printf("Sum of entered matrices:-\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
printf("%d\t", sum[c][d]);
printf("\n");
}

11/3/2016 11:27 AM

16 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

return 0;
}

ce_tmkodinariya

5. Write a program to given no in ascending order.

#include <stdio.h>
#define MAXSIZE 10

void main()
{
int array[MAXSIZE];
int i, j, num, temp;

printf("Enter the value of num \n");


scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array is \n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
/*
Bubble sorting begins */
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array is...\n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
getch();
}

11/3/2016 11:27 AM

17 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

6. W.A.P to read array of integers and print it in reverse order.

ce_tmkodinariya

#include<stdio.h>
#include<conio.h>
#define MAX 30

void main()
{
int size,i,arr[MAX];
int *ptr;
clrscr();
ptr=&arr[0];

printf("Enter the size of array : ");


scanf("%d",&size);

printf("nEnter %d integers into array:n",size);


for(i=0;i<size;i++)
{
scanf("%d",ptr);
ptr++;
}
ptr=&arr[size-1];

printf("nElements of array in reverse order are:n");


for(i=size-1;i>=0;i--)
{
printf("nElement%d is %d :",i,*ptr);
ptr--;
}
getch();
}

PRACTICAL SET-5:
1.

Write a program to count total words in text.

2.

Find length of string using

3.

Write a program to copy one string to another string.

strlen ( ) function.

11/3/2016 11:27 AM

18 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

4.

Write a program to join two strings.

5.

Write a program convert character into Toggle character.

6.

Find given string is palindrome or not using string library function.

ce_tmkodinariya

Solution:
1.

Write a program to count total words in text.

#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);
getch();
}

2.

Find length of string using

strlen ( ) function.

#include<string.h>
#include<stdio.h>
#include<conio.h>
11/3/2016 11:27 AM

19 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

void main()
{
int strlength;
char *str;
clrscr();
printf("\nEnter the string: ");
gets(str);
strlength=strlen(str);
printf("\nThe length of the string is %d.",strlength);
getch();
}

3.

ce_tmkodinariya

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: ");
scanf("%s",s1);
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
printf("String s2: %s",s2);
return 0;
}

4.

Write a program to join two strings.

#include <stdio.h>
int main()
{
char s1[100], s2[100], i, j;
printf("Enter first string: ");
scanf("%s",s1);
printf("Enter second string: ");
scanf("%s",s2);
for(i=0; s1[i]!='\0'; ++i); /* i contains length of
string s1. */
for(j=0; s2[j]!='\0'; ++j, ++i)
{
s1[i]=s2[j];
}
s1[i]='\0';
11/3/2016 11:27 AM

20 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

5.

printf("After concatenation: %s",s1);


return 0;

ce_tmkodinariya

Write a program convert character into Toggle character.

#include<stdio.h>
int main()
{
char str[100];
int i;
printf("Please enter a string: ");
// gets(str);
// fgets is a better option over gets to read multiword
string .
fgets(str, 100, stdin);
// Following can be added for extra precaution for '\n'
character
//
if(str[length(str)-1]
==
'\n')
str[strlen(str)1]=NULL;
for(i=0;str[i]!=NULL;i++)
{
if(str[i]>='A'&&str[i]<='Z')
str[i]+=32;
else if(str[i]>='a'&&str[i]<='z')
str[i]-=32;
}
printf("String in toggle case is: %s",str);
return 0;
}

6.

Find given string is palindrome or not using string library function.

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

void main()
{
char string[25], reverse_string[25] = {'\0'};
11/3/2016 11:27 AM

21 of 21

http://chintandave.in/cpu/gtupracticals1to5.html

int i, length = 0, flag = 0;

ce_tmkodinariya

printf("Enter a string \n");


gets(string);
/* keep going through each character of the string
till its end */
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of the string '%s' = %d\n", string,
length);
for (i = length - 1; i >= 0 ; i--)
{
reverse_string[length - i - 1] = string[i];
}
/* Check if the string is a Palindrome */
for (flag = 1, i = 0; i < length ; i++)
{
if (reverse_string[i] != string[i])
flag = 0;
}
if (flag == 1)
printf ("%s is a palindrome \n", string);
else
printf("%s is not a palindrome \n", string);
}

11/3/2016 11:27 AM

Vous aimerez peut-être aussi