Thursday, 27 March 2014

Write a program to read in an array of names and to sort them in alphabetical order.

#include<stdio.h>
#include<string.h>
main()
{
         int num,i,j;
         char name[100][100],temp[100];
         printf("Enter a number of names=");
         scanf("%d",&num);
         printf("#%d\n",num);
         for(i=0;i<num;i++)
         {
                  printf("\nEnter name %d=",i+1);
                  scanf("%s",&name[i]);
         }
         printf("\nThe names are\n");
         for(i=0;i<num;i++)
         {
                  printf("\nName %d=",i+1);
                  puts(name[i]);
         }
         for(i=0;i<num;i++)
         {
                  for(j=0;j<num-1-i;j++)
                  {
                           if(strcmp(name[j],name[j+1])>0)
                           {
                                    strcpy(temp,name[j]);
                                    strcpy(name[j],name[j+1]);        
                                    strcpy(name[j+1],temp);
                           }
                  }
         }
         printf("\nThe names after sorting are\n");
         for(i=0;i<num;i++)
         {
                  printf("\nName %d=",i+1);
                  puts(name[i]);
         }
}

Write a C program to print the Floyd’s triangle given below 1 2 3 4 5 6 7 8 9 10 … … … … …

#include<stdio.h>
void main()
{
int num,i,j,k=0;
printf("Enter number of lines=");
scanf("%d",&num);
printf("\nFloyd’s triangle\n");
for(i=1;i<=num;i++)
{
for(j=1;j<=i;j++)
{
k++;
printf("%d\t",k);
}
printf("\n");
}
}

Write a C program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by a given integer x.

#include<stdio.h>
main()
{
int i,con=0,sum=0,num;
printf("Enter a number=");
scanf("%d",&num);
for(i=100;i<=200;i++)
{
if(i%num==0)
{
con++;
sum=sum+i;
}
}
printf("\nNumber of integers=%d\n",con);
printf("\nSum of integers=%d\n",sum);
}

Wednesday, 26 March 2014

Write a C program to count the of words in a file named "file.txt". [A word is a sequence of letters ending with a blank, or a tab, or an end of line marker or end of file or punctuation symbols such as ",", ".", "!" and "?".].

#include<stdio.h>

main()
{
    int count=0;
    char ch;
    FILE *fp;
    fp=fopen("file.txt","r");
    if(fp==NULL)
        printf("\nFile not found");
    else
    {
        do
        {
            ch=fgetc(fp);
            if(ch==' ' ||ch=='\n' || ch==',' || ch=='.' || ch=='?' || ch=='!' || ch=='\t' || ch==EOF)
            {
                count++;
            }
        }while(ch!=EOF);
       
    }
    printf("\nNumber of words=%d",count);
   
}

Define a structure of employees of an organization with the following fields: Empno, Empname, Date_of_joining, Salary, Department Write a program which accepts details of ten employees and print them on the screen.

#include<stdio.h>


struct employees
{
    int Empno;
    char Empname[100], Date_of_joining[20], Department[100];
    float Salary;
};

main()
{   
    int i;
    struct employees e1[10];
    for(i=0;i<10;i++)
    {
        printf("\nEnter details of employee %d=",i+1);
        printf("\nEmployee number=");
        scanf("%d",&e1[i].Empno);
        printf("\nEmployee name=");
        scanf("%s",e1[i].Empname);
        printf("\nDate of joining=");
        scanf("%s",e1[i].Date_of_joining);
        printf("\nDepartment=");
        scanf("%s",e1[i].Department);
        printf("\nSalary=");
        scanf("%f",&e1[i].Salary);
    }
   
    for(i=0;i<10;i++)
    {
        printf("\n\nDetails of employee %d=",i+1);
        printf("\n\tEmployee number=%d",e1[i].Empno);
        printf("\n\tEmployee name=%s",e1[i].Empname);
        printf("\n\tDate of joining=%s",e1[i].Date_of_joining);
        printf("\n\tDepartment=%s",e1[i].Department);
        printf("\n\tSalary=%.2f",e1[i].Salary);
    }
   
}

