Posts

Showing posts from September, 2020

Write a program to sum N numbers using command line arguments. The following program must be executed in the command prompt or with the arguments set in the environment.

/* Write a program to sum N numbers using command line arguments*/ #include <stdio.h> #include <ctype.h> void main(char *argv[],int argc) {     int i,s=0;     for(i=1;i<argc;i++)     {         printf("Value at %u is %s\n",argv[i],argv[i]);         s+=atoi(argv[i]);     }     printf("Sum is %d\n",s);     getch(); }

Write a program to show the usage of storage class:register.

/* Write a program to show the usage of storage class:register*/ #include <stdio.h> #include<time.h> void main() {     float v1;     register float v2;     clock_t s1,e1,s2,e2;     s1=clock();     for(v1=0.01;v1<=1000;v1+=0.01)     {         printf("%8.2f ",v1);     }     e1=clock();     s2=clock();       for(v2=0.01;v2<=1000;v2+=0.01)     {         printf("%8.2f ",v2);     }     e2=clock();     printf("\nNormal processing : %f\n",(double)(e1-s1)/CLOCKS_PER_SEC);     printf("\nRegister processing : %f\n",(double)(e2-s2)/CLOCKS_PER_SEC);       getch(); }

Write a program to show the usage of storage class:static.

/* Write a program to show the usage of storage class:static*/ #include <stdio.h> void disp() {     static int i=5;     printf("I=%d\n",i);     i++; } void main() {     disp();     disp();     disp();     getch(); }

Write a program to show the usage of storage class:extern.

 /* prog38.h*/ int i=50; /*prog38.c*/ /* Write a program to show the usage of storage class:extern*/ #include "prog38.h" #include <stdio.h> extern int i; void main() {     printf("I=%d\n",i);     getch(); }

Write a program to show the usage of storage class:auto.

/* Write a program to show the usage of storage class:auto*/ #include <stdio.h> void disp() {       auto int i=20;     printf("I = %d\n",i);     i++; }   void main() {     disp();     disp();     disp();     getch(); }

Write a program to find the factorial of the given number using preprocessor directive #include. The following program has two source files.

 /*prog36.h*/ long factorial(int x) {     if (x<1)     {         return 1;     }     return x*factorial(x-1); } /*prog36.c*/ /* Write a program to find the factorial of the given number using preprocessor directive #include*/ #include <stdio.h> #include "prog36.h" void main() {     int n;     long fact;     printf("Enter the number : ");     scanf("%d",&n);     fact=factorial(n);     printf("Factorial is %ld\n",fact);     getch(); }

Write a program to accept radius of a circle and find the circumference and area using preprocessor directive #define.

/* Write a program to accept radius of a circle and find the circumference and area using preprocessor directive #define*/ #include <stdio.h> #define PI 3.1415 void main() {     float radius,circum,area;      printf("Enter the radius of a circle : ");     scanf("%f",&radius);     circum=2*PI*radius;     area=PI * radius * radius;     printf("Circumference : %8.3f\n",circum);     printf("Area           : %8.3f\n",area);     getch(); }

Write a program to store employee details and print it. block-level access.

/* Write a program to store employee details and print it. block-level access.*/ #include <stdio.h> void main() {     struct employee     {         int empno;         char ename[20];         float sal;     }e;     FILE *fp;     fp=fopen("d.txt","a+");     do     {         printf("Enter the employee number   : ");         scanf("%d",&e.empno);         fflush(stdin);         printf("enter the employee name    : ");         gets(e.ename);         printf("Enter the salary : ");       ...

Write a program to store employee details and print it. sentence-level access.

/* Write a program to store employee details and print it. sentence-level access.*/ #include <stdio.h> void main() {     int empno;     char ename[20];     float sal;     FILE *fp;     fp=fopen("c.txt","w");     do     {         printf("Enter the employee number   : ");         scanf("%d",&empno);         fflush(stdin);         printf("enter the employee name    : ");         gets(ename);         printf("Enter the salary : ");         scanf("%f",&sal);         fprintf(fp,"%4d %-20s %7.2f\n",empno,ename,sal);      ...

Write a program to accept N names and print it. string-level access.

/* Write a program to accept N names and print it. string-level access.*/ #include <stdio.h> void main() {     char st[20];     FILE *fp;     fp=fopen("b.txt","w");     do     {         printf("Enter the name : ");         gets(st);         fputs(st,fp);         fputs("\n",fp);         printf("Enter to continue;Escape to exit\n");     }while(getch()!=27);     fclose(fp);     fp=fopen("b.txt","r");     printf("Name List :\n");     fgets(st,20,fp);     while(!feof(fp))     {         st[strlen(st)-1]=0;         printf("%s\n...

