Thursday, December 24, 2015

Write a program to find the biggest and smallest element in an array using pointers

#include<stdio.h>
#include<conio.h>
int main()
{
int a[5], i, smallestNo, biggestNo, *p;
clrscr();
p = &a[0];
printf("Enter Any 5 Numbers: \n");
for(i=0;i<5;i++,p++)
scanf("%d",p);
p = &a[0];
smallestNo = *p;
biggestNo = *p;
for(i=0;i<5;i++,p++)
{
if(*p<smallestNo)
smallestNo = *p;
if(*p>biggestNo)
biggestNo = *p;
}
printf("\n Smallest Number is %d ",smallestNo);
printf("\n Biggest Number is %d ",biggestNo);
getch();
}

To Compile Press Alt+f9 
To Run Press Ctrl+f9

Output:



Wednesday, November 25, 2015

Reads a sentence and prints frequency of each of the vowels and total count of consonants

#include<stdio.h>
#include<conio.h>
int main(){
    char text[100];
    int i,vowel=0,cons=0;
    clrscr();
    printf("Enter a string:\n");
    gets(text);
    for(i=0;text[i]!='\0';++i)
    {
if(text[i]=='a' || text[i]=='e' || text[i]=='i' || text[i]=='o' || text[i]=='u' || text[i]=='A' || text[i]=='E' || text[i]=='I' || text[i]=='O' || text[i]=='U')
{
   ++vowel;
}
else if((text[i]>='a'&& text[i]<='z') || (text[i]>='A'&& text[i]<='Z'))
{
   ++cons;
}
    }
    printf("Vowels: %d",vowel);
    printf("\nConsonants: %d",cons);
    getch();
    return 0;
}

To Compile Press Alt+f9 
To Run Press Ctrl+f9

Output:



Copy String Without Using Library Function

#include<stdio.h>
#include<conio.h>
void STRCOPY(char[],char[]);
int main()
{
     char str1[100], str2[100];
     clrscr();
     printf("Enter String to Copy: \n");
     gets(str1);
     STRCOPY(str1,str2);
     printf("Copied Sentence : %s", str2);
     getch();
     return 0;
}

void STRCOPY(char str1[],char str2[])
{
     int i=0;
     if(str1 != "")
     {
  while(str1[i] != '\0')
  {
     str2[i] = str1[i];
     i++;
  }
     }
     str2[i] = '\0';
}

To Compile Press Alt+f9 
To Run Press Ctrl+f9

Output:



Write a C program to maintain a record of “n” student details using an array of structures with four fields (Roll number, Name, Marks, and Grade). Each field is of an appropriate data type. Print the marks of the student given student name as input

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
struct student{
char name[14];
int rollNo;
int marks[3];
float percentage;
};

int main(){
struct student *stuArray;
int n,i,j;
int tempTotal=0,flag=0,foundIndex;
char tempName[14];
clrscr();
printf("\n No of Students: ");
scanf("%d",&n);
stuArray = (struct student*)malloc(n*sizeof(struct student));
for(i=0;i<n;i++){
printf("\n %d.Name :",(i+1));
scanf("%s",&stuArray[i].name);
printf("\n Roll Number :",(i+1));
scanf("%d",&stuArray[i].rollNo);
tempTotal=0;
for(j=0;j<3;j++)
{
printf("\n Mark %d :",(j+1));
scanf("%d",&stuArray[i].marks[j]);
tempTotal+=stuArray[i].marks[j];
}
stuArray[i].percentage=tempTotal/3;
}

printf("\n Enter the Name to be Searched: ");
scanf("%s",&tempName);
for(i=0;i<n;i++){
if(strcmp(tempName,stuArray[i].name)==0)
{
foundIndex=i;
flag=1;
break;
}
}
if(flag==0)
{
printf("Details Not Found");
}
else
{
printf("\n Mark of %s are given below...",tempName);
for(i=0;i<3;i++)
{
printf("\n Mark %d is %d",(i+1),stuArray[foundIndex].marks[i]);
}
}
getch();
return 0;
}

To Compile Press Alt+f9 
To Run Press Ctrl+f9

Output:


Friday, November 6, 2015

Find The Number Using Linear Search - Working Code

#include<stdio.h>
#include<conio.h>
int main()
{
    int i,num,search,flag,arr[10];
    clrscr();
    printf("Enter the total number of input: \n");
    scanf("%d",&num);
    for(i=0;i<num;i++)
    {
printf("Enter the number %d \n",i+1);
scanf("%d",&arr[i]);
    }
    printf("Enter the number to search \n");
    scanf("%d",&search);
    for(i=0;i<num;i++)
    {
if(arr[i] == search)
{
     printf("The given number %d is located at position %d \n",search, i+1);
     flag = 1;
     break;
}
    }
    if(flag == 0)
       printf("Sorry, the given number %d is not present in the array \n",search);
    getch();
    return 0;
}


To Compile Press Alt+f9 
To Run Press Ctrl+f9

Output:



Monday, November 2, 2015

Sorting Given Number In Ascending Order - Working Code

#include<stdio.h>
#include<conio.h>
void main()
{
   int i,j,num,temp,arr[20];
   clrscr();
   printf("Enter The Total Number Of Values For Sorting: \n");
   scanf("%d",&num);
   for(i=0;i<num;++i)
   {
printf("Enter Number %d: \n",i+1);
scanf("%d",&arr[i]);
   }

   for(i=1;i<num;i++)
   {
for(j=0;j<num;j++)
{
    if(arr[i]<arr[j])
    {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
    }
}
   }
   printf("The Given Number In Ascending Order Are: \n");
   for(i=0;i<num;i++)
   {
       printf("%d\n",arr[i]);
   }
   getch();
}

To Compile Press Alt+f9 
To Run Press Ctrl+f9

Note: For sorting descending number, change arr[i]<arr[j] to arr[i]>arr[j]

Output:


Find Largest Number In A Given Array - Working Code

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,arraySize, arr[10];
    clrscr();
    printf("Enter Values to Insert Into Arrays \n");
    scanf("%d",&arraySize);
    for(i=0;i<arraySize;i++)
    {
       printf("Enter the Number %d:",i+1);
       scanf("%d",&arr[i]);
    }
    for(i=1;i<arraySize;i++)
    {
       if(arr[0] < arr[i])
       arr[0] = arr[i];
    }
    printf("Largest Number is %d",arr[0]);
    getch();
}

To Compile Press Alt+f9 
To Run Press Ctrl+f9

Output:


Tuesday, October 27, 2015

Reverse Number Using Recursion in C - Working Code

#include<stdio.h>
#include<conio.h>
int reverse(int);
void main()
{
    int n,r;
    clrscr();
    printf("Enter a number");
    scanf("%d", &n);
    r = reverse(n);
    printf("%d\n",r);
    getch();
}

int reverse(int n)
{
    static long r = 0;
    if(n == 0)
    {
       return 0;
    }
    r = r * 10;
    r = r + n % 10;
    reverse(n/10);
    return r;
}

To Compile Press Alt+f9 
To Run Press Ctrl+f9

Output:
     

Find Reminder & Quotient in C - Working Code

#include<stdio.h>
#include<conio.h>
void main()
{
   int dividend, divisor, quotient, remainder;
   clrscr();
   printf("Enter the Dividend: ");
   scanf("%d", &dividend);
   printf("Enter the divisor: ");
   scanf("%d", &divisor);
   quotient = dividend/divisor;
   remainder = dividend%divisor;
   printf("Quotient: %d\n",quotient);
   printf("Remainder: %d",remainder);
   getch();
}


To Compile Press Alt+f9 
To Run Press Ctrl+f9

Output:


Monday, October 19, 2015

Find The Given Number is Armstrong in C - Working Code

#include<stdio.h>
#include<conio.h>
void main()
{
   int n, n1, rem, num=0;
   clrscr();
   printf("Enter a positive integer: ");
   scanf("%d", &n);
   n1=n;
   while(n1 != 0)
   {
      rem = n1%10;
      num+=rem*rem*rem;
      n1/=10;
   }
   if(num == n)
   printf("%d is an Armstrong number.",n);
   else
   printf("%d is not an Armstrong number.",n);
   getch();
}

To Compile Press Alt+f9 
To Run Press Ctrl+f9

Output:


Friday, October 16, 2015

Find Remainder Without Modulo operation in C - Working Code

#include<stdio.h>
#include<conio.h>
void main()
{
    int n1, n2, quotient, remainder;
    clrscr();
    printf("Enter two numbers \n");
    scanf("%d%d",&n1,&n2);
    quotient = n1/n2;
    remainder = n1 - quotient * n2;
    printf("%d",remainder);
    getch();
}


To Compile Press Alt+f9 
To Run Press Ctrl+f9

Output:


Reverse Number in C - Working Code

#include<stdio.h>
#include<conio.h>
void main()
{
   int n, reverse=0;
   clrscr();
   printf("Enter any 4 numbers to reverse\n");
   scanf("%d", &n);
   while(n != 0)
   {
      reverse = reverse*10;
      reverse = reverse+n%10;
      n = n/10;
   }
   printf("Reverse of entered number is = %d\n",reverse);
   getch();
}

To Compile Press Alt+f9 
To Run Press Ctrl+f9

Output: 



Thursday, October 15, 2015

String Reverse in C - Working Code

#include<stdio.h>
#include<string.h>
void main()
{
    char str[100], temp;
    int i,j = 0;
    printf("\nEnter the string: ");
    gets(str);
    i = 0;
    j = strlen(str) - 1;
    while(i < j)
    {
       temp = str[i];
       str[i] = str[j];
       str[j] = temp;
       i++;
       j--;
    }
    clrscr();
    printf("\nReverse string is :%s", str);
    getch();
}

To Compile Press Alt+f9 
To Run Press Ctrl+f9

Output: