Upload
metalwihen
View
235
Download
0
Embed Size (px)
Citation preview
8/2/2019 Sem 1: Computer Practical
1/23
1
8/2/2019 Sem 1: Computer Practical
2/23
2
(Basics)1. WAP to print text on the screen.2. WAP to check if number is odd or even.3. WAP to check which of the two entered numbers is greater.4. WAP to check whether entered number is a leap year or not.5. WAP to convert temperature in Fahrenheit to Celsius.
(Looping)6. WAP to display the multiplication table of entered number.7. WAP to check if number is Palindrome or not.8. WAP to check if number is an Angstrom number or not.9. WAP to check if number is a prime or not.10. WAP to find the sum of Fibonacci series.
(Arrays)11. WAP to find the sum and average of 10 numbers using arrays.12. WAP to add elements of two 3x3 matrices.
(Pointers & Functions)13. WAP to understand pointers.14. WAP to swap two numbers.15. WAP to Swap two numbers using pointers (and functions).16. WAP to find the factorial of a number using recursive function.
(Structures)17. WAP using structures to input and display details of student.
(Strings)
18. WAP to enter two strings and get them to display.
(Data File Handling)19. WAP to enter and retrieve a character from a file.20. WAP to copy the contents of one file to another.
34567
89
101112
1314
16171819
20
21
2223
8/2/2019 Sem 1: Computer Practical
3/23
3
Write a Program to display text on screen.
SOURCE CODE:
1
2
3
4
5
6
7
#include
int main()
{
printf(" Welcome to Neil's Practical File. XD ");
return0;
}
OUTPUT:
Welcome to Neil's Practical File. XD
8/2/2019 Sem 1: Computer Practical
4/23
4
Write a Program to check if number is odd or even.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include
int main()
{
int num;
printf(" Enter the number: ");
scanf(" %d ",&num);
if(num%2==0)
printf("\n Number is Even. ");
else
printf("\n Number is Odd. ");
return0;
}
OUTPUT:
Enter the number: 5
Number is Odd.
Enter the number: 8
Number is Even.
8/2/2019 Sem 1: Computer Practical
5/23
5
Write a Program to check which of the two enterednumbers is greater.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include
main ()
{
int a,b,c;
printf("\n Enter two Numbers: ");
scanf("%d%d",&a,&b);
c=a ? b:a ;
printf("\n Greater number of the two is : %d \n",c);
}
OUTPUT:
Enter two Numbers: 5 357
Greater number of the two is : 357
8/2/2019 Sem 1: Computer Practical
6/23
6
Write a Program to check whether entered number is aleap year or not.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
1112
13
14
15
#include
main()
{
int Y;
printf("Enter any year: ");
scanf("%d",&Y);
if((Y%4==0))
printf("\nThe entered year is a Leap Year.");
elseprintf("\nThe entered year is not a Leap Year.");
return0;
}
OUTPUT:
Enter any year: 1991
The entered year is not a Leap Year.
Enter any year: 1994
The entered year is a Leap Year.
8/2/2019 Sem 1: Computer Practical
7/23
7
Write a Program to convert temperature in Fahrenheitto Celsius.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
11
1213
14
15
#include
int main()
{
float Tf, Tc;
printf("Enter temperature in Farenheit: ");
scanf("%f",&Tf);
Tc=((Tf-32)*(5.0/9.0));
printf("The temperature in Celcius is %f", Tc);
return0;
}
OUTPUT:
Enter temperature in Farenheit: 98.6
The temperature in Celcius is 37.000000
8/2/2019 Sem 1: Computer Practical
8/23
8
Write a Program to display the multiplication table ofentered number.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
1112
13
14
15
16
#include
int main()
{
int i,n,a;
printf("\nEnter the number whose table is to be printed : ");
scanf("%d",&n);
for(i=1;i
8/2/2019 Sem 1: Computer Practical
9/23
9
Write a Program to check if number is Palindrome ornot.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
11
1213
14
15
16
17
18
19
20
21
22
23
24
2526
27
28
29
30
31
32
33
#include
main()
{
int num; //number inputted by user
int n; //temporary storage
int rev=0;//stores the reverse of that number
int digit;//stores the individual digits
printf(" Enter the number to check if it's Palindrome or not:
");scanf("%d", &num);
n=num;
do
{
digit=n%10;
rev=(rev*10)+digit;
n=n/10;
}
while(n>0);
if(rev==num){
printf("\n The number is a palindrome. ");
}
else
{
printf("\n The number is not a palindrome. ");
}
}
OUTPUT:
Enter the number to check if it's Palindrome or not: 121
The number is a palindrome.
Enter the number to check if it's Palindrome or not: 153
The number is not a palindrome.
8/2/2019 Sem 1: Computer Practical
10/23
10
Write a Program to check if number is an Angstromnumber or not.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
11
1213
14
15
16
17
18
19
20
21
22
23
24
2526
27
28
29
30
31
32
33
#include
main()
{
int num; //number inputted by user
int n; //temporary storage
int A=0;//stores the sum of the cube of the digits
int digit;//stores the individual digits
printf(" Enter the number to check if it's Angstrom or not:
");
scanf("%d", &num);
n=num;
do
{
digit=n%10;
A+=(digit*digit*digit);
n=n/10;
}
while(n>0);
if(A==num)
{printf("\n The number is an Angstrom number. ");
}
else
{
printf("\n The number is not an Angstrom number. ");
}
}
OUTPUT:
Enter the number to check if it's Angstrom or not: 121
The number is not an Angstrom number.
Enter the number to check if it's Angstrom or not: 153
The number is an Angstrom number.
8/2/2019 Sem 1: Computer Practical
11/23
11
Write a Program to check if number is a prime or not.
SOURCE CODE:
1
23
4
5
6
7
8
9
10
11
12
13
14
1516
17
18
19
20
21
22
23
24
25
26
27
2829
30
31
32
33
34
35
#include
int main()
{
int num;
printf("\n Enter the number to check whether prime or not:
");
scanf(" %d ",&num);
if( num==0|| num==1)
{
printf("\n The number is not prime. ");
}
else
{int i=0;
int flag=1;// 1 if prime, 0 if not prime
for(i=2; i
8/2/2019 Sem 1: Computer Practical
12/23
12
Write a Program to find the sum of Fibonacci series.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
11
12
13
1415
16
17
18
19
20
21
22
23
24
25
26
2728
29
30
31
32
33
34
35
36
37
38
39
4041
42
43
#include
main()
{
int sum=0;
int n;
printf("\n Till which term should the Fibonacci series be
found: ");
scanf(" %d ", &n);
printf("\n The Fibonacci Series: \n ");
if( n==0)
{
printf(" None ");
}
elseif( n==1)
{
printf("0");
}
else
{
int i,temp;
int first=0,second=1;
//2nd term
printf("%d + %d ",first,second);
sum+=second;
for(i=3; i
8/2/2019 Sem 1: Computer Practical
13/23
13
Write a Program to find the sum and average of 10numbers using arrays.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
11
1213
14
15
16
17
18
19
20
21
22
23
24
2526
27
28
29
30
31
32
33
34
#include
int main()
{
//INITIAL DECLARATIONS
int x[10];
int sum=0;
float avg;
int i;
//INPUT
printf("\n Enter the 10 numbers: \n ");
for(i=0; i
8/2/2019 Sem 1: Computer Practical
14/23
14
Write a Program to add elements of two 3x3 matrices.
SOURCE CODE:
1
23
4
5
6
7
8
9
10
11
12
13
14
1516
17
18
19
20
21
22
23
24
25
26
27
2829
30
31
32
33
34
35
36
37
38
39
40
4142
43
44
45
46
47
48
49
50
51
52
53
54
#include
int main()
{
int A[3][3], B[3][3];
int i,j;
//INPUT of MATRIX/ 2D ARRAY A
printf("\n Enter the elements of matrix A: \n");
for(i=0; i
8/2/2019 Sem 1: Computer Practical
15/23
15
OUTPUT:
Enter the elements of matrix A:1 2 3
1 2 3
3 2 1
Enter the elements of matrix B:
3 2 1
3 2 1
1 2 3
Adding matrices A and B = matrix C
Matrix C is:4 4 4
4 4 4
4 4 4
8/2/2019 Sem 1: Computer Practical
16/23
16
Write a Program to understand pointers.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
11
12
13
1415
16
17
18
#include
main()
{
int a,*p;
printf("Enter an integer: ");
scanf("%d",&a);
p =&a;
printf("\nAddress of a: %u",&a);
printf("\n\nAddress of p: %u",&p);
printf("\n\nValue of p: %d", p);printf("\n\nValue of a: %d", a);
printf("\n\nValue of a: %d",*p);
}
OUTPUT:
Enter an integer: 7
Address of a: 3219592736
Address of p: 3219592732
Value of p: -1075374560
Value of a: 7
Value of a: 7
8/2/2019 Sem 1: Computer Practical
17/23
17
Write a Program using structures to input and displaydetails of student.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
11
1213
14
15
16
17
18
19
20
21
22
23
24
#include
struct student
{
char name[20];
float marks;
int age;
}b1, b2;
main(){
printf("\n Enter Names, Marks & Age of the students:
\n");
scanf("%s %f %d",&b1.name,&b1.marks,&b1.age);
scanf("%s %f %d",&b2.name,&b2.marks,&b2.age);
printf("\n This is what you have entered: \n");
printf("\n %s %f %d ",b1.name,b1.marks,b1.age);
printf("\n %s %f %d ",b2.name,b2.marks,b2.age);
}
OUTPUT:
Enter Names, Marks & Age of the students:
Neil 94 18
Bugger 0 100
This is what you have entered:
Neil 94.000000 18
Bugger 0.000000 100
8/2/2019 Sem 1: Computer Practical
18/23
18
Write a program to swap two numbers.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
11
12
13
1415
16
17
18
19
20
21
22
23
24
25
26
2728
#include
int main()
{
int a,b;
printf("\n Enter the value of A: ");
scanf("%d",&a);
printf(" Enter the value of B: ");
scanf("%d",&b);
//Swapping:
int temp;
temp=a;
a=b;
b=temp;
printf("\n After Swapping... \n");
printf(" A: %d", a);
printf("\n B: %d", b);
return0;}
OUTPUT
Enter the value of A: 5
Enter the value of B: 10
After Swapping...
A: 10B: 5
8/2/2019 Sem 1: Computer Practical
19/23
19
Write a Program to Swap two numbers using pointers.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
11
12
1314
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include
void Swap(int*a, int*b)
{ // *Note the syntax of pointers for arguments
//Swapping:
int temp;
temp=*a; // *(pointer_variable) to access the value
*a=*b;
*b=temp;
}
int main()
{
int a,b;
printf("\n Enter the value of A: ");
scanf("%d",&a);
printf(" Enter the value of B: ");
scanf("%d",&b);
Swap(&a,&b); // *Note the & during such a call
printf("\n After Swapping... \n");
printf(" A: %d", a);
printf("\n B: %d", b);
return0;
}
OUTPUT:
Enter the value of A: 4
Enter the value of B: 9
After Swapping...
A: 9
B: 4
8/2/2019 Sem 1: Computer Practical
20/23
20
Write a Program to find the factorial of a number usingrecursive function.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
1112
13
14
15
16
17
18
19
20
21
22
23
2425
26
27
28
29
#include
int fact(int f)
{
if( f==1|| f==0)
{return1;}
else
{
f=f*fact(f-1);
return f;}
}
main()
{
int num;
int factorial;
printf(" Enter the number whose factorial needs to be found:
");
scanf(" %d ", &num);
factorial=fact(num);
printf("\n The factorial of %d is %d. ",num,factorial);
}
OUTPUT:
Enter the number whose factorial needs to be found: 0
The factorial of 0 is 1.
Enter the number whose factorial needs to be found: 4
The factorial of 4 is 24.
8/2/2019 Sem 1: Computer Practical
21/23
21
Write a Program to enter two strings and get them todisplay.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
11
12
1314
15
16
17
18
19
20
21
22
23
#include
int main()
{
char S1[20];
char S2[20];//the datatype string doesn't exist in C.
//method 1: can only take one word at a time (till spaces).
printf("\n Enter the first string: ");
scanf(" %s ", S1);
//method 2: can take whole line including spaces.printf(" Enter the second string: ");
gets(S2);
printf("\n The Strings are:\n 1st: %s\n 2nd: ",S1);
puts(S2);
return0;
}
OUTPUT:
Enter the first string: Hello
Enter the second string: World
The Strings are:
1st: Hello
2nd: World
Enter the first string: New YorkEnter the second string:
The Strings are:
1st: New
2nd: York
8/2/2019 Sem 1: Computer Practical
22/23
22
Write a Program to enter and retrieve a character froma file using DATA FILE HANDLING.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
11
12
1314
15
16
17
18
19
20
#include
main()
{
char ch;
FILE *file;
file = fopen("abc.txt","w");
printf("\nEnter any character into FILE: ");
scanf("%c",&ch);
putc(ch, file);
fclose(file);
file = fopen("abc.txt","r");
ch = getc(file);
printf("\nEntered Character from FILE: %c", ch);
}
OUTPUT:
Enter any character into FILE: Y
Entered Character from FILE: Y
8/2/2019 Sem 1: Computer Practical
23/23
Write a program to copy the contents of one file toanother using DATA FILE HANDLING.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
11
1213
14
15
16
17
18
19
#include
main()
{
char ch;
FILE *fp1,*fp2;
fp1 = fopen("abc.txt","r");
fp2 = fopen("xyz.txt","w");
while((ch = getc(fp1))!= EOF)putc(ch, fp2);
fclose(fp1);
fclose(fp2);
return0;
}
OUTPUT:
(none)