Command line arguments – Theory and Programs [Learn to code]

What are Command line arguments? Learn syntax for using Command line arguments, programs on Command line arguments.

  • 101. Write a program accept a file name from user and print content of that file
    #include<stdio.h>
    int main(int argc, char *argv[])
    {
        if ( argc != 2 ){
             printf( "usage: %s filename", argv[0] );
        }
        else 
        {
            FILE *file = fopen( argv[1], "r" );
     
            if(file == NULL)
                 printf("Could not open filen");
            else 
            {
                int x;
                while((x=fgetc(file)) != EOF)
                     printf( "%c", x );
                fclose( file );
            }
        }
    }

File operations – Theory and Programs [Learn to code]

What are File operations? Different modes of File operations and programs on File operations.

  • 098. Write a program to read and write a file character by character.
    #include<stdio.h>
    int main()
    {
        FILE *fp;   /* file pointer*/
        char fName[20];
        char ch;
     
        printf("nEnter file name to create :");
        scanf("%s",fName);
     
        fp = fopen(fName,"w");
        if(fp==NULL)
        {
            printf("File not created !!");
            exit(0); 
        }
     
        printf("File created successfully.");
        /*writing into file*/
        printf("nEnter text to write (press < enter > to save & quit):n");
        while( (ch = getchar())!='n')
        {
            putc(ch, fp); /*write character into file*/
        }
     
    /*again open file to read data*/
        fp = fopen(fName,"r");
        if(fp==NULL)
        {
            printf("nCan't open the file!!!");
            exit(0);
        }
     
        printf("nContents of file is :n");
        /*read text until, end of file is not detected*/
        while( (ch=getc(fp))!=EOF )
        {
            printf("%c",ch); /*print character on screen*/
        }
     
        fclose(fp);
        return 0;
    }
  • 099. Write a program to read and write a file line by line.
    #include <stdio.h>
    int main()
    {
        FILE *fp;
        char str[100];
        fp = fopen("C:\myfile.txt", "w");
     
        if (fp == NULL)
        {
           puts("An error occurred while opening the specified file");
        }
     
         printf("Enter your string:");
     
         gets(str);
     
         fputs(str, fp);
     
         fclose(fp);
     
        fp = fopen("C:\myfile.txt", "r");
     
        if (fp == NULL)
        {
           puts("An error occurred while opening the specified file");
        }
     
        while(1)
        {
           if(fgets(str, 10, fp) ==NULL)
                break;
           else
                printf("%s", str);
        }
     
        fclose(fp);
        return 0;
    }
  • 100. Merge two files by store data into third one
    #include<stdio.h>
    #include<stdlib.h>
     
    int main()
    {
       FILE *fp1 = fopen("file1.txt", "r");
     
       FILE *fp2 = fopen("file2.txt", "r");
     
       FILE *fp3 = fopen("file3.txt", "w");
     
       char c;
       if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
       {
             puts("Could not open files");
             exit(0);
       }
     
       while ((c = fgetc(fp1)) != EOF)
          fputc(c, fp3);
     
       while ((c = fgetc(fp2)) != EOF)
          fputc(c, fp3);
     
       fclose(fp1);
       fclose(fp2);
       fclose(fp3);
       return 0;
    }

Structures – Theory and Programs [Learn to code]

What are Structures? Learn syntax for Structures and different programs on Structures.

  • 095. Define a structure Employee having elements emp_id, name,etc. Accept data and reprint it
    #include<stdio.h>
    #include<conio.h>
     
    struct Employee
    {
     char name[50];
     int emp_id;
     long phone_no;
    };
     
    main()
    {
    	struct Employee e;
    	clrscr();
    	printf("Enter name : ");
    	scanf("%s",&e.name);
    	printf("Enter emp_id: ");
    	scanf("%d",&e.emp_id);
    	printf("Enter Phone Number: ");
    	scanf("%ld",&e.phone_no);
     
    	printf("nnEnter name : %s",e.name);
    	printf("nnEnter Emp Id       : %d",e.emp_id);
    	printf("nnEnter Phone Number : %ld ",e.phone_no);
     
    	getch();
    }
  • 096. Define a structure Student having fields roll_no, name, marks, etc, for 5 students, accept data and reprint
     
    #include<stdio.h>
    #include<conio.h>
     
    struct Student
    {
     char name[50];
     int roll_no;
     int m1,m2,m3;
    };
     
    main()
    {   int i;
    	struct Student s[5];
    	clrscr();
    	for(i=0;i<5;i++)
    	{   printf("nEnter data for Student %d.....n",i+1);
    		printf("Enter name           : ");
    		scanf("%s",&s[i].name);
    		printf("Enter Roll No.       : ");
    		scanf("%d",&s[i].roll_no);
    		printf("Enter marks for sub1 : ");
    		scanf("%d",&s[i].m1);
    		printf("Enter marks for sub2 : ");
    		scanf("%d",&s[i].m2);
    		printf("Enter marks for sub3 : ");
    		scanf("%d",&s[i].m3);
    	}
     
    	for(i=0;i<5;i++)
    	{   printf("nStudent   %dn",i+1);
    		printf("Name    : %sn",s[i].name);
    		printf("Roll No.: %dn",s[i].roll_no);
    		printf("Sub1    : %dn",s[i].m1);
    		printf("Sub2    : %dn",s[i].m2);
    		printf("Sub3    : %dn",s[i].m3);
    	}
     
    	getch();
    }
  • 097. Define a structure Employee having elements emp_id, name, DOB, DOJ etc. Accept data and reprint it. (use structure within structure)
    #include<stdio.h>
    #include<conio.h>
     
    struct Date
    {
    	int mm,dd,yy;
    };
     
    struct Employee
    {
    char name[50];
     int emp_id;
     struct Date DOB,DOJ;
    };
     
    main()
    {   int i;
    	struct Employee e;
    	clrscr();
    		printf("nEnter name           : ");
    		scanf("%s",&e.name);
    		printf("nEnter emp_id.        : ");
    		scanf("%d",&e.emp_id);
    		printf("nEnter Date of Joiningn ");
    		printf("(dd-mm-yy) : ");
    		scanf("%d-%d-%d", &e.DOJ.dd,&e.DOJ.mm,&e.DOJ.yy);
    		printf("nEnter Date of birthn ");
    		printf("(dd-mm-yy) : ");
    		scanf("%d-%d-%d", &e.DOB.dd,&e.DOB.mm,&e.DOB.yy);
     
    		printf("nName           : %s",e.name);
    		printf("nEmployee ID    : %d",e.emp_id);
    		printf("nEnter DOJ      : %d-%d-%d", e.DOJ.dd,e.DOJ.mm,e.DOJ.yy);
    		printf("nEnter DOB      : %d-%d-%d", e.DOB.dd,e.DOB.mm,e.DOB.yy);
     
    	getch();
    }

