Home >> Sem 1 >> 610002 - Programming Skills-I (FOP)
Last Update : 14 / Oct / 2010
31. Write a program to add first n numbers.
#include<stdio.h>
#include<conio.h>
void sum(int);
int main()
{
int n;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
sum(n);
getch();
return 0;
}
void sum(int n)
{
int i,s=0;
for(i=1;i<=n;i++)
{
s+=i;
}
printf("Sum of first %d integer is %d\n",n,s);
}
Output:
Enter a number : 10
Sum of first 10 integer is 55
32. 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
33. 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 : Diz
Diz in upper case is DIZ
34. Write a function to reverse 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]);
}
35. 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;
}
36. Define a structure called cricket that will describe the following information:
#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 : Diz
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 Diz 100
Pakistan Tiwari 5
India Tendulkar 45
India Dhoni 48
India Yuvi 39
37. In a program declare following structure member:
#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 : Diz
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
--------------------------------
Diz D 21 144 65
38. 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;
}
39. Write a function to calculate the roots of the quadratic equation..
#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
40. 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 :
3 3 3
3 3 3
3 3 3
Enter matrix B :
5 5 5
5 5 5
5 5 5
Addition :
8 8 8
8 8 8
8 8 8
*/