Thursday, 13 December 2018

O Level C Language Imp Programe

C Language Programe1.Find Array greatest number?
        #include<stdio.h>                                                                              
            #include<conio.h>
            void main()
{
            int a[5], i, max=0;
            clrscr();
            printf(“Enter the number =”);
             for(i=0;i<5;i++)
                  {
                       scanf(“%d”,&a);
                  }
            for(i=0;i<5;i++)
                 {
                     if(max<a[i])
                     max=a[i];
                }
printf(“Greatest number=%d”,max);
             getch();
}
            Output:-
Enter the number= 1,4,6,7,2
Greatest number=7

Q.2.Function to print given number of terms of fibonacci series?
            #include<stdio.h>
            #include<conio.h>
            void main()
{
            int  n,  a=0,  b=1,  i, c;
            clrscr();
            printf(“Enter the number = “);
            scanf(“%d”,&n);
                      for(i=0; i<n; i++)
                          {
                              printf(“%d”,a);
                              c=a+b;
                              a=b;
                              b=c;
                          }
                        getch();
}
                        Output:-
Enter the number= 5
                        0 1 1 2 3

Q.3.Without use library function, reverse string ?
            #include<stdio.h>
            #include<conio.h>
            #include<string.h>
            void main()
{
            char a[100];
            int  len,i;
            printf(“Enter String = “);
            gets(a);
            len=strlen(a);
            for(i=len-1; i>0; i--)
                    {
                            printf(“%c”, a[i]);
                     }
                            getch();
}
                        Output:-
Enter String= SUJEET
                        TEEJUS         


Q.4. Read line and print revers number ?
            #include<stdio.h>
            #include<conio.h>
            void main()
{          
            int  a, r=0,  c;
            clrscr();
            printf(“ Enter the number =”);
            scanf(“%d”,&a);
while(a!=0)
    {
            c=a%10;
            r=r*10+c;
            a=a/10;
      }
            printf(“ Revers number %d”,r);
             getch();
}
                                    Output:-
 Enter number= 1 2 3 4
                                    4 3 2 1


Q.5.Count number of  words in string ?
            #include<stdio.h>
            #include<conio.h>
            #include<string.h>
            void main()
{
            char a [100];
            int  i,len, word=0;
            clrscr();
            printf(“Enter the string =”);
            gets(a);
            len= Strlen (a);
             for(i=0; i<len; i++)
                        {
                                    word=word+1;
}
                                    printf(“Count word string %d”, word);
getch();
}
                        Output:-
Enter the String= RAM
                        3


Q.6. Concatenate or Compare string?
            #include<string.h>
            #incldue<conio.h>
            #include<string.h>
            void main()
{
            char  a[20],  b[20];
            clrscr();
            printf(“ Enter first string =”);
            gets(a);
            printf(“ Enter second string =”);
            gets(b);
            Strcat(a,b);
            printf(“Compare string %s”,a);
getch();
}
                        Output:-
Enter first string = Please
                        Enter second string= Subscribe Thankyou
                         Please Subscribe  Thankyou 

Q.7. Convert Binary to Decimal number use function ?
            #include<stdio.h>
            #include<conio.h>
            #include<math.h>
            int binary decimal ( int number );
            void main()
{          
            int n;
            clrscr();
            printf(“ Enter a binary number =”);
            scanf(“ %d”, &n);
            printf( %d”,binary decimal (n));
            getch();
}
            int binary decimal( int number )
{
                                    int decimal=0, i=0,  rem;
                                    while (number !=0)
{
                                                            rem= number %10;
                                                            number= number/10;
                                                            decimal= decimal+rem*pow(2,i);
}
                                                            return decimal;
}          
                        Output:-
Enter a binary number =1010
                        10