Strings – Theory and Programs [Learn to code]

What is String? What are inbuild functions for processing string and different programs on Strings.

  • 081. Print string in reverse,its length,in uppercase,lowercase and copy into another string
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    main()
    {
        char *s1,*s2;
        clrscr();
        printf("Enter string : ");
        scanf("%s",s1);
        printf("Reverse String : %s",strrev(s1));
        printf("Lenght: %d",strlen(s1));
        printf("String in UPPER CASE : %s",strupr(s1));
        printf("String in lower case : %s",strlwr(s1));
        strcpy(s2,s1);
        printf("nCopied string is...%s",s2);
        getch();
    }
  • 082. Write a program to accept a string and print no. of aphlabets, digits, special symbols present in it in C language
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
    main()
    {
    	char *s;
    	int i,alphabets=0,digits=0,symbols=0;
    	clrscr();
    	printf("Enter string : ");
    	scanf("%s",s);
    	for(i=0;s[i]!='';i++)
     
    		if(isalpha(s[i]))
    			alphabets++;
    		else if(isdigit(s[i]))
    			digits++;
    		else
    			symbols++;
     
    	printf("nAphabets : %d",alphabets);
    	printf("nDigits   : %d",digits);
    	printf("nSymbols  : %d",symbols);
    	getch();
    }
  • 083. Write a program to accept two strings and compare them in C language
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    main()
    {
    	char *s1,*s2;
    	clrscr();
    	printf("Enter string 1 : ");
    	scanf("%s",s1);
    	printf("Enter string 2 : ");
    	scanf("%s",s2);
     
    	if(strcmp(s1,s2)>0)
    		printf("nString 1 is greater..");
    	else if(strcmp(s1,s2)<0)
    		printf("nString 2 is greater..");
    	else
    		printf("nStrings are equal..");
     
    	getch();
    }
  • 084. Write a program to accept two strings and concatenate them in C language
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    main()
    {
    	char *s1,*s2;
    	clrscr();
    	printf("Enter string 1 : ");
    	scanf("%s",s1);
    	printf("Enter string 2 : ");
    	scanf("%s",s2);
    	printf("nConcatinated string is...%s",strcat(s1,s2));
    	getch();
    }
  • 085. Write a program to print length of a given string in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,len=0;
    	char *s;
    	clrscr();
    	printf("Enter the string....");
    	scanf("%s",s);
    	for(i=0;s[i]!='';i++)
    		len++;
    	printf("The lenth of the string is...%d",len);
    	getch();
    }
  • 086. Write a program to copy a string into another string in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i;
    	char *s1,*s2;
    	clrscr();
    	printf("Enter the string 1....");
    	scanf("%s",s1);
    	for(i=0;s1[i]!='';i++)
    	{
    		s2[i]=s1[i];
    	}
    	s2[i]='';
    	printf("The copied string is...%s",s2);
    	getch();
    }
  • 087. Write a program to convert given string to UPPER CASE in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i;
    	char *s1;
    	clrscr();
    	printf("Enter the string 1....");
    	scanf("%s",s1);
     
    	for(i=0;s1[i]!='';i++)
    	{
    		if((s1[i]>='a')&&(s1[i]<='z'))
    			s1[i]=s1[i]-32;
    	}
    	printf("Resultant string is...%s",s1);
    	getch();
    }
  • 088. Write a program to convert givern string to lower case in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i;
    	char *s1;
    	clrscr();
    	printf("Enter the string 1....");
    	scanf("%s",s1);
     
    	for(i=0;s1[i]!='';i++)
    	{
    		if((s1[i]>='A')&&(s1[i]<='Z'))
    			s1[i]=s1[i]+32;
    	}
     
    	printf("Resultant string is...%s",s1);
    	getch();
    }
  • 089. Write a program to concatinate two strings in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,len=0,j;
    	char *s1,*s2;
    	clrscr();
    	printf("Enter the string 1....");
    	scanf("%s",s1);
    	printf("Enter the string 2....");
    	scanf("%s",s2);
     
    	for(i=0;s1[i]!='';i++)
    	{
             len++;
    	}
    	j=0;
    	for(i=len;s2[j]!='';i++)
    	{
    		s1[i]=s2[j];
    		j++;
    	}
    	s1[i]='';
     
    	printf("Resultant string is...%s",s1);
    	getch();
    }
  • 090. Write a program to count no, of alphlabets, digits, special symbols in C language
     
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,alphabets=0,digits=0,symbols=0;
    	char *s;
    	clrscr();
    	printf("Enter the string 1....");
    	scanf("%s",s);
     
    	for(i=0;s[i]!='';i++)
    	{
    		 if(((s[i]>='A')&&(s[i]<='Z'))||((s[i]>='a')&&(s[i]<='z')))
    				alphabets++;
    		 else if((s[i]>='0')&&(s[i]<='9'))
    				digits++;
    		 else
    				symbols++;
    	}
    	printf("nAlphabets : %d",alphabets);
    	printf("nDigits    : %d",digits);
    	printf("nSymbols   : %d",symbols);
    	getch();
    }
  • 091. Write a program to compare two strings in C language
    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
    main()
    {
    	int i;
    	char *s1,*s2;
    	clrscr();
    	printf("Enter the string 1....");
    	scanf("%s",s1);
    	printf("Enter the string 2....");
    	scanf("%s",s2);
     
    	for(i=0;s1[i]!='';i++)
    	{
    		 if(s1[i]!=s2[i])
    			{
    				if(s1[i]>s2[i])
    					printf("nString 1 is greater...");
    				else
    					printf("nString 2 is greater...");
    				getch();
    				exit(0);
    			}
    	}
    	printf("String are equal....");
    	getch();
    }
  • 092. Write a program to count all vowels present in the string in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,vowel=0;
    	char *s;
    	clrscr();
    	printf("Enter the string 1....");
    	scanf("%s",s);
     
    	for(i=0;s[i]!='';i++)
    	{
    		if((s[i]=='A')||(s[i]=='E')||(s[i]=='O')||(s[i]=='U')||(s[i]=='I')||
    		  (s[i]=='a')||(s[i]=='e')||(s[i]=='o')||(s[i]=='u')||(s[i]=='i'))
    						vowel++;
    	}
    	printf("nVowels : %d",vowel);
    	getch();
    }
  • 093. Write a program to reverse the given string in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,len=0;
    	char *s1,*s2;
    	clrscr();
    	printf("Enter the string ....");
    	scanf("%s",s1);
    	for(i=0;s1[i]!='';i++)
    		len++;
    	j=0;
    	for(i=len-1;i>=0;i--)
    	{
    		s2[j]=s1[i];
    		j++;
    	}
    	s2[j]='';
    	printf("nReversed String : %s",s2);
    	getch();
    }
  • 094. Write a program to check if the given string is palindrome or not

    E.g. NITIN, LIRIL, MALAYALAM.

    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
    main()
    {
    	int i,j,len=0;
    	char *s1,*s2;
    	clrscr();
    	printf("Enter the string ....");
    	scanf("%s",s1);
    	for(i=0;s1[i]!='';i++)
    		len++;
    	j=0;
    	for(i=len-1;i>=0;i--)
    	{
    		s2[j]=s1[i];
    		j++;
    	}
    	s2[j]='';
    	for(i=0;s1[i]!='';i++)
    		if(s1[i]!=s2[i])
    		{
    			printf("nNot a palindrome");
    			getch();
    			exit(0);
    	}
    	printf("Palindrome");
    	getch();
    }

