Showing posts with label c language. Show all posts
Showing posts with label c language. Show all posts

C Programming Previous Year Question Paper for Automobile, Mechanical, Production or Metallurgy

Here i have uploaded the previous year (2014 November) question paper of Programming in C for Third semester for Automobile Engineering, Mechanical Engineering, Production Engineering or Metallurgy branches under MG university BTech course.

Course : B.Tech Engineering (degree)
University: MG university (Mahatma Gandhi university) Kottayam, Kerala
Department or branch: Automobile Engineering, Mechanical Engineering, Production Engineering or Metallurgy

C Program Using 8086 Interrupts to Restrict Mouse Pointer Into a Circle of Given Center and Radius

C Program Using 8086 Interrupts to Restrict Mouse Pointer Into a Circle of Given Center and Radius. The mouse ponter will be restricted to a circle of user specified center and radius using the interrupt 33 of 8086 in c complier. I have tested this program in Turbo C compiler.
How to restrict mouse cursor or pointer within a user specified circle Using 8086 INT33 service interrupts arrow mice radius centre Turbo c program Turbo C++ code source code without thread screen rectangle
Mouse pointer restricted into a circle in Turbo C


#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>

Storage Classes for Variables in C Programming Language

Every variable in C programming language has two properties; type and storage class. 'Type' refers to the data type of the variable such as integer, character, floating point values etc. It also deals with the size of the variable ( in bytes). 'Storage class' determines the part of memory where storage is allocated for the variable and how long the storage allocation continues to exist. It also determines the scope which specifies the part of the program over which a variable name is visible, i.e. the variable is accessible by name. The scope restriction can be overrode by making use of pointers. Whatever, in short, storage class is the property that determines the part of memory where storage is allocated, the lifetime of variable and the scope of the variable.

There are for storage classes in C programming:

  • Automatic
  • Register
  • External
  • Static

The ASCII Character Set or ASCII Chart

ASCII stands for American Standard Code for Information Interchange. ASCII is a 7 bit alphanumeric code used in computers. Each character is assigned with a positive integer (in between 0 and 127). This is the most popular coding system. The following is the ASCII chart of characters and corresponding integer value.

C and C++ Programs for String Sorting

Here i am writing programs in c and c++ languages to sort a given number of strings. Sorting is carried out based on the ASCII values of the letters in the strings. The string comparing function strcmp() is used in the program. These programs sort a given set of strings in ascending order.To change it into descending order just replace > with < in comparison line. The comparison line in the code is marked by comment.

C Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{

C and C++ swapping numbers

The following are C and C++ programs to swap the values of two variables using a third variable.

C Program:
#include<stdio.h>
void main()
{
int x,y,temp;
printf("Enter the value of xand y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\n x= %d\n y = %d\n",x,y);
temp = x;
x= y;
y = temp;
printf("After Swapping\n x= %d\ny= %d\n",x,y);
}

2 Different C and C++ Programs to Check Whether a Given String is Palindrome or Not

Here i am writing two different programs to check whether a given string (text) us palindrome or not. There are C and C++ programs for this.

C Programs:

(i) Using string functions:




#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char a[100],b[100];
printf("Enter the string to check if palindrome or not\n");
gets(a);
strcpy(b,a);
strrev(b);
if(strcmp(a,b)==0)
printf("\nEntered string is palindrome");
else
printf("\nEntered string is not palindrome");
getch();
} 

(ii) Comparing Character by Character


#include<string.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int len,i;
char a[100];
printf("Enter the string to check whether it is palindrome or not\n");
gets(a);
len=strlen(a);
for(i=0;i<len/2;i++)
{
if(a[i]!=a[len-i-1])
break;
}

if(i==len/2)
printf("\nEntered string is palindrome");
else
printf("\nEntered string is not palindrome");
getch();
}

C++ Programs:

(i) Using String functions:


C and C++ Program to Check Whether a Given Number is Even or Odd

C and C++ programs to check whether a given number is Odd or even

C Program

#include<stdio.h>
void main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
if ( n%2 == 0 )
printf("Even");
else
printf("Odd");
}

C++ Program

#include<iostream.h>
void main()
{
int n;
cout<<"Enter an integer\n";
cin>>n;
if ( n%2 == 0 )
cout<<"\nEven";
else
cout <<"\nOdd";
}


C and C++ Program to Check Whether a Given Year is Leap Year or Not

This is post contains a C program and a C++ program to check whether an input year is leap year or not. A year is a leap year if:

  1. The year is divisible by 400
  2. The year is divisible by 4 but not divisible by 100.
