11 November 2022

Array

 

Array:

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

                int i, no[10];

                clrscr();

                printf(“Enter values in an array”);

                printf(“--------------------------------“);

 

                // Input/ Scan the numbers

                for(i=0; i<10; i++)

                {

                printf(“Enter value %d”, i+1);

                scanf(“%d”, &no[i]);

}

 

// Output/ Print the numbers

for(i=0; i<10; i++)

                {

                printf(“Value %d is %d”, i+1, no[i]);         

}

                getch();

}

 

 

Exercise:

1. Define an array of 5 employee. Enter the id number of 5 employee and print it.

2. Define an array of 5 students. Enter the percentage of 5 studnets and print it.

 

08 November 2022

C Programming Language Function Program

                                   Exercise Date: 08/11/2022

 

                                        For A&B Division                          

                                       Function Programs

 

Sr. No.

Program Definition

1

Define function with example. 

2

Define more than one function in one program.

3

Function calling function demo.

4

Recursion. Direct type demo.

5

Recursion. Indirect type demo.

6

Addition of two number using function.

(without return value, without argument)

7

Addition of two number using function.

(without return value, with argument)

8

Addition of two number using function.

(with return value, without argument)

9

Addition of two number using function.

(with return value, with argument)

10

Star print with function.

11

Function pattern print.

12

No return value, with argument: addition of three numbers

13

No return value, with argument: reverse number

14

No return value, with argument: square of number

15

No return value, No argument: swapping of two number

16

No return value, with argument: swapping of two number

17

with return value, no argument .. square of number

18

with return value, without argument ..  addition of two number

19

with return value, without argument .. reverse number

20

With return value, with argument .. square of number

21

With return value, with argument .. addition of two number

22

with return value, with argument .. reverse number

23

with return value, No argument: swapping of two number

24

with return value, with argument: swapping of two number

25

With return value, with argument: multiplication of three numbers

       

C Function Program Solutions 

===============================================

12 March 2022

Today's C File Program

File Program

#include<stdio.h>

#include<conio.h>

void main()

{

FILE *fp1, *fp2, *fp3;


fp1 = fopen("1.txt", "w");

fp2 = fopen("2.ppp", "w");

fp3 = fopen("3.xyz", "w");


fprintf(fp1, "Welcome to BCA");

fprintf(fp2, "Gujarat");

fprintf(fp3, "University");


fcloseall();

}


Header File Program

#include<stdio.h>

#include<conio.h>

#include"reverse.c"


void main()

{

long int no;

clrscr();


printf("\nEnter number: ");

scanf("%ld", &no);


printf("\n\nReverse number = %ld", reverse(no)) ;


getch();

}

Header File Name. reverse.c

long reverse(long int no)

{

long revno=0;

int rem;


while(no > 0)

{

rem = no % 10;

revno = revno * 10 + rem;

no = no / 10;

}


return revno;

}

04 February 2022

C Practical Unit 4

 

ASSIGNMENT   :  4

 

1 Display this kind of output on screen.

C

CP

CPR

. .

 CPROGRAMING

. .

CPR

CP

C

 

2 Write a program which will take 10 numbers from user and stored it in the array. It will print all the numbers, their sum and average of it.

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int a[10] , i  ;

        float avg , sum=0 , no ;

        clrscr();

        printf("Enter Limit of Array :  ");

        scanf("%f",&no);

 

        for(i=0 ; i<no ; i++)

        {

                printf("\nEnter Element %d  :  ",i+1);

                scanf("%d",&a[i]);

        }

 

        printf("\nEnter Elements in Array are :  ");

 

        for(i=0 ; i<no ; i++)

        {

                printf("%d ",a[i]);

                sum=sum+a[i];

        }

 

        avg = sum/no;

 

        printf("\n\nSum = %.2f \n Average = %.2f",sum,avg);

 

        getch();

}

3 Write a program to find binary of given number.

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

        long  no , decino , rem , base=1 , binary = 0 ;

        clrscr();

 

        printf("\nEnter a Decimal Integer :  ");

        scanf("%ld",&no);

 

        decino=no;

 

        while(no>0)

        {

                rem = no % 2 ;

                binary = binary + rem * base ;

                no = no / 2 ;

                base = base * 10 ;

        }

 

        printf("\n\nEntered number is  :  %d",decino);

        printf("\n\nIts Binary number is  :  %ld",binary);

 

        getch();

}

 