More on functions – Theory and Programs [Learn to code]

Learn more theory about Functions. And different programs on functions. What is recursion? And programs on recursion.

  • 063. Write a program to accept two numbers from user and swap their values using call by reference method in C language
    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
     
    swap(int *x ,int *y)
    {
    	int temp;
    	temp=*x;
    	*x=*y;
    	*y=temp;
    }
    main()
    {
    	int a,b;
    	clrscr();
    	printf("Enter numbers : ");
    	scanf("%d%d",&a,&b);
            printf("nBefore Swapping a = %d, b = %dn",a,b);
    	swap(&a,&b);
    	printf("nAfter  Swapping a = %d, b = %dn",a,b);
    	getch();
    }
  • 064. Write a program using recursions for fibbonacci series in C language
     
    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
    fibbo(int pre, int cur ,int x)
    {        int temp;
    	if(x==2)
    	{
    		getch();
    		exit(0);
    	}
    	temp=cur;
    	cur=pre+cur;
    	pre=temp;
    	printf("%d ",cur);
    	fibbo(pre,cur,x-1);
    }
    main()
    {
    	int n,pre=1,cur=1;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	printf("%d %d ",pre,cur);
            fibbo(pre,cur,n);
    	getch();
    }
  • 065. Write a program to print factorial of a given number using recursive function in C language
    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
     
    int fact(int n)
    {
    	int f;
    	if(n==1)
    		return 1;
    	else
    		f=n*fact(n-1);
    	return f;
    }
    main()
    {
    	int n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	printf("nFactorial is...%dn",fact(n));
    	getch();
    }

