Sunday 20 March, 2011

Using Pointers (continues)


int  int1     = 1036;   /* some data to point to  */
int  int2     = 8;
int *int_ptr1 = &int1;  /* get addresses of data  */
int *int_ptr2 = &int2;
int_ptr1 = *int_ptr2;
int_ptr1 = int_ptr2;

Changes happen:
1.Type check warning:  *int_ptr2 is not an int *
2.Changes int_ptr1 – doesn’t change int1

Using Pointers (cont.)


int  int1     = 1036;   /* some data to point to  */
int  int2     = 8;
int *int_ptr1 = &int1;  /* get addresses of data  */
int *int_ptr2 = &int2;
*int_ptr1 = int_ptr2;
*int_ptr1 = int2;

int1 becomes 8
Type check warning:  int_ptr2 is not an int

Using Pointers



int  i1;
int  i2;
int *ptr1;

int *ptr2;
i1 = 1;
i2 = 2;

ptr1 = &i1;//ptr1 points to address 0x1000

ptr2 = ptr1;//ptr2 takes address of ptr1 and so ptr2 points to   address 0x1000

*ptr1 = 3;//value of i1(to which ptr1 pointed becomes 3

i2 = *ptr2;//as i1=3,*ptr2=*ptr1=3,so i2 becomes 3


Pointer Operations in C


Creation
& variable  Returns variable’s memory address
Dereference
* pointer  Returns contents stored at address
Indirect assignment
* pointer = val  Stores value at address
Assignment
pointer = ptr  Stores pointer in another variable

eg. 
      if a variable  is a=24 and it is stored at memory address 2000.Consider we want to point a pointer named p at that variable,do following things.
int *p,a=24,b,c;//initializing the pointers
*p=&a;//stores 2000 address to the pointer's address
b=*p;//outputs as '24' as it returns content of pointer
*p=55;//directly stores the value at address 2000
c=p;//stores the pointer
  

Pointers

Special case of bounded-size natural numbers
Maximum memory limited by processor word-size
232 bytes = 4GB, 264 bytes = 16 exabytes
A pointer is just another kind of value
A basic type in C
 
eg. int *ptr
The variable “ptr” is a pointer to an “int”,the datatype.

Memory Addresses


1.Storage cells are typically viewed as being byte-sized
 
Usually the smallest addressable unit of memory
 
Few machines can directly access bits individually
 
Such addresses are sometimes called byte-addresses
 
2.Memory is often accessed as words
 
Usually a word is the largest unit of memory access by a single machine instruction
 
CLEAR’s word size is 8 bytes (= sizeof(long))
 
 A word-address is simply the byte-address of the word’s first byte

Expressions

•Constant
–25+5

•Integral
–M*x assume M and x are both integer

•Float
–Sum/9

•Pointer
–Ptr++

•Relational
–X<=y

•Logical
–A>b && b<c

Data types and operators


•Built-in type
–Integer,float,charetc.
•Derived data types
–Array,pointeretc.
•User defined data type
–Structure, union, class, enumeration.
•Operators
–Arithmetic
–Assignment
–Increment/decrement
–Unary
–Relational
–Ternary
–Logical
–bitwise

Basic Commands

C statement
Printf(“%d”,a); //to print variable "a"

Equivalent C++ statement
Cout<<a;

C statement
Scanf(“%f”,&average); //to get the value and store in vaiable "average"

Equivalent C++ statement
Cin>>average;

C statement
Printf(“total is %d this ”,sum);//to print the variable 'sum'

Equivalent C++ statement
Cout<<“totalis“<<sum<<“ this”;

C statement
Scanf(“%d %f”,&a,&x);//to get and store the value of vaeiables 'a' and 'x'

Equivalent C++ statement
Cin>>a>>x;

GTU syllabus for OOCP

Dear friends,if you are a GTU 4th sem student and OOCP is one of your subject,here is a link of your subject syllabus.Click here to download it.

Introduction of C++