Q.8. Convert Decimal to Binary use function ?
            #include<stdio.h>
            #include<conio.h>
            #include<math.h>
            int decimal binary (int number );
            void main()
{
            int   n;
            clrscr();
            printf(“ Enter a decimal number =”);
            scanf(“%d”,&n);
getch();
}
            int decimal binary ( int number )
{
                                    int rem , i=1,  binary=0;
                                    while ( number !=0)
{
                                                            rem=number%2;
                                                            number= number/2;
                                                            binary= binary+rem;
                                                            i=i*10;
}
                                                            return binary;
   }
                                    Output:-         
Enter a decimal number =10
                                      10


Q.9. Calculate Vowels in string ?
            #include<stdio.h>
            #include<conio.h>
            #include<string.h>
            void main()
{
            char a[100] , v[ ]= “ aeoui AEIOU” ;
            int  i,j, count=0;
            clrscr();
            printf(“ Enter a string =”);
            gets(a);
for(i=0; a[i]; i++)
{
                                    for(j=0;  v[j]; j++)
                                                {
                                                            count++;
                                                            break;
                                                }
                                                            printf(“Total vowels %s %d”, a,count);
}
getch();           
}
                                    Output:-
                                    Enter a String = RAM
                                    Total Vowela = 1

Q.10.Input Array=1 2 3 4,    Output=  2 1 4 3 ,Odd/ Evem ?
            #include<stdio.h>
            #include<conio.h>
            void main()
{          
            int arr[50], num, I;
            clrscr();
            printf(“ Enter Arrais y size =”);
            scanf(“ %d”,&num);
for(i=0; i<num; i++)
{
                                    scanf(“%d” &arr[i]);
}
                                    printf(“ Even number is the Array =”);
for(i=0; i<n;i++)
{          
                                                            if( arr[i] %2==0)
                                                }          
                                                            printf(“%d”,arr[i]);
                       
}
                                    printf(“ Odd number is the Array =”);
                                    for(i=0; i<n; i++)
{
                                                if( arr[i]%2!=0)
                                                            {
                                                                        printf(“Odd”,arr[i]);
                                                            }
}
getch();
}


Q.11. Read an Array of number and to short then in Alphabetical order ?
            #include<stdio.h>
            #include<conio.h>
            #include<string.h>
            void main()
{
            char a[40][40],temp [50];
            int  i, j, n;
            clrscr();
            printf(“ Enter the number of Student”);
            scanf(“ %d” &n);
            for(i=0;i<n;i++)
{
                                    fflush (stdin);
                                    printf(“ Enter the name of Student %d”,i+1);
                                    gets(a[i]);
}
                                    for(i=0;i<n; i++)
{
                                                            for(j=0;j<n-1; j++)
                        {
                                                            if(strcmp a[j], a[j+1] j>o)
                                                {
                                                            strcpy( temp,a[j]);
                                                            strcpy( a[j],a[j+1]);
                                                            strcpy (a [j+1], temp);
                                                }
                                    }                       printf(“Name of the student\n);
}
for(i=0;i<n;i++)
{
                                                                                    puts (a[i]);
}
getch();
}
                                    Output:-
Enter the number of Student  Sujeet , Jonny
Jonny
                                                Sujeet

Q.12. Two swap variable value ,use function?
            #include<stdio.h>
            #include<conio.h>
            void swap(int *a,int *b);
            int main()
{
            int a ,b;
            clrscr();
            printf(“Enter the first number =”);
            scanf(“%d”&a);
            printf(“Enter the second number= ”);
            scanf(“%d”,&b);
            swap(a,b);
            printf(“After swapping = a=%d, b=%d”, a,b);
getch();
}
            void swap (int8p, int*q)
{
                                    int temp;
                                    temp=*p;
                                    *p=*q;
                                    *q=temp;
}
                        Output:-