Write a program to write and read a file using byte-level access.

/* Write a program to write and read a file using byte-level access.*/ #include <stdio.h> void main() {     char ch;     FILE *fp;     fp=fopen("a.txt","w");     printf("Enter the contents (End with Ctrl+Z):\n");     while((ch=getchar())!=EOF)     {         fputc(ch,fp);     }     fclose(fp);     fp=fopen("a.txt","r");     printf("Contents of the file :\n");     ch=fgetc(fp);     while(!feof(fp))     {         putch(ch);         ch=fgetc(fp);     }     fclose(fp);     getch(); }

Write a program to create a structure employee with empno, name and salary, accept and display the details, create a pointer of structures and allocate for the given number of employees using dynamic memory allocation(malloc).

/* Write a program to create a structure employee with empno, name and salary, accept and display the details, create a pointer of structures and allocate for the given number of employees using dynamic memory allocation(malloc)*/ #include <stdio.h> struct employee {     int empno;     char empname[20];     float sal; }*e; void main() {     int i,n;     printf("Enter the number of employees : ");     scanf("%d",&n);     e=(struct employee *)malloc(sizeof(struct employee)*n);       for (i=0;i<n;i++)     {         printf("Employee %d:\n",i+1);         printf("Enter the employee number : ");         scanf("%d",&(e+i)->empno);         fflush(stdin); ...

Write a program to create a structure employee with empno, name and salary, accept and display the details, create an array of structures of fixed size.

/* Write a program to create a structure employee with empno, name and salary, accept and display the details, create an array of structures of fixed size.*/ #include <stdio.h>   struct employee {     int empno;     char empname[20];     float sal; }e[3]; void main() {     int i;     for (i=0;i<3;i++)     {         printf("Employee %d:\n",i+1);         printf("Enter the employee number : ");         scanf("%d",&e[i].empno);         fflush(stdin);         printf("Enter the employee name    : ");          gets(e[i].empname);         printf("Enter the employee salary : ");   ...

Write a program to find the factorial of the given number using function with recursion.

/* Write a program to find the factorial of the given number using function with recursion*/ #include <stdio.h> long factorial(int); void main() {     int n,f;     printf("Enter the number : ");     scanf("%d",&n);     f=factorial(n);     printf("Factorial = %ld\n",f);     getch(); } long factorial(int x) {     if (x<1)     {         return 1;     }     return x*factorial(x-1); }

Write a program to find the factorial of the given number using function with while loop.

/* Write a program to find the factorial of the given number using function with while loop*/ #include <stdio.h> long factorial(int); void main() {     int n,f;     printf("Enter the number : ");     scanf("%d",&n);     f=factorial(n);     printf("Factorial = %ld\n",f);     getch(); } long factorial(int x) {     long fact=1;     while(x>1)     {         fact*=x--;     }     return fact; }

Write a program to find the factorial of the given number using function with for loop.

/* Write a program to find the factorial of the given number using function with for loop*/ #include <stdio.h> long factorial(int); void main() {     int n,f;     printf("Enter the number : ");     scanf("%d",&n);     f=factorial(n);     printf("Factorial = %ld\n",f);     getch(); } long factorial(int x) {     int i;     long fact=1;     for(i=1;i<=x;i++)     {         fact*=i;       }     return fact; }

Write a program to add two numbers using functions accept, add, display using call by value and call by reference.

/* Write a program to add two numbers using functions accept, add, display using call by value and call by reference.*/ #include <stdio.h> void accept(int *,int *); int add(int,int); void display(int,int,int); void main() {     int a,b,c;     accept(&a,&b);     c=add(a,b);     display(a,b,c);     getch(); } void accept(int *x,int *y) {     printf("Enter the first number   : ");     scanf("%d",x);     printf("Enter the second number : ");     scanf("%d",y); } int add(int x,int y) {     int z;     z=x+y;     return z; } void display(int x,int y,int z) {     printf("The sum of %d and %d is %d\n",x,y,z); }

Write a program to create an union with two members, store value in one member and retrieve it through another member.

/* Write a program to create an union with two members, store value in one member and retrieve it through another member.*/ #include <stdio.h> union un {     int m1;     char m2; }u1={0},u2={0},*p; void main() {     p=&u2;     u1.m1=65;     printf("Union 1 :\n");     printf("M1=%d M2=%c\n",u1.m1,u1.m2);     p->m2='K';     printf("Union 2 :\n");     printf("M1=%d M2=%c\n",p->m1,p->m2);     getch(); }