Write a recursive function to compute factorial of a number.

#include<stdio.h>

int factorial(int n)
{
    if(n<=1)
        return 1;
    else
        return n*factorial(n-1);   
}
main()
{
    int a,b;
    printf("\nEnter a number=");
    scanf("%d",&a);
    printf("\nfactorial %d is %d ",a,factorial(a));
}

Thursday, 20 March 2014

Write a C program to exchange two numbers without using temporary variable.

#include<stdio.h>

main()
{
    int a,b;
    printf("\nEnter two numbers=");
    scanf("%d%d",&a,&b);
    printf("\nBefore exchange a=%d,b=%d",a,b);
    a=a+b;
    b=a-b;
    a=a-b;
    printf("\nAfter exchange a=%d,b=%d",a,b);
}

Write a C program to exchange two numbers using temporary variable.

#include<stdio.h>

main()
{
    int a,b,temp;
    printf("\nEnter two numbers=");
    scanf("%d%d",&a,&b);
    printf("\nBefore exchange a=%d,b=%d",a,b);
    temp=a;
    a=b;
    b=temp;
    printf("\nAfter exchange a=%d,b=%d",a,b);
}

Write a C program to find the HCF and LCM of two numbers.

#include<stdio.h>

main()
{
    int a,b,temp,i;
    printf("\nEnter two numbers=");
    scanf("%d%d",&a,&b);
    if(a>b)
    {
        temp=a;
        a=b;
        b=temp;
    }
   
    for(i=a;i>=1;i--)
    {
        if(a%i==0 && b%i==0)
        {
            break;
        }
    }
    printf("\nHCF=%d",i);
    printf("\nLCM=%d",a*b/i);
}

Wednesday, 12 March 2014

Write a C program to write a sentence into a file.

#include<stdio.h>
main()
{
    int i;
    char str[100];
    FILE *fp;
    fp=fopen("out.txt","w");
    printf("\nEnter a sentence=");
    gets(str);
    fprintf(fp,"%s",str);
    fclose(fp);
}

Define a self referential structure for representing a simple linked list of integers. Write a C function to split the list into two lists so that the first list contains all even numbered elements and the second list contains only odd numbered elements. For example, if the original list is {2, 8, 1, 13, 14, 28, 0} then the resultant list first list would be {2, 8, 14, 28, 0} and the second list would be { 1, 13}.

#include<stdio.h>
#include<malloc.h>
struct node
{
    int info;
    struct node *link;
};

void create_list(struct node **start,int data)
{
    struct node *q,*tmp;
    tmp= malloc(sizeof(struct node));
    tmp->info=data;
    tmp->link=NULL;
    if(*start==NULL)
        *start=tmp;
    else
    {
        q=(*start);
        while(q->link!=NULL)
        {
            q=q->link;
        }
        q->link=tmp;
    }
}

void display(struct node *start)
{
    struct node *q;
    if(start==NULL)
    {
        printf("List is empty\n");
        return;
    }
    q=start;
   
    while(q!=NULL)
    {
        printf("%d ", q->info);
        q=q->link;
    }
    printf("\n");
}

void split_list(struct node *start,struct node **even,struct node **odd)
{
    struct node *q;
    q=start;
    while(q!=NULL)
    {
        if(q->info%2==0)
        {
            create_list(&(*even),q->info);
        }
        else
            create_list(&(*odd),q->info);       
        q=q->link;
    }   
}

main()
{
    struct node *start=NULL,*even=NULL,*odd=NULL;
    int n,i,m;
    printf("Number of terms of 1st list ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("Enter the element : ");
        scanf("%d",&m);
        create_list(&start,m);
    }
    printf("List is :\n");
    display(start);
    split_list(start,&even,&odd);
    printf("Even List is :\n");
    display(even);
    printf("Odd List is :\n");
    display(odd);
}