Enter the first number =5
                        Enter the second number= 7
                        After Swapping  a=7
                                                              b=5   
 Q.13. Find Square root of given Quadratic equton ?
            #include< stdio.h>
            #include<conio.h>
            #include<math.h>
            void main()
{
            int   a,b,c,d;
            float x,y;
            printf(“ Enter Ceefficent=”);
            scanf(“%d%d%d”,&a,&b,&c);
            d=b*b-4*a*c;
            if(d==0)
            {
                        printf(“ Both roots are equal ”);
                        x= -b / (2.0*a);
            }
                        printf(“ Roots is %f”,x);
                        if(d>0)
                                    {
                                                printf(“Roots are real and distent ”);
                                                x=(-b+sqrt+(d)/(2*a);
                                                y=(-b-sqrt(d)/(2*a);
                                                printf(“Roots are =%f,%f ”, x,y);
                                    }
getch();
}
                                                Output:-
1
                                                            6
                                                            9
                                                Both roots are equal= 3.00000

Q.14.Factorial with help of Recurtion ?
            #include<stdio.h>
            #include<conio.h>     
            int factorial ( int num);
            void main()     
{
            int num, fact;
            clrscr();
            printf(“Enter the number =”);          
            scanf(“ %d” &num);
            fact=factorial (num);
            printf(“Factorial of %d%d”,num,fact);
getch();
}
            int factorial (int num)
{
                                    int fact;
                                    if(n==1)
                                    return 1;
                        }
else
{
                                                            fact=num*factorial(num-1);
                                                            return fact;
}

                                    Output:-
Enter the number= 5
                                     20



Q.15. Without using size operator ?
            #include<stdio.h>
            #include<conio.h>
            void main()
{
            int a;
            clrscr();
            printf(“ Size of is %d”, ( char a) (&a+1)- (char a) &a);
getch();
}

Q.16. Without using size operator in Structure /
            #include<stdio.h>
            #include<conio.h>
            struct
{          
            int num1, num2;
            char s1;
            int *ptr;
            int abc [5];
}           a[2];
void main()
{
                                    int start last;
                                    start = &a[1],num1;
                                    last   = &a[0], num2;
                                    printf(“Size of stricter %d byte”,Start, last);
}
getch();
}

Q.17. Student data using structure ?
            #include<stdio.h>
            #include<conio.h>
            Struct student
{
            char name [100];
            int roll ;
            int marks;
}
            void main()
                        {
                                    struct student s[100];
                                    int i;
                                    clrscr();
                                    printf(“Enter information of student ”);
                                    for(i=0;i<10;i++)
                        {
                                    s[i].roll=i+1;
                                    printf(“for all number %d”, s[i].roll);
                                    printf(“Enter name ”);
                                    printf(“%s”,&s[i].name);
                                    printf(“Enter marks”);
                                    scanf(“%d”,&s[i].marks);
                                    printf(\n);
}
                                    printf(“Displaying information of structure\n\n);
for(i=0;i<10;i++)
{
                                                            printf(“ Information for all roll number %d”,i+1);
                                                            printf(“Name”);
                                                            puts(s[i].name);
                                                            printf(“Marks %d”,s[i].marks);
}
getch();
}

Q.18.Delete array elements ?
            #include<stdio.h>
            #include<conio.h>
            void main()
{
            int a[100],position ,i,n;
            clrscr();
            printf(“Enter number of element in Array =”);
            scanf(“ %d”, &n);     
            printf(“Enter %d element”);
            for(i=0;i<n;i++)
{
                                    scanf(“%d”, a[i]);
                                    printf(“ Enter the location where you use to delete ”);
                                    scanf(“%d”,&position);
                                    if(position>=n+1)
{
                                    printf(“Delection not possible “);
}
                                    else
                                    {
                                                for(i=position; i<n-1; i++)
                                                            {
                                                                        a[i]=a[i+1];
                                                                        printf(“Resutent array”);
                                                            }
                                                                        for(i=0;i<n-1; i++)
                                                                                    {
                                                                                                printf(“ %d”,a[i]);
                                                                                    }
getch();
}

Q.19. Find Substing of without using library function ?
            #include< Stdio.h>
            #include<conio.h>
            int findsubstring( char*str,char*substring);
            int main()
{
            char str[40], substr[40];
            printf(“Enter the string”);
            gets(str);
            printf(“Enter the substring %d”,findsubstring);
            getch();
}
            int findsubstring (char*str,char*substr)
{
                                    int i=0, j=0;
while ( (str[j]!=0’\0’||(substr[i]!=’\0’)
                                    {
                        if(substr[j] ! = str [j] )
                        {
                                    j++;
                                    i=0;
                        }
                                    else
                                                {
                                                            i++
                                                            j++;
                                    if(substr [i] ==’\0’)
                                    return 1;
                                    else
                                    return 1;        
                        }                       }

Q.20.Find whether or polindrom or not ?
            #include<stdio.h>
            #include<conio.h>
            void main()
{
            int a, b, c=0, n;
            clrscr();
            printf(“ Enter the number =”);
            scanf(“ %d”,&a);
            n=a;
            while(a!=0)
            {
                        b=a%10;
                        c=c*10+b;
                        a=a/10;
            }
                        if(n==c)
                        {
                                    printf(“Polindrom number ”);
                        }
getch();
}

Q.21.Copy data from one file to Another file ?
            #include<stdio.h>
            #include<conio.h>
            void main()
{
            FILE *fp1,*fp2;
            char ch;
            fp1=fopen(“abc.txt”, “r”);
            fp2= fopen (“xyz.txt”, “w”);
            while ( ch=getc (fp1)! =EOF)
{
            putc ( ch,fp2);
            fclose(fp1);
            fclose (fp2);
getch();
}

Q.22.     Write a line of String a text file ?
            #include<stdio.h>
            #include<conio.h>
            #include<string.h>
            int main()
{
            FILE *fp;
            char file name [100];
            char writes [100];
            printf(“ Enter file name ”);
            gets(writers);
            fp=fopen(file name,”w”);
            if(fp)
{
            fputs(writers,fp);
}
            else
            {
            printf(“Failed to open the file “);      
            }
            fclose(fp);
            return(0);
getch();
}



Q.23. Amstrong or not Amstrong ?
            #include<stdio.h>
            #include<conio.h>
            void main()
{
            int n , p , s=0, m;        
            clrscr();
            printf(“ Input a number =”);
            scanf(“%d”,&n);
            m=n;
            while( n!=0 )
{
                                    p=n%10;
                                    s=s+p*p*p;
                                    n=n/10;
}
                                    if(m==s)
{
                                                            printf(“Amstrong number “);
}
                                    else
{
                                                            printf(“Not Amstrong number ”);
           
}
getch()

Q.24.Factorial number ?
            #include<stdio.h>
            #include<conio.h>
            void main()
{
            int a ,b, c=1;
            clrscr();
            printf(“Input number “);
            scanf(“%d”,&b);
            for(a=b;a>=1; a--)
{
            c=c*a;
}
            printf(“ %d”, c);
getch();
}
                                               

Q.25.Sparsh matrix ?

            #include<stdio.h>
            #include<conio.h>
            void main()
{
            int  a[3][3], i, j, c=0, m=3, n=3;
            clrscr();
            printf(“\n Enter the matrix ”);
            for(i=0;I <m; i++)
{
                                    for(j=0; j<n; j++)
                                                {
                                                            scanf(“%d”, &a[i][j]);
                                                }
}
                                                            for(i=0; j<m; j++)
                                                            if( a[i] [j]==0)
                                                            c++;

                                                            if(c>= (m*n/2)
                                                            {
                                                                        printf(“ Sparsh matrix ”);
                                                            }
                                                            else
                                                                        {
                                                                                    printf(“ Not Sparsh matrix );

}


Q.26. Find  Transpose matrix ?
            #include<stdio.h>
            #include<conio.h>
            void main()
{
            int a[10] [10], trans [10][10], r, c,i,j;
            clrscr();
            printf(“Enter rows and coloum of matrix ”);
            scanf(“ \n Enter elements of matrix ”);
            for(“ i=0; i<r; ++i)
{
                                    for(j=0; j<c; ++j)
                                    {
                                                printf(“Enter elements a = %d%d”, i+1, j+1);
                                                scanf(“ %d”, &a[i][j]);
                                    }
                        }                       printf(“ \n Entered matrix \n);
                                                for(i=0;i<r;++i);
                                                for(j=0;j<c;++j)
{
                                                                        printf(“%d”,a[i][j]);
                                                                        if(j==c-1)
}
                                                                        printf(“\n\n”);
for(i=0; i<r; ++i)
for(j=0; j<c;++j)
{
                                                                                                trans[j][i]= a[i][j];
}
                                                                                    printf(“\n Transpose of matrix \n);
                                                                                    for(i=0; i<c; ++i)
                                                                                    for( j=0; j<r; ++j)
{
                                                                                                            printf(“%d” ,trans[i][j])’
                                                                                                            if(j==r-1)
                                                                                                            printf(“\n\n);
}
getch();
}
                        Output:-
                        r-2,      c-2,      1,2,3,4
                        1 2
                        3 4
                        1 3
                        2 4



Q.27. Addition two Matrix ?
            #include<stdio.h>
            #include<conio.h>
            void main()
{
            int a[3][3], b[3][3], c[3][3], i,j,’
            clrscr();
            printf(“\n Enter first matrix ”);
            for(i=0; i<3; i++)
            for(j=0; j<3;j++)
{
                                    scanf(“%d”, &a[i][j]);
}
                                    printf(“\n Enter second matrix ”);
                                    for(i=0; i<3; i++)
                                    for(j=0; j<3;j++)
{
                                                            scanf(“%d”, &b[i][j]);
}
for(i=0; i<3; i++)
for(j=0; j<3;j++)
{
                                                                                    c[i][j]=a[i][j]+b[i][j];
                                                                                    printf(“%d”, c[i][j]);
}                                                                                   printf(“\n”);


Q.28. Electrical bill ?
            #include<stdio.h>
            #include<conio.h>
            void main()
{
            float unit, bill;
            clrscr();
            printf(“\n Enter the electricity uniy charge ”);
            scanf(“%f”,&unit);
            if(unit <=50)
{
            bill=unit*0.50;
}
else if( unit <=150 )
{
            bill=25+(unit-50)*0.75;
}
else if ( unit=<250)
{
            bill=100+(unit-150)*1.20;
}
else
{
            bill=220+(unit-250)*1.50;
}
printf(“\n Unit used =%f\t electricity bill =%f”,unit,bill);
getch();
}
Q29. Difference between union and structure
#include <stdio.h>
union unionJob
{
   //defining a union
   char name[32];
   float salary;
   int workerNo;
} uJob;

struct structJob
{
   char name[32];
   float salary;
   int workerNo;
} sJob;

int main()
{
   printf("size of union = %d", sizeof(uJob));
   printf("\nsize of structure = %d", sizeof(sJob));
   return 0;
}
Output

size of union = 32
size of structure = 40

Q30. PROGRAM FOR FILE OPEN, FILE WRITE AND FILE CLOSE ?
/ * Open, write and close a file : */
# include <stdio.h>
# include <string.h>

int main( )
{
    FILE *fp ;
    char data[50];
    // opening an existing file
    printf( "Opening the file test.c in write mode" ) ;
    fp = fopen("test.c", "w") ;
    if ( fp == NULL )
    {
        printf( "Could not open file test.c" ) ;
        return 1;
    }
    printf( "\n Enter some text from keyboard” \
             “ to write in the file test.c" ) ;
    // getting input from user
    while ( strlen ( gets( data ) ) > 0 )
    {
        // writing in the file
        fputs(data, fp) ;  
        fputs("\n", fp) ;
    }
    // closing the file
    printf("Closing the file test.c") ;
    fclose(fp) ;
    return 0;       
}

No comments:

Post a Comment

Thanks you
Sujeet kumar