You are on page 1of 23

1

(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.

3 4 5 6 7

8 9 10 11 12

13 14

16 17 18 19

20

21

22 23

Write a Program to display text on screen.


SOURCE CODE:

1 2 3 4 5 6 7

#include<stdio.h> int main() { printf(" Welcome to Neil's Practical File. XD "); return 0; }

OUTPUT: Welcome to Neil's Practical File. XD

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<stdio.h> 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. "); return 0; }

OUTPUT:

Enter the number: 5 Number is Odd. Enter the number: 8 Number is Even.

Write a Program to check which of the two entered numbers is greater.


SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> 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

Write a Program to check whether entered number is a leap year or not.


SOURCE CODE:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

#include <stdio.h> main() { int Y; printf("Enter any year: "); scanf("%d", &Y); if((Y%4==0)) printf("\nThe entered year is a Leap Year."); else printf("\nThe entered year is not a Leap Year."); return 0; }

OUTPUT: Enter any year: 1991 The entered year is not a Leap Year. Enter any year: 1994 The entered year is a Leap Year.

Write a Program to convert temperature in Fahrenheit to Celsius.


SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <stdio.h> 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); return 0; }

OUTPUT: Enter temperature in Farenheit: 98.6 The temperature in Celcius is 37.000000

Write a Program to display the multiplication table of entered number.


SOURCE CODE:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

#include<stdio.h> int main() { int i,n,a; printf("\nEnter the number whose table is to be printed : "); scanf("%d",&n); for(i=1;i<11;i++) { a=n*i; printf("%dx%i=%d \n",n,i,a); } return 0; }

OUTPUT:

Enter the number whose table is to be printed : 5 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 5x6=30 5x7=35 5x8=40 5x9=45 5x10=50

Write a Program to check if number is Palindrome or not.


SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include <stdio.h> main() { int int int int num; //number inputted by user n; //temporary storage rev=0; //stores the reverse of that number 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.

Write a Program to check if number is an Angstrom number or not.


SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include <stdio.h> 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.

10

Write a Program to check if number is a prime or not.


SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include<stdio.h> 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<num; i++) { if(num%i==0) { flag=0; break; } } if(flag==1) { printf("\n The number is prime. "); } else { printf("\n The number is not prime "); } }return 0; }

OUTPUT: Enter the number to check whether prime or not: 1 The number is not prime. Enter the number to check whether prime or not: 5 The number is prime. Enter the number to check whether prime or not: 12 The number is not prime.

11

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 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include<stdio.h> 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 "); } else if( 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<=n; i++) //3rd term onwards { temp=second; second=second+first; first=temp; printf("+ %d ",second); sum+=second; } } printf("\n Sum = %d",sum); }

OUTPUT: Till which term should the Fibonacci series be found: 8 The Fibonacci Series: 0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 Sum = 33

12

Write a Program to find the sum and average of 10 numbers using arrays.
SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include <stdio.h> 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<10; i++) { scanf(" %d ", &x[i]); // *Note the & for array element } //CALCULATIONS for(i=0; i<10; i++) { sum+=x[i]; } avg=sum/10.0; //OUTPUT printf("\n The Sum of the numbers: %d ",sum); printf("\n The Average of the numbers: %f \n",avg); return 0; }

OUTPUT: Enter the 10 numbers: 1 2 3 4 5 6 7 8 9 10 The Sum of the numbers: 55 The Average of the numbers: 5.500000

13

Write a Program to add elements of two 3x3 matrices.


SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include<stdio.h> 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<3; i++) { for(j=0; j<3; j++) { scanf(" %d ", &A[i][j]); } } //INPUT of MATRIX/ 2D ARRAY B printf("\n Enter the elements of matrix B: \n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { scanf(" %d ", &B[i][j]); } } //ADDING THE TWO MATRICES printf("\n Adding matrices A and B = matrix C "); int C[3][3]; for(i=0; i<3; i++) { for(j=0; j<3; j++) { C[i][j]=A[i][j]+B[i][j]; } } //OUTPUT printf("\n Matrix C is: \n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { printf(" %d ", C[i][j]); } printf("\n"); } return 0; }

14

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

15

Write a Program to understand pointers.


SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <stdio.h> 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

16

Write a Program using structures to input and display details of student.


SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <stdio.h> 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

17

Write a program to swap two numbers.


SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include<stdio.h> 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); return 0; }

OUTPUT Enter the value of A: 5 Enter the value of B: 10 After Swapping... A: 10 B: 5

18

Write a Program to Swap two numbers using pointers.


SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include<stdio.h> void Swap(int *a, int *b) { // *Note the syntax of pointers for arguments //Swapping: int temp; temp=*a; *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); return 0; } // *(pointer_variable) to access the value

OUTPUT: Enter the value of A: 4 Enter the value of B: 9 After Swapping... A: 9 B: 4

19

Write a Program to find the factorial of a number using recursive function.


SOURCE CODE:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

#include<stdio.h> int fact(int f) { if( f==1 || f==0) { return 1; } 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.

20

Write a Program to enter two strings and get them to display.


SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <stdio.h> 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); return 0; }

OUTPUT: Enter the first string: Hello Enter the second string: World The Strings are: 1st: Hello 2nd: World Enter the first string: New York Enter the second string: The Strings are: 1st: New 2nd: York

21

Write a Program to enter and retrieve a character from a file using DATA FILE HANDLING.
SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <stdio.h> 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

22

Write a program to copy the contents of one file to another using DATA FILE HANDLING.
SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <stdio.h> 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); return 0; }

OUTPUT: (none)

23

You might also like