Any other year is not a leap year.

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


C++ program
#include <iostream.h>
void main()
{
int year;
cout<<"Enter a year to check if it is a leap year or not";
cin>>year;
if(year%400==0 || ((year%4=0) && (year%100!=0)))
cout<<"\n"<<year<<"is a leap year."; else
cout<<"\n"<<year<<"is NOT a leap year."; 
}

C program to Calculate Permutations and Combinations (nPr and nCr)

This is a C program to calculate total number of permutations and combinations for given values of n and r. This program calculates nPr and nCr using functions.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
long double fact( int);
int ncr( int ,int);
long npr( int ,int);
main()
{
int n,r;
printf(" Enter value of n & r \n");
scanf("%d %d",&n ,&r);
if( n>= r)
{
printf( "%d C %d is %d \n",n,r,ncr( n ,r));
printf("%d P %d is %ld\n",n,r,npr( n, r));
}
else
{
printf("\n n should be greater than r");
getch ();
exit(0);
}
}


C Program to Check Whether a Given String is Palindrome or Not

Here i am writing two different programs two check whether an input string is palindrome or not. The first program checks the string manually character by chatter from both ends. The other program make use of a string function strrev() (under string.h) to reverse the string. Therefore, the second program is more compact.

Program 1:

#include<stdio.h>

#include<string.h>

void main()

{

char a[20];

int i, len;

printf("\nEnter the string:\n");

gets(a);

len = strlen(a);

for (i = 0; i < len / 2; i++)

{

if (a[i] != a[len - i - 1])

break;

}

Sizeof operator in C

Sizeof operator is a unary operator used in both C and C++ programming languages. This operator is used to get the size of a data type, variable, object of a class, structure variable etc in bytes. In some programs it may be necessary to use the size of certain variable or data type. Such situations often occur in file handling programs. But the size of data type may vary from machine to machine. In most of the 32 bit systems, int has a size of 4 bytes. But in some systems it is 2 bytes. So, if a program is written by assuming the size to be known, it will affect the portability of the program since a change in size of a data type may result in unexpected output. In such cases, the sizeof operator helps to obtain the size of data type or variable in bytes. The argument of sizeof can be a variable of primitive days types (int, char etc), an object (of class), a structure variable, union variable, pointer variables etc. The sizeof operator is very useful in dynamic memory allocation.

C program to show working of sizeof operator:

#include<stdio.h>

struct student

{

char name[5];

int roll;

int mysize;

};

main()
{
unsigned long int b;
printf("size of variable b: %d",sizeof b);
printf("\nsize of int:%d",sizeof(int));
printf("\nsize of float:%d",sizeof(float));
printf("\nsize of char:%d",sizeof(char));
printf("\nsize of long:%d",sizeof(long));
printf("\nsize of double:%d",sizeof(double));
printf("\nsize of short:%d",sizeof(short));
struct student s1;
s1.mysize=sizeof(s1.name)+sizeof(s1.roll)+sizeof(s1.mysize);
printf("\nSizeof structure student :%d",s1.mysize);
/*see the difference shown in size of structure */
printf("\nsize of structure student : %d",sizeof(struct student));
}

Output:

C Program to Sort a Matrix Column wise using Pointers

This is a c program to sort a mxn matrix column wise in ascending order using pointers. To sort column wise in descending order, just replace the '>' symbol in the comparing line of code into '<'. That part of the code is marked with a comment 'comparison'.

#include<stdio.h>

main()

{

int a[7][7],i,j,k,m,n,temp;

//reading
printf("enter number of rows of matrix\n");
scanf("%i",&m);
printf("enter number of columns of matrix\n");
scanf("%i",&n);
printf("enter the elements\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
//displaying
printf("the matrix you entered:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%4d",*(*(a+i)+j));
}
}

C and C++ Program to Display Pascal's Triangle

This post gives C and C++ programs to display given number of rows of pascal's triangle. Thenuser can enter the number of rows of pascal triangle to be displayed.

C Program:

#include<stdio.h>

#include<conio.h>

long factorial(int);

main()

{

int i, n, c;

printf("Enter the number of rows you wish to see in pascal triangle\n");

scanf("%d", &n);

for (i = 0; i < n; i++)

{

for (c = 0; c <= (n - i - 2); c++)

printf(" ");

for (c = 0; c <= i; c++)

printf("%ld ", factorial(i) / (factorial(c) * factorial(i - c)));

printf("\n");

}

getch();

}