Define a structure for a student having name, roll number, and marks obtained in six subjects. Assume that “allstudents” is an array of students. Write a C program to print the name and roll numbers of the students who have secured highest marks in each subject.

#include<stdio.h>
struct student
{
    char name[100];
    int rollno,sub1,sub2,sub3,sub4,sub5,sub6;
};

main()
{
    struct student allstudent[100];
    int n,i,max;
    printf("\nEnter number of students=");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("\nEnter name of student %d=",i+1);
        scanf("%s",allstudent[i].name);
        printf("\nEnter roll number of student %d=",i+1);
        scanf("%d",&allstudent[i].rollno);
        printf("\nEnter marks of student %d=",i+1);
        printf("\nSubject 1=");
        scanf("%d",&allstudent[i].sub1);
        printf("\nSubject 2=");
        scanf("%d",&allstudent[i].sub2);
        printf("\nSubject 3=");
        scanf("%d",&allstudent[i].sub3);
        printf("\nSubject 4=");
        scanf("%d",&allstudent[i].sub4);
        printf("\nSubject 5=");
        scanf("%d",&allstudent[i].sub5);
        printf("\nSubject 6=");
        scanf("%d",&allstudent[i].sub6);
    }
    printf("\nName\tRoll Number\tSubject 1\tSubject 2\tSubject 3\tSubject 4\tSubject 5\tSubject 6");
    for(i=0;i<n;i++)
    {
        printf("\n%s",allstudent[i].name);
        printf("\t\t%d",allstudent[i].rollno);
        printf("\t\t%d",allstudent[i].sub1);
        printf("\t\t%d",allstudent[i].sub2);
        printf("\t\t%d",allstudent[i].sub3);
        printf("\t\t%d",allstudent[i].sub4);
        printf("\t\t%d",allstudent[i].sub5);
        printf("\t\t%d\n",allstudent[i].sub6);       
    }

    max=0;
    for(i=1;i<n;i++)
    {
        if(allstudent[max].sub1<allstudent[i].sub1)
        {
            max=i;
        }
    }
    printf("\nThe maximum mark in subject1");
    printf("\nName=%s,Roll Number=%d,Mark=%d",allstudent[max].name,allstudent[max].rollno,allstudent[max].sub1);
   
    max=0;
    for(i=1;i<n;i++)
    {
        if(allstudent[max].sub2<allstudent[i].sub2)
        {
            max=i;
        }
    }
    printf("\nThe maximum mark in subject2");
    printf("\nName=%s,Roll Number=%d,Mark=%d",allstudent[max].name,allstudent[max].rollno,allstudent[max].sub2);
   
    max=0;
    for(i=1;i<n;i++)
    {
        if(allstudent[max].sub3<allstudent[i].sub3)
        {
            max=i;
        }
    }
    printf("\nThe maximum mark in subject3");
    printf("\nName=%s,Roll Number=%d,Mark=%d",allstudent[max].name,allstudent[max].rollno,allstudent[max].sub3);
   
    max=0;
    for(i=1;i<n;i++)
    {
        if(allstudent[max].sub4<allstudent[i].sub4)
        {
            max=i;
        }
    }
    printf("\nThe maximum mark in subject4");
    printf("\nName=%s,Roll Number=%d,Mark=%d",allstudent[max].name,allstudent[max].rollno,allstudent[max].sub4);
   
    max=0;
    for(i=1;i<n;i++)
    {
        if(allstudent[max].sub5<allstudent[i].sub5)
        {
            max=i;
        }
    }
    printf("\nThe maximum mark in subject5");
    printf("\nName=%s,Roll Number=%d,Mark=%d",allstudent[max].name,allstudent[max].rollno,allstudent[max].sub5);
    max=0;
    for(i=1;i<n;i++)
    {
        if(allstudent[max].sub6<allstudent[i].sub6)
        {
            max=i;
        }
    }
    printf("\nThe maximum mark in subject6");
    printf("\nName=%s,Roll Number=%d,Mark=%d",allstudent[max].name,allstudent[max].rollno,allstudent[max].sub6);
}