Arrays – Theory and Programs [Learn to code]

What are Arrays? Learn syntax for Arrays and different programs on Arrays.

  • 066. Write a program to accept a number n from user and then accept n array elements from user and reprint them in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
     int n,i,a[20];
     clrscr();
     printf("Enter number...");
     scanf("%d",&n);
     printf("Enter array elements :n" );
     for(i=0;i<n;i++)
    	{
    		printf("Enter element %d : ",i+1);
    		scanf("%d",&a[i]);
    	}
     for(i=0;i<n;i++)
    		printf("nElement %d : %d",i+1,a[i]);
     getch();
    }
  • 067. Write a program to accept a number n from user and then accept n array elements from user and reprint them in reverse order of inputs in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
     int n,i,a[20];
     clrscr();
     printf("Enter number...");
     scanf("%d",&n);
     printf("Enter array elements :n" );
     for(i=0;i<n;i++)
    	{
    		printf("Enter element %d : ",i+1);
    		scanf("%d",&a[i]);
    	}
     for(i=n-1;i>=0;i--)
    		printf("nElement %d : %d",i+1,a[i]);
     getch();
    }
  • 068. Write a program to accept a number n from user and then accept n array elements from user and print addition of those n array elements in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
     int n,i,a[20],sum=0;
     clrscr();
     printf("Enter number...");
     scanf("%d",&n);
     printf("Enter array elements :n" );
     for(i=0;i<n;i++)
    	{
    		printf("Enter element %d : ",i+1);
    		scanf("%d",&a[i]);
    	}
     
     for(i=0;i<n;i++)
    	sum=sum+a[i];
     printf("nSum of given array elements is : %d",sum);
     getch();
    }
  • 069. Write a program to accept a number n from user and print fibbonacci series up to nth level using arrays in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int a[20],n,i;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	a[1]=a[2]=1;
     
    	for(i=3;i<=n;i++)
    		a[i]=a[i-1]+a[i-2];
     
    	for(i=1;i<=n;i++)
    		printf(" %d",a[i]);
     
    	getch();
    }
  • 070. Write a program to accept a number n from user and then accept n array elements from user and print maximum and minimum array element from that set of array in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int n,i,a[20],max,min;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	printf("Enter array elements...n");
    	for(i=0;i<n;i++)
    	{
    		printf("Enter element %d : ",i+1);
    		scanf("%d",&a[i]);
    	}
    	max=min=a[0];
    	for(i=0;i<n;i++)
    	{
    		if(a[i]>max)
    			max=a[i];
    if(a[i]<min)
    			min=a[i];
    	}
    	printf("nMin : %d",min);
    	printf("nMax : %d",max);
    	getch();
    }
  • 071. Write a program to accept a number n from user and then accept n array elements from user, print positive & Negative numbers separately in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int n,i,a[20];
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	printf("Enter array elements...n");
    	for(i=0;i<n;i++)
    	{
    		printf("Enter element %d : ",i+1);
    		scanf("%d",&a[i]);
    	}
     
    	printf("nnNegative Elements : n");
    	for(i=0;i<n;i++)
    		if(a[i]<0)
    			printf("n%d",a[i]);
     
    	printf("nnPositive Elements : n");
    	for(i=0;i<n;i++)
    		if(a[i]>0)
    			printf("n%d",a[i]);
     
    	getch();
    }
  • 072. Write a program to accept a number n from user and then accept n array elements from user, print these array elements in ascending and descending order in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int n,i,a[20],j,max,min,temp;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	printf("Enter array elements...n");
    	for(i=0;i<n;i++)
    	{
    	printf("Enter element %d : ",i+1);
    		scanf("%d",&a[i]);
    	}
    	max=a[0];
    	for(i=0;i<n;i++)
    	{
    		for(j=0;j<i;j++)
    			if(a[i]>a[j])
    			{
    				temp=a[i];
    				a[i]=a[j];
    				a[j]=temp;
    			}
    	}
    	printf("Ascending order.....n");
    	for(i=0;i<n;i++)
    	{
    		printf(" %d  ",a[i]);
    	}
     
    	min=a[0];
    	for(i=0;i<n;i++)
    	{
    		for(j=0;j<i;j++)
    			if(a[i]<a[j])
    			{
    				temp=a[i];
    				a[i]=a[j];
    				a[j]=temp;
    			}
    	}
    	printf("nDescending order....n");
    	for(i=0;i<n;i++)
    	{
    		printf(" %d  ",a[i]);
    	}
     
    	getch();
    }
  • 073. Write a program to accept a mXn matrix and reprint it in matrix form in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,m,n,a[5][5];
    	clrscr();
    	printf("Enter order of matrix :n");
            scanf("%d%d",&m,&n);
    	printf("Enter matrix elements.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			scanf("%d",&a[i][j]);
     
    	for(i=0;i<m;i++)
    	{
    		for(j=0;j<n;j++)
    			printf("%d ",a[i][j]);
    		printf("n");
    	}
    	getch();
    }
  • 074. Write a program to accept two M x N matrices and add them in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,m,n,a[5][5],b[5][5],c[5][5];
    	clrscr();
    	printf("Enter order of matrix :n");
    	scanf("%d%d",&m,&n);
    	printf("Enter matrix elements of first matrix.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			scanf("%d",&a[i][j]);
     
    	printf("Enter matrix elements of second matrix.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			scanf("%d",&b[i][j]);
     
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			c[i][j]=a[i][j]+b[i][j];
     
    	printf("Resultant matrix .....n");
    	for(i=0;i<m;i++)
    	{
    		for(j=0;j<n;j++)
    			printf("%d ",c[i][j]);
    		printf("n");
    	}
    	getch();
    }
  • 075. Write a program to accept a M x N matrix and print addition of their array elements in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,m,n,a[5][5],sum=0;
    	clrscr();
    	printf("Enter order of matrix :n");
    	scanf("%d%d",&m,&n);
    	printf("Enter matrix elements.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			scanf("%d",&a[i][j]);
     
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			sum=sum+a[i][j];
     
    	printf("nSum of matrix elements.....%d",sum);
    	getch();
    }
  • 076. Write a program to accept a M x N matrix and print addition of diagonal elements of that matrix in C language
     
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,m,a[5][5],sum=0;
    	clrscr();
    	printf("Enter order of matrix :n");
    	scanf("%d",&m);
    	printf("Enter matrix elements.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<m;j++)
    			scanf("%d",&a[i][j]);
     
    	for(i=0;i<m;i++)
    		for(j=0;j<m;j++)
    			if(i==j)
    				sum=sum+a[i][j];
     
    	printf("nSum of diagonal elements.....%d",sum);
    	getch();
    }
  • 077. Write a program to accept a M x N matrix and print addition of upper triangular matrix elements in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,m,a[5][5],sum=0;
    	clrscr();
    	printf("Enter order of matrix :n");
    	scanf("%d",&m);
    	printf("Enter matrix elements.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<m;j++)
    			scanf("%d",&a[i][j]);
     
    	for(i=0;i<m;i++)
    		for(j=0;j<m;j++)
    			if(i<j)
    				sum=sum+a[i][j];
     
    	printf("nSum of upper triangular elements.....%d",sum);
    	getch();
    }
  • 078. Write a program to accept a M x N matrix and print addition of lower triangular matrix elements in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,m,a[5][5],sum=0;
    	clrscr();
    	printf("Enter order of matrix :n");
    	scanf("%d",&m);
    	printf("Enter matrix elements.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<m;j++)
    			scanf("%d",&a[i][j]);
     
    	for(i=0;i<m;i++)
    		for(j=0;j<m;j++)
    			if(i>j)
    				sum=sum+a[i][j];
     
    	printf("nSum of lower triangular elements.....%d",sum);
    	getch();
    }
  • 079. Write a program to accept two M X N matrices and print their multiplication in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,m,n,k,a[5][5],b[5][5],c[5][5];
    	clrscr();
    	printf("Enter order of matrix :n");
    	scanf("%d%d",&m,&n);
    	printf("Enter matrix elements of first matrix.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			scanf("%d",&a[i][j]);
     
    	printf("Enter matrix elements of second matrix.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			scanf("%d",&b[i][j]);
     
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    		{
    			c[i][j]=0;
    			for(k=0;k<n;k++)
    				c[i][j]=c[i][j]+a[i][k]*b[k][j];
    		}
     
    	printf("Resultant matrix .....n");
    	for(i=0;i<m;i++)
    	{
    		for(j=0;j<n;j++)
    			printf("%d ",c[i][j]);
    		printf("n");
    	}
    	getch();
    }
  • 080. Write a program to accept a M x N matrix and print it’s transpose matrix in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,m,n,a[5][5];
    	clrscr();
    	printf("Enter order of matrix :n");
    	scanf("%d%d",&m,&n);
    printf("Enter matrix elements.....n");
    	for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    			scanf("%d",&a[i][j]);
     
    	for(i=0;i<m;i++)
    	{
    		for(j=0;j<n;j++)
    			printf("%d ",a[j][i]);
    		printf("n");
    	}
    	getch();
    }

Functions – Theory and Programs [Learn to code]

What are Functions? Learn syntax for Functions, types of Functions and programs on Functions.

  • 060. Write a program to accept two numbers from user and print it’s addition,subtraction,multiplication,division using different functions in C language
     
    #include<stdio.h>
    #include<conio.h>
     
    int add(int x,int y)
    {
     return(x+y);
    }
     
    int sub(int x,int y)
    {
     return(x-y);
    }
     
    int mul(int x,int y)
    {
     return(x*y);
    }
     
    int div(int x,int y)
    {
     return(x/y);
    }
     
    main()
    {
    	int a,b;
    	clrscr();
    	printf("Enter numbers : n");
    	scanf("%d%d",&a,&b);
    	printf("nAddition       : %dn",add(a,b));
    	printf("nSubtraction    : %dn",sub(a,b));
    	printf("nMultiplication : %dn",mul(a,b));
    	printf("nDivision       : %dn",div(a,b));
    getch();
    }
  • 061. Write a program to accept a number from user and print it’s factorial, check if it prime or not , and print it’s fibbonacci series using different functions in C language
     
    #include<stdio.h>
    #include<conio.h>
     
    fact(int x)
    {
    	int i,fact=1;
    	for(i=1;i<=x;i++)
    		fact=fact*i;
    	printf("Factorial is : %d",fact);
    }
     
    IsPrime(int x)
    {
    	int i;
    	for(i=2;i<x-1;i++)
    	{
    		if(x%i==0)
    			{
    			printf("nNot a Prime Numbern");
    			return 0;
    			}
    	}
    	printf("nIt is a Prime numbern");
    }
     
    fibbo(int x)
    {
    	int pre=1,cur=1,i,temp;
    	printf("%d %d",pre,cur);
    	for(i=3;i<=x;i++)
    	{
    		temp=cur;
    		cur=pre+cur;
    		pre=temp;
    		printf(" %d",cur);
    	}
    }
    main()
    {
    	int n;
    	clrscr();
    	printf("Enter number...");
    	scanf("%d",&n);
    	fact(n);
    	IsPrime(n);
    	fibbo(n);
            getch();
    }
  • 062. Write a program to calculate square and cube of a given number in C language
     
    #include<stdio.h>
    #include<conio.h>
     
    int square(int x)
    {
    	return(x*x);
    }
     
    int qube(int x)
    {
    	return(x*x*x);
    }
    main()
    {
    	int n;
    	clrscr();
    	printf("Enter number.....");
    	scanf("%d",&n);
    	printf("nSquare : %dn",square(n));
    	printf("nQube   : %dn",qube(n));
    	qube(n);
    	getch();
    }

Case Control Structure – Theory and Programs [Learn to code]

What is Case Control Structure? Learn syntax for Case Control Structure, types of Case Control Structure and different programs on Case Control Structure.

  • 057. Write a program to accept a single value interger from user and print that integer in words in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int n;
    	clrscr();
    	printf("Enter number...");
    	scanf("%1d",&n);  //%1d to scan single integer
    	switch(n)
    	{
    		case 0 : printf("Zero");break;
    		case 1 : printf("One");break;
    		case 2 : printf("Two");break;
    		case 3 : printf("Three");break;
    		case 4 : printf("Four");break;
    		case 5 : printf("Five");break;
    		case 6 : printf("Six");break;
    		case 7 : printf("Seven ");break;
    		case 8 : printf("Eigth ");break;
    		case 9 : printf("Nine ");break;
    	}
    	getch();
    }
  • 058. Write a program to accept a number from user and print that number in words but in reverse order in C language

    E.g. 153 -> THREE FIVE ONE

    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int n,rem;
    	clrscr();
    	printf("Enter number...");
    	scanf("%d",&n);
    	while(n>0)
    	{
    		rem=n%10;
    		switch(rem)
    		{
    		  case 0 : printf("Zero");break;
    		  case 1 : printf("One");break;
    		  case 2 : printf("Two");break;
    		  case 3 : printf("Three");break;
    		  case 4 : printf("Four");break;
    		  case 5 : printf("Five");break;
    		  case 6 : printf("Six");break;
                      case 7 : printf("Seven");break;
    		  case 8 : printf("Eigth");break;
    		  case 9 : printf("Nine");break;
    		}
    		n=n/10;
     
    	}
    	getch();
    }
  • 059. Write a Program to accept two numbers and a operator (+, -, *, / from user and complete that particular operation only in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int n,rem,a,b;
    	char op;
    	clrscr();
    	printf("Enter Operator(+,-,*,/)...");
    	scanf("%c",&op);
    	printf("Enter numbers...");
    	scanf("%d%d",&a,&b);
    	switch(op)
    	{
    	case '+' : printf("Result : %d ",a+b);break;
    	case '-' : printf("Result : %d ",a-b);break;
    	case '*' : printf("Result : %d ",a*b);break;
    	case '/' : printf("Result : %d ",a/b);break;
    	default  : printf("Invalid operator....");
    	}
    	getch();
    }

For Loop – Advance Programs – Programs [Learn to code]

Learn programming logic to write advanced programs on for loop.

  • 047. Write a program to accept a number from user and print it’s factorial in C language

    Eg: factorial of 5 is:-
    5! = 5 * 4 * 3 * 2 * 1=120

    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,fact=1,n;
    	clrscr();
    	printf("Enter number...");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    		fact=fact*i;
    	printf("facttorialof the given number is...%d",fact);
    	getch();
     
    }
  • 048. Write a program to accept a number from user and print if it is prime or not in C language
    #include<conio.h>
    #include<process.h>
    main()
    {
    	int i,n;
    	clrscr();
    	printf("Enter number...");
    	scanf("%d",&n);
    	for(i=2;i<=n/2;i++)
    	{
    		if(n%i==0)
    		{
    		printf("Not Prime");
    		getch();
    		exit(0);
    		}
    	}
    	printf("Prime ");
    	getch();
    }
  • 049. Write a program to accept a number and print prime numbers between 2 and n in C language
     
    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
    main()
    {
    	int i,flag=1,n,newn;
    	clrscr();
    	printf("Enter number...");
    	scanf("%d",&n);
    	for(newn=2;newn<=n;newn++)
    	{
    		flag=1;
    		for(i=2;i<=newn/2 ;i++)
    		{
    			if(newn%i==0)
    			{
    			flag=0;
    			break;
    			}
    		}
    		if(flag==1)
    			printf("%d ",newn);
    	}
    	getch();
    }
  • 050. Write a program to accept a number and print fibonacci series upto that level in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int pre=1,cur=1,temp,i,n;
            clrscr();
    	printf("Enter number...");
    	scanf("%d",&n);
    	printf("%dt%d",pre,cur);
    	for(i=3;i<=n;i++)
    		{
    		 temp=cur;
    		 cur=pre+cur;
    		 pre=temp;
    		 printf("t%d",cur);
    		}
    	getch();
    }
  • 051. Write a program to print digits, alphabets in capital and lower case in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i;
    	clrscr();
    	for(i=65;i<=90;i++)
    		  printf("%c  ",i);
     
    	printf("nnnn");
    	for(i=97;i<=122;i++)
    		  printf("%c  ",i);
     
    	printf("nnnn");
    	for(i=48;i<=57;i++)
    		  printf("%ct",i);
     
    	getch();
    }
  • 052. Write a program to accept a number n from user and Add n terms of the series in C language

    1/2! + 2/3! + 3/4! + 4/5! + 5/6! + ………

     
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,n;
    	float sum=0,fact=1;
    	clrscr();
    	printf("Enter number....");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    	{
    		fact=1;
    		for(j=1;j<=i+1;j++)
    			fact=fact*j;
    	sum=sum+i/fact;
    	}
    	printf("Sum of the series....%f",sum);
    	getch();
    }
  • 053. Write a program to read n numbers (Xi) from the user and print out their average and standard deviation. Formulae are

    Average = (Sum of Xi)/n

     
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int n,i,newn,sum_avg=0;
    	float avg;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	printf("Enter numbers...");
    	for(i=1;i<=n;i++)
    	{
    		scanf("%d",&newn);
    		sum_avg=sum_avg+newn;
    	}
    	avg=sum_avg/(float)n; //Type casting.
     
    	printf("nAverage = %f",avg);
    	getch();
    }
  • 054. Write a program to print out ASCII chart on a single screen (all 256 characters from 0 to 255) in a tabular form. The ASCII code should be followed by the corresponding character in C language
     
    #include<stdio.h>
    #include<conio.h>
    main()
    {
     int i;
     clrscr();
     for(i=0;i<=255;i++)
    	printf("%d-%ct",i,i);
     getch();
    }
  • 055. Write a program to print following output
    ******************************* 
    *                             *
    *                             *  
    *                             *
    *******************************
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j;
    	clrscr();
    	for(i=1;i<=80;i++)
    		printf("*");
    	for(i=2;i<=23;i++)
    	{
    		printf("*");
    		for(j=2;j<=79;j++)
    			printf(" ");
    		printf("*");
    	}
    	for(i=1;i<=80;i++)
    		printf("*");
     
    	getch();
     
    }
  • 056. Write a program to print following output

    #include<stdio.h>
    #include<conio.h>
    main()
    {
            int i, j;
            clrscr();
            printf("╔");
            for(i=2;i<=79;i++)
                    printf("═");
            printf("╗");
            for(i=2;i<=24;i++)
            {
                    printf("║");
                    for(j=2;j<=79;j++)
                            printf(" ");
                    printf("║");
            }
            printf("╚");
            for(i=2;i<=79;i++)
                    printf("═");
            printf("╝");
     
            getch();
    }
  • 057. Write a program to accept a single value interger from user and print that integer in words in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int n;
    	clrscr();
    	printf("Enter number...");
    	scanf("%1d",&n);  //%1d to scan single integer
    	switch(n)
    	{
    		case 0 : printf("Zero");break;
    		case 1 : printf("One");break;
    		case 2 : printf("Two");break;
    		case 3 : printf("Three");break;
    		case 4 : printf("Four");break;
    		case 5 : printf("Five");break;
    		case 6 : printf("Six");break;
    		case 7 : printf("Seven ");break;
    		case 8 : printf("Eigth ");break;
    		case 9 : printf("Nine ");break;
    	}
    	getch();
    }

For Loop – Pattern Programming – Theory and Programs [Learn to code]

What is pattern programming? Learn how to create different patterns using for loop.

  • 034. Write a program to print following outputs in C language
    * * * * * *
    * * * * * *
    * * * * * *
    * * * * * *
    * * * * * *
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    	{
    		for(j=1;j<=n;j++)
    			{
    			 printf("*");
    			}
    		printf("n");
    	}
    	getch();
    }
  • 035. Write a program to print following outputs in C language
    *
    * *
    * * *
    * * * *
    * * * * *
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    	{
    		for(j=1;j<=i;j++)
    			{
    			 printf("*");
    			}
    		printf("n");
    	}
    	getch();
    }
  • 036. Write a program to print following outputs in C language
    * * * * *
    * * * *
    * * *
    * *
    *
    
    #include<stdio.h> 
    #include<conio.h>
    main()
    {
    	int i,j,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=n;i>=1;i--)
    	{
    		for(j=1;j<=i;j++)
    			{
    			 printf("*");
    			}
    		printf("n");
    	}
    	getch();
    }
  • 037. Write a program to print following outputs in C language
    * * * * *
      * * * *
        * * *
          * *
            *
    
    #include<stdio.h> 
    #include<conio.h>
    main()
    {
    	int i,j,k,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    	{
    		for(k=1;k<i;k++)
    			{
    			 printf(" ");
    			}
    		for(j=i;j<=n;j++)
    			{
    			 printf("*");
    			}
    		printf("n");
    	}
    	getch();
    }
  • 038. Write a program to print following outputs in C language
            *
          * *
        * * *
      * * * *
    * * * * *
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,k,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=n;i>=1;i--)
    	{
    		for(k=1;k<=(n-i);k++)
    			{
    			 printf(" ");
    			}
    		for(j=1;j<=i;j++)
    			{
    			 printf("*");
    			}
    		printf("n");
    	}
    	getch();
    }
  • 039. Write a program to print following outputs in C language
            *
          * * *
        * * * * *
      * * * * * * *
    * * * * * * * * *
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,k,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    	{
    		for(k=1;k<=(n-i);k++)
    			{
    			 printf(" ");
    			}
    		for(j=1;j<=i;j++)
    			{
    			 printf("*");
    			}
    		for(j=2;j<=i;j++)
    			{
    			 printf("*");
    			}
     
    		printf("n");
    	}
    	getch();
    }
  • 040. Write a program to print following outputs in C language
    * * * * * * * * *
      * * * * * * *
        * * * * *
          * * *   
            *
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,k,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=n;i>=1;i--)
    	{
    		for(k=1;k<=(n-i);k++)
    			{
    			 printf(" ");
    			}
    		for(j=1;j<=i;j++)
    			{
    			 printf("*");
    			}
    		for(j=2;j<=i;j++)
    			{
    			 printf("*");
    	}
     
    		printf("n");
    	}
    	getch();
    }
  • 041. Write a program to print following outputs in C language
            *
          * * *
        * * * * *
      * * * * * * *
    * * * * * * * * *
      * * * * * * *
        * * * * *
          * * *   
            *
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,k,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
     
    	for(i=1;i<=n;i++)
    	{
    		for(k=1;k<=(n-i);k++)
    			{
    			 printf(" ");
    			}
    		for(j=1;j<=i;j++)
    			{
    			 printf("*");
    			}
    		for(j=2;j<=i;j++)
    			{
    			 printf("*");
    			}
     
    		printf("n");
    	}
     
    	for(i=n-1;i>=1;i--)
    	{
    		for(k=1;k<=(n-i);k++)
    			{
    			 printf(" ");
    			}
    		for(j=1;j<=i;j++)
    			{
    			 printf("*");
    			}
    		for(j=2;j<=i;j++)
    			{
    			 printf("*");
    			}
     
    		printf("n");
    }
    	getch();
    }
  • 042. Write a program to print following outputs in C language
    1
    12
    123
    1234
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,k,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    	{
    		for(j=1;j<=i;j++)
    			printf("%d",j);			
    		printf("n");
    	}
    	getch();
    }
  • 043. Write a program to print following outputs in C language
    1
    22
    333
    4444
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,k,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    	{
    		for(j=1;j<=i;j++)
    			printf("%d",i);
    		printf("n");
    	}
    	getch();
    }
  • 044. Write a program to print following outputs in C language
       A
      BBB
     CCCCC
    DDDDDDD
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,k,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	//ASCII Code for 'A' is 65
    	for(i=1;i<=n;i++)
    	{
    		for(k=1;k<=(n-i);k++)
    			{
    			 printf(" ");
    			}
    		for(j=1;j<=i;j++)
    			{
    			 printf("%c",64+i);
    			}
    		for(j=2;j<=i;j++)
    			{
    			 printf("%c",64+i);
    			}
     
    		printf("n");
    	}
    	getch();
    }
  • 045. Write a program to print following outputs in C language
    ABCDEEDCBA
    ABCD  DCBA
    ABC    CBA
    AB      BA
    A        A
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
      int i,n,j,k;
      clrscr();
      printf("Enter number........");
      scanf("%d",&n);
      for(i=n-1;i>=0;i--)
      {
    		for(j=0;j<=i;j++)
    			printf("%c",65+j);
    		for(k=1;k<(n-i);k++)
    			printf("  ");
    		for(j=i;j>=0;j--)
    			printf("%c",65+j);
    	printf("n");
      }
      getch();
    }
  • 046. Write a program to print following outputs in C language
    1
    11
    121
    1231
    12341
    123451
    
     
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,n;
    	clrscr();
    	printf("Enter number...");
    	scanf("%d",&n);
    	for(i=0;i<=n;i++)
    	{
    		for(j=1;j<=i;j++)
    		{
    			printf("%d",j);
    		}
    		printf("1n");
    	}
    	getch();
    }