C++ was developed by Bjarne Stroustrup of AT&T to add objectoriented
constructs to the C language. Because object-oriented
methodology was relatively new at the time, and all existing
implementations of object-oriented languages were rather slow and
inefficient (most were interpreters), one goal of C++ was to maintain
the efficiency of C.

Saturday 19 March, 2011

Write a program to create linear linked list interactively and print out the list and total number of items in the list.


#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
}*head,*last,*temp,*n;
void menu(); // to select the choise
void create(); // insert at last
void display(); // to print the link list
int i;
void main()
{
i=0;
clrscr();
menu();
getch();
}
void menu()
{
int ch;
printf("1. Create\n");
printf("2. Display\n");
printf("3. Exit\n");
printf("Enter choise : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : create();
case 2 : display();
case 3 : printf("You have choose to exit\n");
getch(); exit(0);
default : printf("Invalid choise\n"); menu();
}
}
void create()
{
n= new node;
printf("Enter the information : ");
scanf("%d",&n->info);
if(i==0)
{
head=n;
i++;
}
else
{
last->next=n;
i++;
}
last=n;
last->next=NULL;
menu();
}
void display()
{
if(head==NULL)
{
printf("No information in list\n");
menu();
}
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->info);
temp=temp->next;
}
printf("\n");
printf("Total number of elements in the link list are : %d\n",i);
menu();
}
Output:
1. Create
2. Display
3. Exit
Enter choise : 1
Enter the information : 12
1. Create
2. Display
3. Exit
Enter choise : 1
Enter the information : 23
1. Create
2. Display
3. Exit
Enter choise : 1
Enter the information : 45
1. Create
2. Display
3. Exit
Enter choise : 2
12->23->45->
Total number of elements in the link list are : 3
1. Create
2. Display
3. Exit
Enter choise : 3
You have choose to exit

Write a program to read data from keyboard, write it to a file named STUDENT again read the same data from STUDENT file and write it into DATA file. Same data should be displayed on the screen


#include<stdio.h>
#include<conio.h>
#include<process.h>
struct stud
{
int rno;
char *nm;
};
void main()
{
struct stud *s;
int n,i;
FILE *fp,*fp1;
clrscr();
printf("Enter how many record you want to input : ");
scanf("%d",&n);
s=(struct stud *)malloc(n*sizeof(struct stud));
fp=fopen("STUD.txt","w");
for(i=0;i<n;i++)
{
printf("\n\tInformation for student : %d\n",i+1);
printf("Enter Roll No : ");
scanf("%d",&s[i].rno);
printf("Enter Name : ");
fflush(stdin);
gets(s[i].nm);
fprintf(fp,"%5d %-20s\n",s[i].rno,s[i].nm);
}
fclose(fp);
fp=fopen("STUD.txt","r");
fp1=fopen("DATA.txt","w");
printf("\nContent of the STUD.txt file is\n");
printf("Roll No Name\n");
printf("---------------------------\n");
while(!feof(fp))
{
fscanf(fp,"%5d %20s\n",&s[i].rno,s[i].nm);
fprintf(fp1,"%5d %-20s\n",s[i].rno,s[i].nm);
printf("%7d %-20s\n",s[i].rno,s[i].nm);
}
fcloseall();
getch();
}
Output:
Enter how many record you want to input : 3
Information for student : 1
Enter Roll No : 1
Enter Name : happy
Information for student : 2
Enter Roll No : 2
Enter Name : Umesh
Information for student : 3
Enter Roll No : 3
Enter Name : Mehul
Content of the STUD.txt file is
Roll No Name
---------------------------
1 happy
2 Umesh
3 Mehul

Write a function using pointers to add two matrices and to return the resultant matrix to the calling function.