Tuesday, 11 March 2014

Write a function that accepts an array and constant parameters and count how many elements of array are less than the constant, equal to it and greater than it. Return these three values either by way of pass by reference parameter or a three element array parameter.

#include<stdio.h>
int* count(int a[],int n,int ele)
{
    int i,c[3]={0,0,0};
    for(i=0;i<n;i++)
    {
        if(a[i]==ele)
            c[0]++;
        else if(a[i]<ele)
            c[1]++;
        else
            c[2]++;
    }
    return c;
}

main()
{
    int a[100],n,i,*cnt,item;
    printf("\nEnter number of terms=");
    scanf("%d",&n);
    printf("\nEnter the integers=");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    printf("\nThe array elements are=");
    for(i=0;i<n;i++)
    {
        printf("%d ",a[i]);
    }
    printf("\nEnter a number=");
    scanf("%d",&item);
    cnt=count(a,n,item);   
    printf("\nNumber of elements equale to %d=%d\nNumber of elements less than %d=%d\nNumber of elements greater than %d=%d\n",item,cnt[0],item,cnt[1],item,cnt[2]);   
}

Monday, 10 March 2014

Write a ‘C’ function that returns the k-th digit from the right in the positive integer n. For example, digit(829,1) returns 9, digit(829,3) returns 8. If k is greater than the number of digits in n then the function is to return –1. Include appropriate documentation in your program.

#include<stdio.h>
int digit(int,int);

main()
{
int num,pos,dg;
printf("\nEnter a number=");
scanf("%d",&num);
printf("\nEnter the position=");
scanf("%d",&pos);
dg=digit(num,pos);
if(dg==-1)
printf("\nInvalid position");
else
printf("\nDigit found in position %d is %d",pos,dg);
}

int digit(int n,int p)
{
int i=1;
while(i<p && n>=0)
{
n=n/10;
i++;
}
if(n==0)
return -1;
else
return n%10;
}

Write the following function: int search(int a[], int n, int x); Where a is an array to be searched, n is the number of elements in the array, and x is the search key. “search” should return 1 if x matches some element of a, 0 if it doesn’t. Use pointer arithmetic to visit array elements. Include appropriate documentation in your program.

#include<stdio.h>

int search(int a[],int n,int x);

main()
{
    int a[100],i,n,ele,f;
    printf("\nEnter how many terms=");
    scanf("%d",&n);
    printf("\nEnter the elements=");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("\nThe elements are=");
    for(i=0;i<n;i++)
    {
        printf("%d ",*(a+i));
    }
    printf("\nEnter the element to be searched=");
    scanf("%d",&ele);
    f=search(a,n,ele);
    if(f==-1)
        printf("\nElement not found");
    else
        printf("\nElement found in position %d",f);
}

int search(int a[],int n,int x)
{
    int i,f=0;
    for(i=0;i<n;i++)
    {
        if(a[i]==x)
        {
            return 1;
        }
    }
    return 0;
}

Write a function "int count_factor(int)" to print the factors and return number of factors of a given number. Then using that function write another function "void isprime(int)" to check if it is a prime number or not.Write a function "int count_factor(int)" to print the factors and return number of factors of a given number. Then using that function write another function "void isprime(int)" to check if it is a prime number or not.

#include<stdio.h>
int count_factor(int n)
{
    int i=1,con=0;
    printf("\nThe factors are=");
    while(i<=n)
    {
        if(n%i==0)
        {
            con++;
            printf(" %d ",i);
        }
        i++;
    }
    return con;
}

void isprime(int n)
{
    if(count_factor(n)==2)
        printf("\n%d is a prime number\n");
    else
        printf("\n%d is a prime number\n");
}
main()
{
    int num;
    printf("\nEnter a number=");
    scanf("%d",num);
    printf("\nNumber of factors=",count_factor(num));
    isprime(num);
}

Write a program to sort n integers using bubble sort using function.

