Posts

Showing posts from November, 2017

My Subject Blog

C Program for FILE Function fputc fputs fgets

/* Write a program to illustrate the use of fputc(), fputs(), and fgets(). */ #include<stdio.h> #include<string.h> int main()  {      FILE *fp;  char str[4];  char ch[4]={'a','b','c','d'};  int i;  fp=fopen("Test.txt","w");  if(fp == NULL)   {   perror("Error opening file");   return(-1);  }  for(i=0;i<4;i++)  { fputc(ch[i],fp); //use of fputc()  }  fputs("EFGH",fp);    //use of fputs()  fclose(fp);      fp=fopen("Test.txt","r");   if(fp == NULL)   {   perror("Error opening file");   return(-1);  }   while(fgets(str,4,fp)!=NULL)   //use of fgets()  { printf("%s",str);  }  fclose(fp);     return 0; } 

C Program for Basic File Operation

/* Write a program to create a file and perform basic I/O operations on the same.  */ #include<stdio.h> int main()  {     FILE *fp;     int ch;     fp=fopen("Test.txt","w");     if(fp == NULL)  { perror("Error opening file"); return(-1); } putc('A',fp);     fclose(fp);        fp=fopen("Test.txt","r"); if(fp == NULL)  { perror("Error opening file"); return(-1); }  ch=getc(fp);     printf("CHAR = %c",ch);     fclose(fp);     return 0; } 

C Program for Memory Allocation and Reallocation

/* Write a program to store a character string in a block of memory space created by malloc and then modify the same to store a large string. */ #include <stdio.h> #include <stdlib.h> int main(void)  { char *ch; int n,n1; printf("\n Enter Size of String you Want : "); scanf("%d",&n); if((ch = (char *)malloc(n *sizeof(char))) == 0)         {             printf("\n No space available");             exit(1);         }     else       {           printf("\n %ld Bytes Space are allocated Successfully", n * sizeof(char));       }     printf("\nEnter String :");     scanf("%s",ch);     printf("\n String is : %s",ch);     printf("\n Enter Size You Want Increase: ");     scanf("%d",&n1);     if((c...

C Program for Runtime Memory Allocation for Table of Integers

/* Write a program that uses a table of integers whose size will be specified interactively at runtime.  */ #include <stdio.h> #include <stdlib.h> int main() {     int *p;     int n,i;     printf("\nEnter the size of table : ");     scanf("%d",&n);     if((p = (int*)malloc(n *sizeof(int))) == 0)        {            printf("\n No space available");            exit(1);        }    else       {          printf("\n %ld Bytes Space are allocated Successfully", n * sizeof(int));       }    for(i=0;i<n;i++)    {     printf("Enter Value for Position : [%d]",i+1);     scanf("%d",(p+i));    }    printf("\n Values in Reverse \n ");     for(i=n-1;i>=0;i--) ...

C program to Find Total of Marks using Structure

/* Write a function to enter Roll no, marks of the three subject for 3 students and find total obtained by each student. */ #include <stdio.h> struct student { int rollno; int marks[3]; }s[3]; int main(void)  { int i,j,total=0; for(i=0;i<3;i++) { total=0; printf("Enter Roll No : "); scanf("%d",&s[i].rollno); for(j=0;j<3;j++) { printf("Enter Marks for Subject [%d]",j+1); scanf("%d",&s[i].marks[j]); total=total+s[i].marks[j]; } printf("\n Total Marks of Student Roll No [%d] : %d",s[i].rollno,total); } return 0; }

C Program to define structure called Cricket

/* Define a structure called cricket that will describe the following information:  a. Player name  b. Team name  c. Batting average */ struct cricket { char player_name[20]; char team_name[20]; float batting_average; };

C Program to Copy String Using Pointer

/* Write a program using a pointer to copy one string to another string.  */ #include <stdio.h> int main(void)  {  char str1[50],str2[50];  int i,j;  char *p,*q;  printf("\n Enter First String : ");  scanf("%s",str1);  p=str1;  q=str2;  for(i=0;*(p+i)!='\0';i++)  {      *(q+i)=*(p+i);  } *(q+i)='\0';  printf("Copied String = %s",q);  return 0; }

C Program to Access Array using Pointer

/* Write a program using a pointer to read an array of integer and print elements in reverse order. */ #include <stdio.h> int main(void)  { int a[10],*p,i; p=a; for(i=0;i<10;i++) { printf("Enter Value for [%d] Position : ",i+1); scanf("%d",(p+i)); } for(i--;i>=0;i--) { printf("\n a[%d] = %d",i+1,*(p+i)); } return 0; }

C Program to Compare Two String Using Pointer

/* Write a program using a pointer to compare two strings.  */ #include <stdio.h> int main(void)  { char str1[50],str2[50]; int i,j; char *p,*q; printf("\n Enter First String : "); scanf("%s",str1); printf("\n Enter Second String : "); scanf("%s",str2); p=str1; q=str2; for(i=0;*(p+i)!='\0';i++) { if(*(p+i) != *(q+i)) { break; } } if(*(q+i)=='\0') { printf("\n Both String are Equal"); } else { printf("\n Both String are Not Equal"); } return 0; }

C Program to Change Value of Variable Using Pointer

/* Write a program to change the value of a variable using pointer.  */ #include <stdio.h> int main(void)  { int i=10,*p; p=&i; printf("\n Value of I = %d",i); *p=20; printf("\n Value of I After Changing = %d",i); return 0; }

C Program to Print Address of Variable

/* Write a program to print an address of a variable.  */ #include <stdio.h> int main(void)  { int i=10; printf("Address of I is %u",&i); return 0; }

C Program for Prime Number using Function

/* Write a function prime that returns 1  if it‘s argument is prime and return 0 otherwise.    */     #include <stdio.h>     int prime(int );     int main(void) {     int no;     printf("\n Enter any No : ");     scanf("%d",&no);     if(prime(no)==1)     {     printf(" %d is Prime",no);     }     else     {     printf(" %d is not Prime",no);     }     return 0;     }           int prime(int n)     {     int i=2;     while(i<n)     {     if(n%i==0)     {     break;     }     i++;     }     if(i==n)     {     return 1;     }     else     { ...

C Program for Calculator using Function

/* Write a calculator program(add,subtract,multiply,divide).  Prepare user defined function for each functionality.   */ #include <stdio.h> int add(int, int); int subtract(int, int); int multiply(int, int); int divide(int, int); int main(void) { int a, b, ch; do { printf("\n Calculator Program "); printf("\n======================="); printf("\n 1 - Add "); printf("\n 2 - Subtract "); printf("\n 3 - Multiply "); printf("\n 4 - Divide "); printf("\n 5 - Exit "); printf("\n======================="); printf("Enter Your Choice :"); scanf("%d",&ch); printf("\n Enter First Value :"); scanf("%d",&a); printf("\n Enter Second Value :"); scanf("%d",&b); switch(ch) { case 1: printf("Add = %d",add(a,b)); break; case 2: printf("Subtract = %d...

C Program to Find Factorial Using Function with Recursion and Without Recursion

/* Write a Program using function to Find out Factorial of given number with and without recursion.  */ #include <stdio.h>      int fact_rec(int); \\ With Recursion int fact(int);        \\Without Recursion int main(void)  {     int n;     printf(" Enter Value n = ");     scanf("%d",&n);     printf("\n Without Rec = %d",fact(n));     printf("\n With  Rec = %d",fact(n));     return 0; }      int fact(int n) \\Without Recursion {     int ans = 1;     while(n>=1)     {     ans = ans * n;     n--;     }     return ans; }       int fact_rec(int n) \\ With Recursion {     int ans = 1;     if(n==1)     {     return ans;     }     ans =  ans * fact_rec(...

C Program to Find Cube using Function

/* Write a Program using function to Find out Cube of any given number N. */ #include <stdio.h> int cube(int); int main(void)  { int n; printf("Enter Any Number N : "); scanf("%d",&n); printf("\n Cube = %d",cube(n)); return 0; } int cube(int n) { return (n*n*n); }  

C Program to Find Sum and Average of First N Number using Function

/* Write a Program using function to Add 1st n numbers and also Find out Average of 1st n numbers.  (Where n is enter through Keyboard.)  */ #include <stdio.h> int getsum(int ); float getavg(int ); int main(void)  { int n; printf("Enter Any numner n = "); scanf("%d",&n); printf("\n SUM = %d",getsum(n)); printf("\n AVG = %f",getavg(n)); return 0; } int getsum(int n) { return ((n*(n+1))/2); } float getavg(int n) { return (((float)n*(n+1))/2)/n; }

C Program to Count No of Words in Sentence

/* Write a program to count no of words in a sentence.  */ #include <stdio.h> int main(void) { char str[100]; int i,cnt=0,flag=0; printf("\n Enter Sentence : "); scanf("%[^\n]s",str); for(i=0;str[i]!='\0';i++) { if(str[i]==' ') { flag=0; } else if(str[i]!=' ' && flag==0) { cnt++; flag=1; } } printf("\n No of Words in Sentence  = %d",cnt); return 0; }

C Program to String Copying

/* Write a program to copy one string to another string */ #include <stdio.h> int main(void)  { char str1[50],str2[50]; int i; printf("Enter First String :"); scanf("%[^\n]s",str1); for(i=0;str1[i]!='\0';i++) { str2[i]=str1[i]; } str2[i]='\0'; printf("Copied String = %s",str2); return 0; }

C Program to Find String is Palindrome or not

/* Write a program to check whether given String is Palindrome or not.  */ #include <stdio.h> int main(void)  {     char str[100];     int i=0,j=0;     printf("Enter the First String : ");     scanf("%s",str);     for(j=0;str[j]!='\0';j++)     {     }     j--;     while(i<j)     {     if(str[i]!=str[j])     {     break;     }     i++;     j--;     }     if(i<j)     {     printf(" String : %s is not palindrome",str);     }     else     {     printf(" String : %s is palindrome",str);     } return 0; }

C Program to Concatenate Two Strings

/* Write a program to append one string to another String.  */     #include <stdio.h>           int main(void) {     // your code goes here     char str1[100],str2[50];     int i,j;     printf("Enter the First String : ");     scanf("%s",str1);     printf("Enter the Second String : ");     scanf("%s",str2);     for(i=0;str1[i]!='\0';i++)     {           }     for(j=0;str2[j]!='\0';j++)     {     str1[i]=str2[j];     i++;     }     str1[i]='\0';     printf(" Concatenated String : %s",str1);     return 0;     }

C Program to Find Vowels in String

/* Write a program to Count no. of Vowels in given String.  */ #include <stdio.h> int main(void) { // your code goes here char str[100]; int i,cnt=0; printf("Enter the String : "); scanf("%[^\n]s",str); for(i=0;str[i]!='\0';i++) { switch(str[i]) { case 'A': case 'a': case 'E': case 'e': case 'I': case 'i': case 'O': case 'o': case 'U': case 'u': cnt++; break; } } printf("Total No of Vowel : %d",cnt); return 0; }

C Program to Find String Length

/* Write a program to Find String Length without using inbuilt function.  */ #include <stdio.h> int main(void) { // your code goes here char str[100]; int len; printf("Enter the String :"); scanf("%s",str); for(len=0;str[len]!='\0';len++) { } printf("Length = %d",len); return 0; }

C Program to do Matrix Addition

/* Write a program to Add two Matrices. i.e.  C= A + B. (Matrix Addition) */ #include<stdio.h> #define size 5 int main()     {     int c[size][size],a[size][size],b[size][size],i,j,m1,n1,m2,n2;     printf("Enter the order of 1st matrix \n");     printf("Rows \t");     scanf("%d",&m1);     printf("\n Column\t");     scanf("%d",&n1);     printf("Enter the order of 2st matrix \n");     printf("Rows\t");     scanf("%d",&m2);     printf("\n Column\t");     scanf("%d",&n2);     if(m1!=m2 && n1!=n2)     {      printf("\nMatrix Addition is not possible");     }     else     {       printf("\nEnter the elements of 1st array");      for(i=0;i<m1;i++)       {         for(j=0;j<n1;j...

C Program for Matrix Multiplication

/* Write a program to Multiply two 3X3 Matrices. (Matrix Multiplication) */ #include<stdio.h> #define size 3 void main() { int c[size][size],a[size][size],b[size][size],i,j,k,m1,n1,m2,n2; printf("Enter the order of 1st matrix \n"); printf("Rows \t"); scanf("%d",&m1); printf("\n Column\t"); scanf("%d",&n1); printf("Enter the order of 2st matrix \n"); printf("Rows\t"); scanf("%d",&m2); printf("\n Column\t"); scanf("%d",&n2); if(n1!=m2) {  printf("\nMatrix multiplication is invalid"); } else { printf("\nEnter the elements of 1st array");  for(i=0;i<m1;i++)   {      for(j=0;j<n1;j++)      {       scanf("%d",&a[i][j]);      }   }   for(i=0;i<m1;i++)   {      for(j=0;j<n1;j++)      {        printf("%d\t",a[i][j]);      }      printf("\n");  ...

C Program to Print Array in Reverse Order

/* Write a program to read array of integers and print it in reverse order  */ #include <stdio.h>       int main(void)  { int a[10],i,n=10; for(i=0;i<n;i++) {     printf("Enter Value at a[%d] : ",i);     scanf("%d",&a[i]); } for(i=n-1;i>=0;i--) {     printf("\n Enter Value at a[%d] : %d ",i,a[i]); } return 0; }

Followers