#include<stdio.h>
#include<conio.h>
int a[5][5],b[5][5],row,col;
void add(int(*)[5]);
int main()
{
int c[5][5],i,j;
clrscr();
printf("Enter row : ");
scanf("%d",&row);
printf("Enter column : ");
scanf("%d",&col);
printf("Enter matrix A :\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter matrix B :\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&b[i][j]);
}
}
add(c);
printf("Addition :\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
return 0;
}
void add(int c[5][5])
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
/*
Enter row : 3
Enter column : 3
Enter matrix A :
333
333
333
Enter matrix B :
555
555
555
Addition :
8
8
8
8
8
8
8
8
8

Saturday 12 March, 2011

Write a function to calculate the roots of the quadratic equation. The function must use two pointer parameters, one to receive the coefficients a, b, and c, and the other to send the roots to the calling function

#include<stdio.h>
#include<conio.h>
void solve(int*,float*);
int main()
{
int a[3];
float *root;
clrscr();
printf("Enter the value of A : ");
scanf("%d",&a[0]);
printf("Enter the value of B : ");
scanf("%d",&a[1]);
printf("Enter the value of C : ");
scanf("%d",&a[2]);
solve(a,root);
if(root==NULL)
printf("The root is not possible\n");
else if(root[0]==root[1])
printf("The root is %.2f\n",root[0]);
else
printf("The roots are %.2f & %.2f\n",root[0],root[1]);
getch();
return 0;
}
void solve(int *a,float *r)
{
float d;
d=a[1]*a[1]-4*a[0]*a[2];
if(d<0)
{
r=NULL;
}
else if(d==0)
{
r[0]=r[1]=-a[1]/(2*a[0]);
}
else
{
r[0]=-a[1]/(2*a[0]);
r[1]=-a[1]/(2*a[0]);
}
}
Output:
Enter the value of A : 1
Enter the value of B : -4
Enter the value of C : 4
The root is 2.00

Write a program using pointers to read an array of integers and print its elements in reverse order

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr,i,n;
clrscr();
printf("Enter the no of elements:");
scanf("%d",&n);
ptr=(int *)malloc(sizeof(int)*n);
if(ptr==NULL)
{
printf("Not enough memory");
exit(1);
}
for(i=0; i<n; i++)
{
printf("Enter %d element : ",i+1);
scanf("%d",&ptr[i]);
}
printf("Array in original order\n");
for(i=0; i<n; i++)
{
printf("%d\n",ptr[i]);
}
printf("Array in reverse order\n");
for(i=n-1; i>=0; i--)
{
printf("%d\n",ptr[i]);
}
getch();
return 0;
}
Output:
Enter the no of elements:5
Enter 1 element : 12
Enter 2 element : 56
Enter 3 element : 89
Enter 4 element : 45
Enter 5 element : 23
Array in original order
12
56
89
45
23
Array in reverse order
23
45
89
56
12

In a program declare following structure member: name, code, age, weight and height. Read all members of the structure for 100 persons and find list of persons with all related data whose weight > 50 and height > 40 and print the same with suitable format and title

#include<stdio.h>
#include<conio.h>
#define size 3
struct
{
char nm[20],cd;
int height,age,weight;
}s[size];
int main()
{
int i;
clrscr();
for(i=0;i<size;i++)
{
printf("For person %d\n",i+1);
printf("Enter the name : ");
fflush(stdin);
gets(s[i].nm);
printf("Enter the code : ");
flushall(.);
s[i].cd=getc(stdin);
printf("Enter the age : ");
fflush(stdin);
scanf("%d",&s[i].age);
printf("Enter the weight : ");
fflush(stdin);
scanf("%d",&s[i].weight);
printf("Enter the height : ");
fflush(stdin);
scanf("%d",&s[i].height);
}
printf("\n\nData having weight>50 & height>40\n");
printf("\nName Code Age Height Weight\n");
printf("--------------------------------\n");
for(i=0;i<size;i++)
{
if(s[i].weight>50 && s[i].height>40)
printf("%-10s%-5c%3d%7d%7d\n",
s[i].nm,s[i].cd,s[i].age,s[i].height,s[i].weight);
}
getch();
return 0;
}
Output:
For person 1
Enter the name : radhe
Enter the code : D
Enter the age : 21
Enter the weight : 65
Enter the height : 144
For person 2
Enter the name : Mehul
Enter the code : R
Enter the age : 22
Enter the weight : 42
Enter the height : 45
For person 3
Enter the name : Umesh
Enter the code : S
Enter the age : 21
Enter the weight : 55
Enter the height : 35
Data having weight>50 & height>40
Name Code Age Height Weight
--------------------------------
radhe D 21 144 65

Define a structure called cricket that will describe the following information: Player name Team name Batting average Using cricket, declare an array player with 50 elements and wire a program to read the information about all the 50 players and print a list.

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct cricket
{
char nm[20],team[20];
int avg;
};
#define total 5
int main()
{
struct cricket player[total],temp;
int i,j;
clrscr();
for(i=0;i<total;i++)
{
printf("For player %d\n",i+1);
printf("Enter the name of player : ");
fflush(stdin);
gets(player[i].nm);
printf("Enter the team : ");
fflush(stdin);
gets(player[i].team);
printf("Enter the batting average : ");
fflush(stdin);
scanf("%d",&player[i].avg);
}
printf("\nTeam Name Average\n");
printf(" \n");
for(i=0;i<total;i++)
{
printf("%-10s %-10s %7d\n",player[i].team,player[i].nm,player[i].avg);
}
getch();
return 0;
}
Output:
For player 1
Enter the name of player : radhe
Enter the team : India
Enter the batting average : 100
For player 2
Enter the name of player : Tiwari
Enter the team : Pakistan
Enter the batting average : 5
For player 3
Enter the name of player : Tendulkar
Enter the team : India
Enter the batting average : 45
For player 4
Enter the name of player : Dhoni
Enter the team : India
Enter the batting average : 48
For player 5
Enter the name of player : Yuvi
Enter the team : India
Enter the batting average : 39
Team Name Average
-----------------------------
India radhe 100
Pakistan Tiwari 5
India Tendulkar 45
India Dhoni 48
India Yuvi 39

Write a program that search an item from array of string

#include<stdio.h>
#include<conio.h>
#include<string.h>
int string(char[],char[]);
int main()
{
char str[100],find[20];
int i;
clrscr();
printf("Enter the string :");
gets(str);
printf("Enter the srting that you want to find : ");
gets(find);
i=string(str,find);
if(i==1)
printf("%s is found in %s",find,str);
else
printf("%s is not found in %s",find,str);
getch();
return 0;
}
int string(char str[20], char find[20])
{
int i,j,flag;
for(i=0;str[i];i++)
{
if(str[i]==find[0])
{
flag=1;
for(j=1;find[j];j++)
{
if(str[i+j]!=find[j])
{
flag=0;
break;
}
}
if(flag==1)
break;
}
}
return flag;
}
Output:
Enter the string :radhe
Enter the string that you want to find : r
r is found in string
Enter the string :kano
Enter the string that you want to find : z
z is not found in kano

Write a function to upper the string

#include<stdio.h>
#include<conio.h>
void reverse(char[]);
int main()
{
char str[20];
clrscr();
printf("Enter string : ");
gets(str);
reverse(str);
getch();
return 0;
}
void reverse(char str[20])
{
int i;
printf("%s in reverse order is ",str);
for(i=strlen(str)-1;i>=0;i--)
printf("%c",str[i]);
}
Output:
Enter string : radhe
radhe in reverse order is ehdar

Write a function that will scan a character string passed as an argument and convert all lower-case character into their upper-case equivalent

#include<stdio.h>
#include<conio.h>
void upper(char[]);
int main()
{
char str[20];
clrscr();
printf("Enter string : ");
gets(str);
upper(str);
getch();
return 0;
}
void upper(char str[20])
{
int i;
printf("%s in upper case is ",str);
for(i=0;str[i];i++)
{
if(str[i]>96 && str[i]<123)
str[i]-=32;
}
printf("%s",str);
}
Output:
Enter string :string
string in upper case is STRING

Write a function which returns 1 if the given number is palindrome otherwise returns 0

#include<stdio.h>
#include<conio.h>
int pelindrome(int);
int main()
{
int n,p;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
p=pelindrome(n);
if(p==1)
printf("%d is pelindrome",n);
else
printf("%d is not pelindrome",n);
getch();
return 0;
}
int pelindrome(int n)
{
char a[6],b[6];
itoa(n,a,10);
strcpy(b,a);
strrev(b);
if(strcmp(a,b)==0)
return 1;
else
return 0;
}
Output:
Enter a number : 123
123 is not pelindrome
Enter a number : 121
121 is palindrome

Write a function prime that returns 1 if its argument is a prime no. and returns 0 otherwise

#include<stdio.h>
#include<conio.h>
int prime(int);
int main()
{
int n,p;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
p=prime(n);
if(p==1)
printf("%d is prime\n",n);
else
printf("%d is not prime\n",n);
getch();
return 0;
}
int prime(int n)
{
int i;
for(i=2;i<n;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
Output:
Enter a number : 18
18 is not prime
Enter a number : 5
5 is prime

Use recursive calls to evaluate f(x) = x – x3/3! + x5/5! – x7/7! + ……

#include<stdio.h>
#include<conio.h>
int main()
{
float series(float,int),x;
int n;
clrscr();
printf("\nEnter X:");
scanf("%f",&x);
printf("\nEnter n:");
scanf("%d",&n);
printf("\nAns %f",series(x,n));
getch();
return 0;
}
float series(float x,int n)
{
long factorial(int);
float power(float,int);
float sum=0;
int i,s=1;
for(i=1;i<=n;i+=2)
{
sum+=(power(x,i)/factorial(i))*s;
s*=-1;
}
return sum;
}
float power(float x, int y)
{
float p=1;
int i;
for(i=1;i<=y;i++)p*=x;
return p;
}
long factorial(int p)
{
long f=1;
int i;
for(i=1;i<=p;i++)f*=i;
return f;
}
Output
Enter X:1.2
Enter n:5
Ans 0.932736

Write a program that finds a given word in a string

#include<stdio.h>
#include<conio.h>
main()
{
char str[100],find[100];
int i,j,flag;
clrscr();
printf("Enter the string :");
gets(str);
printf("Enter the srting that you want to find : ");
gets(find);
for(i=0;str[i];i++)
{
if(str[i]==find[0])
{
flag=1;
for(j=1;find[j];j++)
{
if(str[i+j]!=find[j])
{
flag=0;
break;
}
}
if(flag==1)
break;
}
}
if(flag==1)
printf("%s is found in %s at %d position",find,str,i+1);
else
printf("%s is not found in %s",find,str);
getch();
}
Output:
Enter the string :The Cat & The Dog
Enter the srting that you want to find : Dog
Dog is found in The Cat & The Dog at 15 position

Write a program that will read a string and rewrite it in the alphabetical order. i.e. the word STRING should be written as GINRST

#include<stdio.h>
#include<conio.h>
main()
{
char str[100],temp;
int i,j;
clrscr();
printf("Enter the string :");
gets(str);
printf("%s in ascending order is -> ",str);
for(i=0;str[i];i++)
{
for(j=i+1;str[j];j++)
{
if(str[j]<str[i])
{
temp=str[j];
str[j]=str[i];
str[i]=temp;
}
}
}
printf("%s\n",str);
getch();
}
Output:
Enter the string :string
string in ascending order is -> ginrst

Write a program that will read a text and count all occurrences of a particular word

#include<stdio.h>
#include<conio.h>
main()
{
char str[100],find[100];
int i,j,cnt=0,flag;
clrscr();
printf("Enter the string :");
scanf("%s",str);
printf("Enter the srting that you want to find : ");
scanf("%s",find);
for(i=0;str[i];i++)
{
flag=0;
if(str[i]==find[0])
{
flag=1;
for(j=1;find[j];j++)
{
if(str[i+j]!=find[j])
{
flag=0;
break;
}
}
if(flag==1)
cnt++;
}
else
{
}
}
printf("%s occurs in %s %d times\n",find,str,cnt);
getch();
}
Enter the string :The cat & The dog
Enter the srting that you want to find : The
The occurs in The cat & The dog 2 times

Give the 1-D array A and B, which are sorted in ascending order. Write a program to merge them into a single sorted array C that contains every item from arrays A and B, in ascending order

#include<stdio.h>
#include<conio.h>
int main()
{
int a[10],b[10],c[20],n1,n2,i,j,temp,k=0;
clrscr();
printf(" Enter the no. of element for 1st array : ");
scanf("%d",&n1);
for(i=0;i<n1;i++,k++)
{
printf(" Enter element [%d] : ",i+1);
scanf("%d",&a[i]);
c[k]=a[i];
}
for(i=0;i<n1;i++)
{
for(j=i+1;j<n1;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n After sorting 1st array : ");
for(i=0;i<n1;i++)
{
printf("\n Element [%d] = %d",i+1,a[i]);
}
printf("\n\n Enter the no. of element for 2nd array : ");
scanf("%d",&n2);
for(i=0;i<n2;i++,k++)
{
printf(" Enter element [%d] : ",i+1);
scanf("%d",&b[i]);
c[k]=b[i];
}
for(i=0;i<n2;i++)
{
for(j=i+1;j<n2;j++)
{
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
printf("\n After sorting 2nd array : ");
for(i=0;i<n2;i++)
{
printf("\n Element [%d] = %d",i+1,b[i]);
}
for(i=0;i<n1+n2;i++)
{
for(j=i+1;j<n1+n2;j++)
{
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
printf("\n\n\n After combined and sorted both array :- ");
for(i=0;i<n1+n2;i++)
{
printf("\n Element [%d] = %d",i+1,c[i]);
}
getch();
}
Output:
Enter the no. of element for 1st array : 5
Enter element [1] : 20
Enter element [2] : 18
Enter element [3] : 6
Enter element [4] : 12
Enter element [5] : 4
After sorting 1st array :
Element [1] = 4
Element [2] = 6
Element [3] = 12
Element [4] = 18
Element [5] = 20
Enter the no. of element for 2nd array : 3
Enter element [1] : 6
Enter element [2] : 2
Enter element [3] : 3
After sorting 2nd array :
Element [1] = 2
Element [2] = 3
Element [3] = 6
After combined and sorted both array :-
Element [1] = 2
Element [2] = 3
Element [3] = 4
Element [4] = 6
Element [5] = 6
Element [6] = 12
Element [7] = 18
Element [8] = 20

Wednesday 9 March, 2011

Write a program to sort given array in ascending order


#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,j,n,temp;
clrscr();
printf("\n Enter no. of elements for 1-D array : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" Enter element[%d] : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n\n Ascending order of inserted array is : ");
for(i=0;i<n;i++)
{
printf("\n %d ",a[i]);
}
getch();
return 0;
}

Output:
Enter no. of elements for 1-D array : 5
Enter element [1] : 2
Enter element [2] : 10
Enter element [3] : 4
Enter element [4] : 13
Enter element [5] : 7
Ascending order of inserted array is:
2
4
7
10

Tuesday 8 March, 2011

Write a program to find maximum element from 1-Dimensional array

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],n,i,max=0; // declared the array a with size 20
clrscr();
printf("\n Enter the number of elements for 1-D array : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter element [%d] : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
}
printf("\n Maximum element from above array inserted is : %d",max);
getch();
return 0;
}
Output:
Enter the number of elements for 1-D array : 5
Enter element [1] : 1
Enter element [2] : 8
Enter element [3] : 2
Enter element [4] : 5
Enter element [5] : 9
Maximum element from above array inserted is: 9

Write a program that prints the following Floyd’s triangle

1
2 3
4 5 6
7 8 9 10
11 ………..15
.
.
79 …………………91

#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n,k;
clrscr();
printf("\n\n Enter the number : ");
scanf("%d",&n);
for(i=1,k=1;i<=n;i++)
{
for(j=1;j<=i;j++,k++)
{
printf(" %d ",k);
}
printf("\n");
}
getch();
return 0;
}
Output:
Enter the number : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Print the following triangle

Print the following triangle.
a b c d e
  a b c d
    a b c
      a b
        a

#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,j,k;
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
for(k=1;k<=i;k++)
{
printf(" ");
}
for(j=1;j<=n-i;j++)
{
printf(" %c",96+j);
}
printf("\n");
}
getch();
return 0;
}
Output:
Enter the number : 5
a b c d e
  a b c d
   a b c
    a b
     a

Write a program to find the roots of an equation ax2 + bx + c = 0

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float a,b,c,alf,bt,dlt;
clrscr();
printf("\n Enter a: ");
scanf("%f",&a);
printf("\n Enter b: ");
scanf("%f",&b);
printf("\n Enter c: ");
scanf("%f",&c);
dlt=b*b-4*a*c;
if(dlt==0)
{
printf("\n ALPHA=BETA=%f",-b/(2*a));
}
else if(dlt<0)
{
printf("\n Imaginary Roots");
}
else
{
alf=(-b+sqrt(dlt))/(2*a);
bt=(-b-sqrt(dlt))/(2*a);
printf("\n\n Alpha = %f\n Beta=%f\n",alf,bt);
}
getch();
return 0;
}
Output:
(1)
Enter a: 12
Enter b: 2
Enter c: 34
Imaginary Roots
(2)
Enter a: 2
Enter b: 6
Enter c: 2
Alpha = -0.381966
Beta = -2.618034
(3)
Enter a: 2
Enter b: 4
Enter c: 2
ALPHA=BETA= -1.000000

Write a program to generate Fibonacci series

#include<stdio.h>
#include<conio.h>
int main()
{
int n1=0,n2=1,n3=1,n,i;
clrscr();
printf("Enter the Number : ");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
printf(" %d ",n3);
n3=n1+n2;
n1=n2;
n2=n3;
}
getch();
return 0;
}
Output:
Enter the Number : 5
1 1 2 3 5

Print series 2, 4, 16,……,n*n using shorthand operator and while loop

#include<stdio.h>
#include<conio.h>
int main()
{
int n,x; // declare the variable
long double i=2; //declare the variable of long double type
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
printf("\n");
x=1;
while(x<=n) // loop will be execute till the value of x I less or equal n
{
printf("%.2Lf\n",i); // print the value of I upto 2 decimals only
x++;
i*=i;
}
getch();
return 0;
}
Output:
Enter the number : 4
2.00
4.00
16.00
256.00

print 2,4,6,8,...,n

Print series 2, 4, 6, 8,…....,n.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n; // declare two integer type variables
clrscr(); // clears the output window
printf("\n Enter the number : ");
scanf("%d",&n); // input the value of n
printf(“\n”); // will break the line on output window
for(i=2;i<=n;i++)
{
if(i%2==0)
{
printf(" %d ",i);
}
}
getch();
return 0;
}
Output:
Enter the number : 14
2 4 6 8 10 12 14

prime or not

Write program to check whether the entered number is PRIME or not.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,flag=0; // declare variable i, n & flag and initialize flag with 0
clrscr();
printf("\n Enter the number : ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("\n %d is a Prime number",n);
}
else
{
printf("\n %d ia not a Prime number",n);
}
getch();
return 0;
}
Output:
Enter the number : 4
4 is not a Prime number
Enter Number : 7
7 is a Prime number

Write program to accept number of days and print year, month and remaining days

#include<stdio.h>
#include<conio.h>
int main()
{
int day,y,m; /*day for day input & output,
y for calculate year,
m for calculate month
printf("Enter the number of days : ");
scanf("%d",&day);
y=day/365; //calculate years
day%=365; // calculate remaining days
m=day/30; // calculate months
day%=30; // calculate remaining days
printf("%d years,%d months, %d days",y,m,day);
getch();
return 0;
}
Output:
Enter number of days : 1025
2 years,9 months, 25 days

Beginning with C:1st example

Write a program to convert Rupees (float) to paisa (int).
#include<stdio.h>
#include<conio.h>
int main()
{
float rs; //real variable for rupees
int ps; // integer variable for paisa
clrscr();
printf("\n\n Enter rupees to convert into paisa : ");
scanf("%f",&rs);
ps=rs*100;
printf("\n Paisa of given rupees is %d",ps);
getch();
return 0;
}
Output:
Enter rupees to convert into paisa : 98.52
Paisa of given rupees is 9852