#include<stdio.h>

void insert(int a[],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
}

void show(int a[],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("%d  ",a[i]);
    }
}

void bubblesort(int a[],int n)
{
    int i,j,k,temp;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }       
        }
    }
}
main()
{
    int arr[100],n,i,ele;
    printf("\nEnter number of terms=");
    scanf("%d",&n);   
    printf("\nEnter the numbers=");
    insert(arr,n);
    printf("\nThe array is\n");
    show(arr,n);
    bubblesort(arr,n);
    printf("\nAfter sorting=");
    show(arr,n);
}

Write a program to sort n integers using bubble sort.

#include<stdio.h>
main()
{
    int a[100],n,i,j,k,temp;
    printf("\nEnter number of terms=");
    scanf("%d",&n);
    printf("\nEnter the integers=");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("\nThe unsorted array is=");
    for(i=0;i<n;i++)
    {
        printf("%d ",a[i]);
    }


    for(i=0;i<n;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }
}

Write a C program to read n elements in an array and find the maximum element of the array.

#include <stdio.h>
main()
{
    int arr[100],i,j,n,max=0;
    printf("\nEnter the number of elements : ");
    scanf("%d",&n);
    printf("\nEnter the elements :");
    for(i=0;i<n;i++)
    {       
        scanf("%d",&arr[i]);
    }
    printf("\nThe list is :");
    for (i=0;i<n;i++)
    {
        printf("%9d ", arr[i]);
    }
    printf("\n");
    for (i=1;i<n;i++)
    {
        if(arr[max]<arr[i])
        {
            max=i;
        }
    }
    printf("\nThe maximum element of the array=%d",arr[max]);
}

Write a C program to read n elements in an array and find the minimum element of the array.

#include <stdio.h>
main()
{
    int arr[100],i,j,n,min=0;
    printf("\nEnter the number of elements : ");
    scanf("%d",&n);
    printf("\nEnter the elements :");
    for(i=0;i<n;i++)
    {       
        scanf("%d",&arr[i]);
    }
    printf("\nThe list is :");
    for (i=0;i<n;i++)
    {
        printf("%9d ", arr[i]);
    }
    printf("\n");
    for (i=1;i<n;i++)
    {
        if(arr[min]>arr[i])
        {
            min=i;
        }
    }
    printf("\nThe minimum element of the array=%d",arr[min]);
}

Write a C program to calculate the sum of all digits of a given integer.

#include<stdio.h>
main()
{
    int num,sum=0;
    printf("\nEnter a number=");
    scanf("%d",&num);
    while(num)
    {
        sum=sum+num%10;
        num/=10;
    }
    printf("\nThe sum =%d",sum);
}

Write a C program to rear n elements in an array and calculate the sum of all the elements of the array.

#include <stdio.h>
main()
{
    int arr[100],i,j,n,sum=0;
    printf("\nEnter the number of elements : ");
    scanf("%d",&n);
    printf("\nEnter the elements :");
    for (i = 0; i < n; i++)
    {       
        scanf("%d", &arr[i]);
    }
    printf("\nThe list is :");
    for (i = 0; i < n; i++)
        printf("%9d ", arr[i]);
    printf("\n");
    for (i = 0; i < n; i++)
        sum=sum+arr[i];
    printf("\nThe sum of the array elements=%d",sum);
}

Program of sorting using insertion sort

