Posts

‘C’ program to accept a number and check if it is positive, negative or zero.

C File => /* Write a ‘C’ program to accept a number and check if it is positive, negative or  zero. */ #include<stdio.h> int main()  {      int num;      printf("\n Enter an integer : ");      scanf("%d",&num);       if(num==0)           printf("\n Number is Zero");    else if(num>0)           printf("\n Number is positive");    else if(num<0)           printf("\n Number is negative");  return 0; }

‘C’ program to accept temperatures in Fahrenheit (F) and print it in Celsius (C) Formula C=5/9(F-32).

C File => /* Write a ‘C’ program to accept temperatures in Fahrenheit (F) and  print it in Celsius (C)  Formula C=5/9(F-32) */ #include<stdio.h> int main()  {       float c,f;       printf("\n Enter temperature in Fahrenheit : ");          scanf("%f",&f);        c=(5*(f-32))/9;        printf("\n Temperature in Celcius : %f",c);  return 0; }

'C' program to find maximum of two numbers.

C File => /* Write a ‘C’ program to find maximum of  two numbers. */ #include<stdio.h> int main()  {       int a,b;       printf("\n Enter number1 : ");       scanf("%d",&a);       printf("\n Enter number2 : ");       scanf("%d",&b);       if(a>b)             printf("\n Maximum of number is : %d",a);      else             printf("\n Maximum of number is : %d",b);   return 0; }

‘C’ program to accept two integers from the user and interchange them.

C File => /* Write a ‘C’ program to accept two integers from the user and interchange them. Display the interchanged numbers.*/ #include<stdio.h> int main()  {        int n1,n2,temp;        printf("\n Enter value for n1 : ");        scanf("%d",&n1);        printf("\n Enter value for n2 : ");        scanf("%d",&n2);        temp=n1;        n1=n2;        n2=temp;        printf("\n After swapping \n ");        printf("n1=%d\tn2=%d",n1,n2);    return 0; }

Assignment III - Advanced 'C'

Assignment III     1.   Write a menu driven program to perform the following operations on strings using standard library functions : 1. Convert string toupper case 2. Copy one string to another 3. Compare two strings 4. Concatenates two strings. 2.     A Write a program to  declare  a structure  person  (name, address)  which  contains another structure birthdate (day,month, year). Accept the details  of n persons and display them. 3.        Write a program to accept ‘n’ employee details (eno, ename, salary) an d   display all employee details whose salary is more than 10000, by passing    array of structure to the function. 4.     Write a C program to create structure employee (id, name,salary). Accept details of n employees and perform the following operations:a.      Display all employ...

Program for addition of matrices

C File => /* Write a program to add two matrices and display the result. Use dynamic memory allocation.*/ #include<stdio.h> #include<stdlib.h> int main() {        int n,i,j,*ptr[50],*ptr1[50],sum[50][50];        printf("\n Enter order of matrix : ");        scanf("%d",&n);      //Dynamic allocation of memory       for(i=0;i<n;i++)      {         ptr[i]=(int*)malloc(n*sizeof(int));         ptr1[i]=(int*)malloc(n*sizeof(int));      }     // Accepting matrix     for(i=0;i<n;i++)    {      for(j=0;j<n;j++)     {        printf("\n Enter matrix element a%d%d : ",i+1,j+1);        scanf("%d",&ptr[i][j]);      }    }    printf("\n\n Given Matrix1 is : \n ");   ...

Program to calculate sum of diagonal elements of matrix

C File => /* Write a C program to accept and display a matrix of order nXn. Use dynamic memory allocation. Also calculate sum of diagonal elements. */ #include<stdio.h> #include<stdlib.h> int main() {      int n,i,j,*ptr[50],sum=0;      printf("\n Enter order of matrix : ");      scanf("%d",&n);     // dynamic allocation of memory      for(i=0;i<n;i++)          ptr[i]=(int*)malloc(n*sizeof(int));     // Accepting matrix      for(i=0;i<n;i++)     {        for(j=0;j<n;j++)       {              printf("\n Enter matrix element a%d%d : ",i+1,j+1);              scanf("%d",&ptr[i][j]);        }     }     printf("\n\n Given Matrix is : \n ");     for(i=0;i<n;i++) ...

Program to count number of occurrences of given character in the file using command line arguments.

C File => /* Write a C program to accept file name and character as command line arguments and count number of occurrences of given character in the file. */ #include<stdio.h> #include<stdlib.h> #include<string.h> int main(int argc,char *argv[]) {     char ch,cnt=0;     char c=argv[2];     FILE *fp;     fp=fopen(argv[1],"r");     while((ch=fgetc(fp))!=EOF)     {          if(ch==c)            cnt++;      }  printf("\n Count of %c = %d",argv[2],cnt); return 0; }

Program to illustrate nested union

C File => /* Write a program to accept details of ‘n’ cricket players (name, type). If type = 1 (batsman), accept batting_average. If type=2 (bowler), accept number_wickets. Use nested union. Display information of all batsmen and bowlers. */ #include<stdio.h> #include<string.h> typedef union player {     char name[30];     union type    {        int type;    }t; }; int main() {      union player p[50];      char batsman[50][50],bowler[50][50],s[30];      float avg[50];      int i,j=0,k=0,wkts[50],n1,n2,n;      printf("\n How many played? ");      scanf("%d",&n);     // Accepting input      for(i=0;i<n;i++)     {        printf("\n Enter player name : ");        scanf("%s",p->name);        strcpy(s,p->name); ...

Program to write user defined functions for string operations

C File => /* Write a program to perform the following operations on strings using user defined functions: a. Length of string b. Copy one string to another c. Convert string to uppercase*/ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> int main() {       char s1[30],s2[30];       int i,len;      //Function to calculate length       int length(char str[30])      {          int len = 0;          while (str[len] != '\0')          len++;          return (len);       }    // Function to copy string to another      void copy(char s1[30],char s2[30])     {         int i=0;         for(i=0;i<strlen(s1);i++)               s2[i]=s1[i]...