4 Write a program to sort and array.

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int size , a[10] , i , j , temp ;

        clrscr();

 

        printf("Enter Limit of Array  :  ");

        scanf("%d",&size);

 

        for(i=0 ; i<size ; i++)

        {

                printf("\nEnter Element %d  :   ",i+1);

                scanf("%d",&a[i]);

        }

 

        for(i=0 ; i<size ; i++)

        {

                for(j=0 ; j<size ; j++)

                {

                        if(a[j]>a[j+1])

                        {

                                temp = a[j] ;

                                a[j] = a[j+1] ;

                                a[j+1] = temp ;

                        }

                }

        }

        printf("\nSorted Elements \n ");

        for(i=0 ; i<size ; i++)

        {

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

        }

        getch();

}

 

5 Write a program to search an element from the array.

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int a[10] , size , i , search , flag ;

        clrscr();

 

        printf("\nEnter number of Elements :  ");

        scanf("%d",&size);

 

        for(i=0 ; i<size ; i++)

        {

                printf("Enter Element %d  :  ",i+1);

                scanf("%d",&a[i]);

        }

        printf("\n\n\nEnter value you want to find :  ");

        scanf("%d",&search);

 

        flag=0;

 

        for(i=0 ; i<size ; i++)

        {

                if(a[i]==search)

                {

                        flag=1;

                        printf("\n%d  is found at location %d",search,i+1);

                        break;

//remove break if you want to search same elements like 11, 11,11 at  different places

                }

        }

        if(flag==0)

        {

                printf("\n%d is not found in the array  ",search);

        }

        getch();

}

 

6 Write a program to find addition of two matrices of 3*3.

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int first[10][10] , second[10][10] , sum[10][10] ;

        int m , n , i , j ;

        clrscr();

 

        printf("\nEnter the Number of Rows in Matrix  :  ");

        scanf("%d",&m);

 

        printf("\nEnter the Number of columns in Matrix  :  ");

        scanf("%d",&n);

 

        printf("\nEnter the Elements of 1st Matrix  :  \n");

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

        {

                for(j=0 ; j<n ; j++)

                {

                        scanf("\t%d",&first[i][j]);

                }

        }

        printf("\nEnter the Elements of 2nd Matrix  :  \n");

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

        {

                for(j=0 ; j<n ; j++)

                {

                        scanf("\t%d",&second[i][j]);

                }

        }

        printf("\n\nSum of entered Matrix  :   \n");

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

        {

                for(j=0 ; j<n ;j++)

                {

                        sum[i][j] = first[i][j] + second[i][j];

                        printf("%d\t",sum[i][j]);

                }

                printf("\n");

        }

        getch();

}

 

 

7 Write a program to find multiplication of two matrices of 3*3.

 

       

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int a[10][10] , b[10][10] , mul[10][10];

        int m , n , p , q , i , j , k , sum=0;

        clrscr();

 

        printf("\nEnter the Number of Rows in Matrix  :  ");

        scanf("%d",&m);

 

        printf("\nEnter the Number of columns in Matrix  :  ");

        scanf("%d",&n);

 

        printf("\nEnter the Elements of 1st Matrix  :  \n");

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

        {

                for(j=0 ; j<n ; j++)

                {

                    printf("Enter the Element %d  :  ",i+1);

                    scanf("%d",&a[i][j]);

                }

        }

        printf("\nEnter the number of Rows in 2nd Matrix  :  \n");

        scanf("%d",&p);

 

        printf("Enter the number of Columns in 2nd Matrix  :   ");

        scanf("%d",&q);

 

        if(n!=p)

        {

                printf("\nCan not Multiplied with each other \n");

        }

        else

        {

                printf("\nEnter the elements of the 2nd Matrix :  \n");

        }

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

        {

                for(j=0 ; j<n ; j++)

                {

                        printf("Enter the element %d  :  ",i+1);

                        scanf("%d",&b[i][j]);

                }

        }

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

        {

                for(j=0 ; j<q ; j++)

                {

                        for(k=0 ; k<p ; k++)

                        {

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

                        }

                        mul[i][j] = sum;

                        sum = 0;

                }

        }

        printf("\nMultiplication of entered of Matriced :   \n");

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

        {

                for(j=0 ; j<n ; j++)

                {

                        printf("%d\t",mul[i][j]);

                }

                printf("\n");

        }

        getch();

}

 

8 Take two strings from the user and check whether the string is palindrome or not

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int len=0 , i , j , flag=0 ;

        char s[10];

        clrscr();

 

        printf("\nEnter value  :  ");

        gets(s);

        puts(s);

 

        while(s[len]!=NULL)

        {

                len++;

        }

 

        for(i=0 , j=len-1 ; i<len/2 ; i++ , j--)

        {

                if(s[i]! = s[j])

                flag=1;

                break;

        }

        if(flag==1)

        {

                printf("\n\nNumber is not Palindrome");

        }

        else

        {

                printf("\n\nNumber is  Palindrome");

        }

        getch();

}      

 

