Cpu P1top5
Cpu P1top5
Cpu P1top5
http://chintandave.in/cpu/gtupracticals1to5.html
ce_tmkodinariya
PRACTICAL SET-1:
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;
#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();
}
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;
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();
);
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.
6 of 21
http://chintandave.in/cpu/gtupracticals1to5.html
ce_tmkodinariya
Solution:
1.
#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.
#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.
#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.
#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.
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.
ce_tmkodinariya
Solution:
1.
#include<stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d",&n);
2.
return 0;
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;
return 0;
# 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
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10],
sum[10][10];
11/3/2016 11:27 AM
16 of 21
http://chintandave.in/cpu/gtupracticals1to5.html
return 0;
}
ce_tmkodinariya
#include <stdio.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, j, num, temp;
11/3/2016 11:27 AM
17 of 21
http://chintandave.in/cpu/gtupracticals1to5.html
ce_tmkodinariya
#include<stdio.h>
#include<conio.h>
#define MAX 30
void main()
{
int size,i,arr[MAX];
int *ptr;
clrscr();
ptr=&arr[0];
PRACTICAL SET-5:
1.
2.
3.
strlen ( ) function.
11/3/2016 11:27 AM
18 of 21
http://chintandave.in/cpu/gtupracticals1to5.html
4.
5.
6.
ce_tmkodinariya
Solution:
1.
#include <stdio.h>
#include <string.h>
void main()
{
char s[200];
int count = 0, i;
2.
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
#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.
#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.
ce_tmkodinariya
#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.
#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
ce_tmkodinariya
11/3/2016 11:27 AM