#include <stdio.h>
main()
{
    int arr[100],i,j,n,temp;
    printf("Enter the number of elements : ");
    scanf("%d",&n);
    for (i = 0; i < n; i++)
    {
        printf("Enter element %d : ",i+1);
        scanf("%d", &arr[i]);
    }
    printf("Unsorted list is :\n");
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");

    for(j=1;j<n;j++)
    {
        temp=arr[j];
        for(i=j-1;i>=0 && temp<arr[i];i--)
            arr[i+1]=arr[i];
        arr[i+1]=temp;
    }
    printf("Sorted list is :\n");
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

Sunday, 9 March 2014

Write a C program to reverse a number using function.

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

int reverse(int a)
{
    int rev=0;
    while(a)
    {
        rev=rev*10+a%10;
        a=a/10;
    }   
    return rev;
}

Write a program to check whether a number is palindrom or not.

#include<stdio.h>
main()
{
    int num,rev=0,rem,temp;
    printf("\nEnter a number=");
    scanf("%d",&num);
    temp=num;
    while(num)
    {
        rem=num%10;
        rev=rev*10+rem;
        num/=10;
    }
    if(temp==rev)
        printf("\n %d is a palindrom number",temp);
    else
        printf("\n %d is not a palindrom number",temp);
}

Write a C program to sort n strings entered by the user.

#include<stdio.h>
#include<string.h>

main()
{
    int i=0,j,n;
    char ptr[100][100],temp[100];
    printf("\nEnter number of words=");
    scanf("%d",&n);
    printf("\nEnter the strings\n");
    for(i=0;i<n;i++)
    {
        scanf("%s",ptr[i]);
    }
    printf("\nThe unsorted strings are\n");
    for(i=0;i<n;i++)
    {
        printf("%s\n",ptr[i]);
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(strcmp(ptr[j],ptr[j+1])>0)
            {
                strcpy(temp,ptr[j]);
                strcpy(ptr[j],ptr[j+1]);
                strcpy(ptr[j+1],temp);
            }
        }
    }
    printf("\nThe sorted strings are\n");
    for(i=0;i<n;i++)
    {
        printf("%s\n",ptr[i]);
    }
}

strcpy

#include<stdio.h>
main()
{
    char str1[30],str2[30];
    int i=0;
    printf("Enter the first string : ");
    gets(str1);

    printf("Enter the second string : ");
    gets(str2);

    CopyString(str1,str2);
    printf("Now the first string is same as the second string \n");
    printf("First string is %s \n",str1);
    printf("Second string is %s \n",str2);

}/*End of main() */

CopyString(char *str1,char *str2)
{
    while(*str2!='\0')
    {
          *str1= *str2;
          str1++;
          str2++;
    }
    *str1='\0';
}/*End of CopyString*/

Write the following function: int search(int a[], int n, int x); Where a is an array to be searched, n is the number of elements in the array, and x is the search key. “search” should return 1 if x matches some element of a, 0 if it doesn’t. Use pointer arithmetic to visit array elements. Include appropriate documentation in your program.

#include<stdio.h>

int search(int a[],int n,int x);

main()
{
    int a[100],i,n,ele,f;
    printf("\nEnter how many terms=");
    scanf("%d",&n);
    printf("\nEnter the elements=");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("\nThe elements are=");
    for(i=0;i<n;i++)
    {
        printf("%d ",*(a+i));
    }
    printf("\nEnter the element to be searched=");
    scanf("%d",&ele);   
    f=search(a,n,ele);
    if(f==-1)
        printf("\nElement not found");
    else
        printf("\nElement found in position %d",f);
}

int search(int a[],int n,int x)
{
    int i,f=0;
    for(i=0;i<n;i++)
    {
        if(a[i]==x)
        {
            return i;           
        }
    }
    return -1;
}

Program to calculate area of rectangle. Length and breadth given by the user

#include<stdio.h>

main()
{
    int l,b,area;
    printf("\nEnter length and breadth=");
    scanf("%d%d",&l,&b);
    area=l*b;
    printf("\nThe area=%d",area);
}

Friday, 7 March 2014

Write a C program to find out greatest of three numbers. The numbers are given by the user.

#include<stdio.h>
main()
{
    int a,b,c;
    printf("\nEnter three numbers=");
    scanf("%d%d%d",&a,&b,&c);
    if(a>b && a>c)
    {
        printf("\n%d is greatest",a);
    }
    else if(b>c)
    {
        printf("\n%d is greatest",b);
    }
    else
    {
        printf("\n%d is greatest",c);
    }
}