9 Write a program to find sum, average of two numbers passed to user defined functions called sum(int,int) and average(int,int)

 

 

#include<stdio.h>

#include<conio.h>

 

int sum(int , int);

int average(int ,int);

 

void main()

{

        int a , b , add , avg ;

        clrscr();

 

        printf("\n\nEnter value of 1st Number :   ");

        scanf("%d",&a);

 

        printf("\n\nEnter value of 2nd Number :  ");

        scanf("%d",&b);

 

        add = sum(a,b);

        printf("\n\nSum  =  %d",add);

 

        avg=average(a,b);

        printf("\n\n\nAverage  =  %d",avg);

 

        getch();

}

 

int sum(int a , int b)

{

        int sum = a + b ;

        return(sum);

}

 

int average(int a , int b)

{

        int avg = (a + b)/2;

        return(avg);

}

 

 

 10 Write a program to print factorial of a given number by recursive user defined function fact(int).

 

       

#include<stdio.h>

#include<conio.h>

 

int fact(int);

 

void main()

{

        int no , f ;

        clrscr();

 

        printf("\n Enter a Number to find Factorial  :   ");

        scanf("%d",&no);

 

        f =  fact(no);

        printf("\n  %d ! = %d\n",no,f);

 

        getch();

}

 

int fact(int no)

{

        if(no==0)

        {

                return 1;

        }

        else

        {

                return (no*fact(no-1));

        }

}

 

 

11 Write a program to print Fibonacci series using recursive UDF.

 

#include<stdio.h>

#include<conio.h>

 

void fibo(int n)

{

        int c , a=0 , b=1 , i ;

 

        printf("%d\t%d\t",a,b);

 

        c = a + b ;

        printf("%d\t",c);

 

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

        {

                a = b ;

                b = c ;

                c = a + b ;

                printf("%d\t",c);

        }

}

 

void main()

{

        clrscr();

        printf("\t\t\t\tFibonacci Series \n\n\n");

        fibo(7);

        getch();

}

 

 

 

12 Write a program to find length of the given string (without including string.h).

 

 

 

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int i , j , len=0;

        char s[10] ,  s2[10];

        clrscr();

 

        printf("\nEnter the data you want to check its length  :  ");

 

        gets(s);

        puts(s);

 

        while(s[len]!='\0')

        {

                len++;

        }

        for(i=0 , j=len-1 ; i<len ; i++ , j--)

        {

                s2[j]=s[i];

        }

        printf("%s",s2);

        printf("\n");

        printf("\nString length is  %d",len);

 

        getch();

}

 

14 Write a program to convert lowercase string to uppercase string (without including string.h).

 

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

        char str[100];

        clrscr();

 

        printf("\nEnter a String in Lower Case :  ");

        gets(str);

 

        printf("\nYour  String in Upper Case :  %s",strupr(str));

 

        getch();

}

 

 

15 Write a program which will accept two strings from the user and print the message that the strings are same or not.

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

       char str1[20] , str2[20] , check ;

       clrscr();

 

       printf("\nEnter the First String :   ");

       gets(str1);

 

       printf("\nEnter the Second String :   ");

       gets(str2);

 

       check = strcmp(str1,str2);

 

       if(check==0)

       {

                printf("\nString is Same");

       }

       else

       {

                printf("\nString is not  Same");

       }

 

       getch();

}

 

 

 

16 Write a program which take a lowercase string from the user and print its length and uppercase string.

 

 

 

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

        char str[80];

        int length;

        clrscr();

 

        printf("\nEnter the String in Lower Case  :  ");

        gets(str);

 

        length = strlen(str);

 

        printf("\n\nYour String in Upper Case :  %s",strupr(str));

        printf("\n\n\nYour String in Length :  %d",length);

 

        getch();

}

 

 

 

 

17 Write a program that uses function digit(N,k) that return the value of the kth digit from the right of the number N. For eg. The function call digit (254693,2) should return 9

 

 

 

 

 

18 Program to find if the given no. is prime or not. The function should accept the number as argument and return if the no. is prime or not

 

 

 

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int no , i , a=0 ;

        clrscr();

 

        printf("\n\nEnter the Number :  ");

        scanf("%d",&no);

 

        for(i=2 ; i<no ; i++)

        {

                if(no%i==0)

                {

                        a++;

                }

        }

        if(a==0)

        {

                printf("\n\n%d is Prime Number ",no);

        }

        else

        {

                printf("\n\n%d is not Prime Number ",no);

        }

        getch();

}