Write a ‘C’ function that returns the k-th digit from the right in the positive integer n. For example, digit(829,1) returns 9, digit(829,3) returns 8. If k is greater than the number of digits in n then the function is to return –1. Include appropriate documentation in your program.

#include<stdio.h>
int digit(int,int);

main()
{
int num,pos,dg;
printf("\nEnter a number=");
scanf("%d",&num);
printf("\nEnter the position=");
scanf("%d",&pos);
dg=digit(num,pos);
if(dg==-1)
printf("\nInvalid position");
else
printf("\nDigit found in position %d is %d",pos,dg);
}

int digit(int n,int p)
{
int i=1;
while(i<p && n>=0)
{
n=n/10;
i++;
}
if(n==0)
return -1;
else
return n%10;
}

Write a program to check whether a number is prime or not.

#include<stdio.h>

main()
{
    int num,i=2,f=0;   
    printf("\nEnter a number=");
    scanf("%d",&num);
    if(num==1)
        printf("\n%d is neither prime nor composite",num);
    else
    {
        while(i<=num/2)
        {
            if(num%i==0)
            {
                f=1;
                break;               
            }
            i++;
        }
        if(f==0)
            printf("\n%d is a prime number",num);
        else
            printf("\n%d is a composite number",num);
    }           
}

While purchasing certain items a discount of 10% is offered if the quantity purchased is more than 2000. Write a program to calculate the total expanses, if quantity and price per item are entered through keyboard.

#include<stdio.h>

float discount(float p);

int main()
{
    float p,total,q;
    printf("\nEnter quantity=");
    scanf("%d",&q);
    printf("\nEnter price per item=";
    scanf("%d",&p);
    total=p*q-discount(p*q);
    printf("\nTotal expanses=",total);
    return 0;
}

float discount(float p)
{   
    if(p>=2000)
        return p*0.10;
    else
        return 0;
}

Write a C program to find out whether a year is leap year or not.

#include<stdio.h>
main()
{
    int year;
    printf("\nEnter a year=");
    scanf("%d",&year);
    if((year%4==0 && year%100!=0) || year%400==0)
        printf("%d is a leap year",year);
    else
        printf("%d is not a leap year",year);
}

Monday, 3 March 2014

Write a C program to create a matrix and display it. Using function.

#include<stdio.h>
void create(int a[][100],int r,int c)
{
    int i,j;
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
}

void display(int a[][100],int r,int c)
{
    int i,j;
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            printf("%9d ",a[i][j]);
        }
        printf("\n");
    }
}

main()
{
    int a[100][100],r,c,i,j;
    printf("\nEnter row number=");
    scanf("%d",&r);
    printf("\nEnter column number=");
    scanf("%d",&c);
    printf("\nEnter the matrix elements\n");
    create(a,r,c);   
    printf("\nThe matrix is\n");
    display(a,r,c);
}

Write a C program to create a matrix and display it.

#include<stdio.h>

main()
{
    int a[100][100],r,c,i,j;
    printf("\nEnter row number=");
    scanf("%d",&r);
    printf("\nEnter column number=");
    scanf("%d",&c);
    printf("\nEnter the matrix elements\n");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
   
    printf("\nThe matrix is\n");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            printf("%9d ",a[i][j]);
        }
        printf("\n");
    }
}

Write a C program to calculate sum of first n odd integers.

#include<stdio.h>

main()
{
    int num,sum=0,i=1;
    printf("\nEnter a number=");
    scanf("%d",&num);
    while(i<=num)
    {       
        sum+=2*i-1;
        i++;
    }   
    printf("\nThe sum =%d",sum);
}

Sunday, 2 March 2014

Write a C program to reverse a number.

#include<stdio.h>

main()
{
    int num,rev=0,rem;
    printf("\nEnter a number=");
    scanf("%d",&num);
    while(num)
    {
        rem=num%10;
        rev=rev*10+rem;
        num/=10;
    }
   
    printf("\nThe reverse =%d",rev);
}