Sei sulla pagina 1di 38

17CS211 –Problem Solving using

C Programming 2. Write a program to find whether a


(COMMON TO MECH, CIVIL) given number is divisible by both 7
and 3.

Input Format:
1. Write a C program that prints “yes” if Input consists of a single integer.
the given integer is divisible by 2 or
3 and “no” otherwise. Sample Input 1:
21
Input Format: Sample Output 1 :
Input consists of a single integer. 21 is divisible by both 7 and 3

Output Format: Sample Input 2:


Output consists of a single line. Refer 18
sample output for the format. Sample Output 2:
18 is not divisible by both 7 and 3
Sample Input 1 :
Enter an integer: #include<stdio.h>
21 int main()
Sample Output 1 :
{
yes
int a;
Sample Input 2: scanf("%d",&a);
Enter an integer: if((a%7==0)&&(a%3==0))
19 {
Sample Output 2: printf("%d is divisible by both 7 and
no 3",a);
}
#include<stdio.h>
else
int main()
{
{
printf("%d is not divisible by both 7 and
int a;
3",a);
printf("Enter an integer:");
}
scanf("%d",&a);
}
if((a%2==0)||(a%3==0))
{
3. Phineas and Ferb were worried
printf("\nyes",a); because they have shifted to a new
} house. As usual there was confusion
else in the house as the AC in their room
{ was not working. They had to stay at
printf("\nno",a); some other room tonight. Out of the
} two rooms, one belongs to their
parents' and one belongs to their
}
sister. Ferb feels cold so he prefers a
warm room to sleep. Given the room
temperature of the rooms , help 4. Sujith is going to Goa this week end.
them to select their room. As the train tickets are unavailable,
he decides to go by bus. Hebooks his
SAMPLE INPUT 1: tickets through a website. Write a
First Room Temperature : program to find whether he booked
23 a window seat or not.Assume that
Second Room Temperature : there are maximum 11 rows in a bus.
18
Input and Output Format:
SAMPLE OUTPUT 1: Input consists of two integers. The first
Phineas must stay in second room and integer corresponds to the number of
Ferb must stay in first room seats per row in the bus.
The second integer corresponds to the
SAMPLE INPUT 2: seat number.
First Room Temperature :
21 Sample Input and Output 1:
Second Room Temperature : Enter number of seats per row
25 4
Enter the seat number
SAMPLE OUTPUT 2: 36
Phineas must stay in first room and Ferb Window Seat
must stay in second room
#include<stdio.h> Sample Input and Output 2:
int main() Enter number of seats per row
{ 3
int t1,t2; Enter the seat number
34
printf("First room temperature:");
Invalid Seat Number
scanf("%d",&t1);
printf("Second room temperature:"); Sample Input and Output 3:
scanf("%d",&t2); Enter number of seats per row
if(t1>t2) 3
{ Enter the seat number
printf("Phineas must stay in second 20
room and Ferb must stay in first Aisle Seat
#include<stdio.h>
room",t1);
int main()
}
{
else intn,sno,d;
{ printf("Enter number of seats per
printf("Phineas must stay in first room row\n");
and Ferb must stay in second room",t2); scanf("%d",&n);
} printf("Enter the seat number\n");
} scanf("%d",&sno);
d=n*11;
if(d>sno) Aim High
{
if((sno%n==0)||(sno%n==1)) Sample Input and Output 3:
{ Enter the height of the shot
printf("Window Seat\n"); 120
} Aim Low
else
{ #include<stdio.h>
printf("Aisle Seat\n"); int main()
} {
}
int h;
else
printf("Enter the height of the shot\n");
printf("Invalid seat number");
} scanf("%d",&h);
if((h>=50)&&(h<=90))
5. King Indravarma ordered his {
servants to distribute gifts for the printf("Hurrah !!!");
people. They arranged for the gifts }
and kept them in a room. Dolu and else if(h>90)
Bolu is excited about gifts. Kalia is up
{
to his mischievous self again. He tied
printf("Aim Low");
balloons to all the gifts that the king
had kept for distributing to the
people. The balloons are tied in }
height of 50m to 90m above the else if(h<50)
ground. He challenged Bheem to {
shoot down the balloons and collect printf("Aim High");
all the gifts by blind folding his eyes. }
Write a program to help Bheem to
}
shoot down the balloons.

Input and Output Formats 6. In Kolkata, every year during dasara


Input contains a single integer h. The there will be a grand doll show.
height at which Bheem shoot. People try to create new dolls of
Output should display “Hurrah !!!” if he different varieties. The best sold
shots correctly. Otherwise display “Aim doll's creator will be awarded with a
High” or Aim Low”. cash prize. So people break their
head to create dolls innovatively.
Sample Input and Output 1: Knowing this competition, Mr.Raju
Enter the height of the shot tried to create a doll which sings
67 only when an even number is
Hurrah !!! pressed and the number should not
be zero and greater than 100. So
Sample Input and Output 2: write a C program to help Mr.Raju to
Enter the height of the shot win.
47
Input Format: number to quantify the energy
Input Consists of Single Integer which contained in an earthquake.
Corresponds to Number pressed by the
user to the doll. Magnitude Type
2.4 or less Micro
Output Format: 2.5 to 5.4 Light
Display whether the doll will Sing or 5.5 to 6.0 Moderate
not. Output consists of the string "Doll 6.1 to 6.9 Strong
will sing" or "Invalid number". 7.0 to 7.9 Major
8.0 or greater Great
Sample Input and Output: Please help Ram to decide the intensity
Press a number: of the earthquake.
56
Doll will sing Input Format:
Input consists of a single floating point
#include<stdio.h> number which corresponds to the
int main() earthquake's Richter magnitude scale.
{
int a; Output Format:
Output consists of the string “Micro” or
printf("Press a number:\n");
“Light” or “Moderate” or “Strong” or
scanf("%d",&a); “Major” or Great”
if((a>0)&&(a<=100)&&(a%2==0)) Refer sample input and output for
printf("Doll will sing\n"); further formatting specifications.
else
printf("Invalid number\n"); Sample Input and Output:
} Richter Magnitude:
5.5
Moderate
7. Mr. Vetri is a very rich businessman
and he lost his family in the Gujarat
Earthquake. He lost interest in his #include<stdio.h>
business after the tragic incident and int main()
he decided to serve the society. He {
started an NGO named after his only float a;
son Ishan to help the earth-quake printf("Richter Magnitude:\n");
victims. Ishan Helpline will provide scanf("%f",&a);
compensation to all earth quake if(a<=2.4f)
survivors. The compensation
printf("Micro\n");
amount given to the survivors is not
fixed and it depends on the intensity else if((a>=2.5f)&&(a<=5.4f))
of the earthquake. printf("Light\n");
else if((a>=5.5f)&&(a<=6.0f))
The expression Richter printf("Moderate\n");
magnitude scale refers to a else if((a>=6.1f)&&(a<=6.9f))
number of ways to assign a single printf("Strong\n");
else if((a>=7.0f)&&(a<=7.9f))
printf("Major\n");
else 9. A teacher tries to relate between the
Angles inscribed in a circle and the
printf("Great\n");
direction. Initially she wants to teach
}
them the directions related with the
angle’s 0,90,180,270 indicating East,
8. Write a C program to calculate the North, West, South respectively.
square of only the numbers whose Write a C program using selection
least significant digit is 5. statement to denote the direction
graphically corresponding to the
Input format : angle entered.
Input consists of an integer.
Output format : Input Format:
Output consists of the square of a Input consists of a single integer
number or a message if the number's corresponding to the angle entered by
least significant is not 5. the student

Sample Input and Output 1 : Output Format:


Enter the number: Output consists of the strings “North”,
125 “South”, “East”, “West” and “Invalid”.
The square of 125 is 15625
Sample input and output:
Sample Input and Output 2 : Enter the angle:
Enter the number: 270
521 270 corresponds to South
Least significant of 521 is not 5
#include<stdio.h> Sample input and output:
int main() Enter the angle:
{ 375
inta,sq; Invalid
printf("Enter the number:\n");
scanf("%d",&a); #include<stdio.h>
if(a%5==0) void main()
{ {
sq=a*a; intang;
printf("The square of %d is %d",a,sq); printf("Enter the angle:\n");
} scanf("%d",&ang);
else if(ang==0)
printf("Least significant of %d is not printf("%d corresponds to East",ang);
5",a); else if(ang==90)
} printf("%d corresponds to North",ang);
else if(ang==180)
printf("%d corresponds to West",ang);
else if(ang==270)
printf("%d corresponds to South",ang); break;
else case 8: printf("eight");
printf("Invalid"); break;
} case 9: printf("nine");
Digit to Word break;
default:
10. Write a C program using switch case printf("please enter the number
to accept any single digit number between 0 and 9");
and print it in words. }
}
Input format:
Input consists of a single digit.
11. Rangan is very weak in converting
Output format: the numbers into months. But his
Output consists of the string. friends often tease him by asking
him to convert a number into a
Sample Input and Output: month. To solve his problem a close
Enter the digit: 0 friend of him, suggested him to meet
zero SIET students, who were very good
at programming. Can you please help
#include<stdio.h> him?
int main( )
Input Format:
{ Input consists of an integer.
int n;
printf("Enter the digit:\n"); Output Format:
scanf("%d",&n); Output consists of the corresponding
switch(n) month and “There is no month for this
{ number” if the input is not in range
case 0: printf("zero"); between 1 and 12.
break;
Sample Input and Output:
case 1: printf("one"); Month number:
break; 2
case 2: printf("two"); The month name is February
break;
case 3: printf("three"); Sample Input and Output:
break; Month number:
13
case 4: printf("four");
There is no month for this number
break;
case 5: printf("five"); #include<stdio.h>
break; void main()
case 6: printf("six"); {
break; int n;
case 7: printf("seven");
printf("Month number:\n"); printf("The month name is
scanf("%d",&n); December\n");
switch(n) break;
{ default:
case 1: printf("There is no month for this
printf("The month name is January\n"); number\n");
break; break;
case 2: }
printf("The month name is }
February\n");
break;
12. THE CWG 2010 committee is bored
case 3:
with the usual long jump, high jump
printf("The month name is March\n"); and triple jump and they
break; havedecided to introduce a new
case 4: game called staircase climbing. The
printf("The month name is April\n"); players need to climb a staircase.
break; There aren steps and each step is 1
case 5: meter higher than the previous one.
The player who first reaches the top
printf("The month name is May\n");
of thestaircase will be declared as
break; the winner.The audio-visual
case 6: committee is responsible for placing
printf("The month name is June\n"); pans and tilt cameras at all locations
break; and one of theirmain objective is to
case 7: capture the winning moment in all
printf("The month name is July\n"); the games. They were pondering on
break; where to place the camera for
capturing the winning moment of
case 8:
the staircase climbing game. Finally,
printf("The month name is they decided onplacing the camera
August\n");break; at the same height as the top of the
case 9: staircase.Given the number of steps
printf("The month name is 'n' and the height of the first step 'h',
September\n"); write a C program to find the
break; positionwhere the camera needs to
case 10: be placed.
printf("The month name is October\n");
Input Format:
break; The input consists of 2 integers which
case 11: correspond to n and h.
printf("The month name is
November\n"); Output Format:
break; Output consists of a single integer.
case 12: Sample Input 1:
5 astrological requirement. Though
1 the real estate agentis a maths
Sample Output 1: graduate, he finds it difficult to give
15 an exact answer to Lucy's question.
#include<stdio.h> He approaches you andseeks your
int main() help.Can you help him out?Write a C
{ program to find the number of lucky
intsum,n,h,i; numbers in the interval [a,b] (both
inclusive].
scanf("%d",&n);
scanf("%d",&h);
Input Format:
for(i=h;i<n+h;i++) Input consists of 2 integers which
{ correspond to a and b. Assume that a<b.
sum=sum+i; a and b are 3-digit integers.
}
printf("%d",sum); Output Format:
} Output consists of the count of lucky
numbers. Refer sample output for
formatting details.
13. Lucy believes a lot in astrology. A
famous astrologer tells her that 3- Sample Input 1:
digit numbers which has the 100
followingproperty are lucky 130
numbers to her.Sum of the first digit
and the third digit is equal to the Sample Output 1:
second digit . For example, 143 is a 2
lucky numberfor her as 1+3 = 4.Lucy Hint[110 & 121 are the only lucky
is planning to buy an apartment in numbers in this interval].
the new Marutham Apartments that
is coming up in Coimbatoreand she
#include<stdio.h>
heard that all blocks are sold out
int main()
except block D. She was very
particular that she is going to buyan {
apartment in which the door inti,j,k,l,m,count=0,a,b;
number is her lucky number. So she scanf("%d%d",&a,&b);
has to visit only the flats which has for(i=a;i<b;i++)
thedoor numbers that satisfy her {
lucky number property.She tells her if(i>99&&i<1000)
requirement to the real estate agent.
{
He tells her that all flats in block D
j=i%10;
have 3-digit doornumbers. He also
tells her that the flats in Block D are m=i/100;
numbered sequentially and the k=i/10;
starting number is aand the ending l=k%10;
number is b.She asks him how many if(j+m==l)
flats in Block D satisfy her count++;
} and print the numbers which are
} divisibleby 2 and not divisible by 3
printf("%d",count); and 5.
}
Input Format :
Input consists of range values a and b
14. Write a C program to print the series (integers) . Always a<b.
1,4,7,10.... upto n terms.
Output Format :
Input Format: Output consists of integers between the
Input consists of a single integer 'n'. range.
Refer Sample Input and Output for
Output Format: further details.
Output consists of a single line. Refer to
sample output for details. Sample Input and Output 1 :
[ All text of bold corresponds to Input
Sample Input 1: and the rest output]
2
Enter the Range :
Sample Output 1: 1
14 40
2 4 8 14 16 22 26 28 32 34 38
Sample Input 2:
5 Sample Input and Output 2 :
Sample Output 2: Enter the Range :
20
1 4 7 10 13 80
22 26 28 32 34 38 44 46 52 56 58 62 64
#include<stdio.h> 68 74 76
int main()
{ #include<stdio.h>
inta,i,n; int main()
scanf("%d",&n); {
for(i=0;i<n;i++) inta,b,i;
{ printf("Enter the Range\n");
if(i==0) scanf("%d%d",&a,&b);
a=1; for(i=a;i<=b;i++)
else {
a=a+3; if((i%2==0)&&(i%3!=0)&&(i%5!=0))
printf("%d\n",a); printf("%d\n",i);
} }
} }
15. Write a C program to generate
numbers between the given range
16. Write a ‘C’ Program using for loop to number, except one and the number
find the largest digit of a number. itself.

Input format : Input format :


Input consists of an integer value. Input consists of an integer.

Output Format : Output format :


Output consists of largest digit in the Output consists of proper factors of the
input value. input.
[ Refer Sample Input and Output for [ Refer Sample Input and Output for
further details ] further specifications ]

Sample Input and Output 1 : Sample Input and Output :


[ All text of bold corresponds to Input [ All text of bold corresponds to
and the rest output ] input ]
Enter the number : Enter the number :
1000 12
Largest digit in 1000 is 1 Proper factors of 12 are
2346
Sample Input and Output 2 : #include<stdio.h>
Enter the number : void main( )
224 {
Largest digit in 224 is 4 intno,x;
scanf("%d",&no);
#include <stdio.h>
for(x=2; x<no; x++)
int main()
{ {
intnum, large = 0, rem = 0; if(no%x==0)
printf("Enter your input value:"); printf("\n%d",x);
scanf("%d", &num); }
while (num > 0) { }
rem = num % 10; 1. Program to check whether a given
if (rem > large) { integer is odd or even
large = rem;
} #include <stdio.h>
num = num / 10; void main()
} {
printf("Largest digit of the number is int a, remainder;
%d\n", large);
return 0; printf("Enter an integer : ");
} scanf("%d", &a);
remainder = a % 2;
17. Write a C program using for loop to if (remainder == 0)
print all the proper factors of an printf("%d is an even integer\n", a);
else
integer.Any of the factors of a
printf("%d is an odd integer\n", a);
}
OUTPUT: if(num<0)
{ /* checking whether number
$ cc pgm4.c is less than 0 or not. */
$ a.out printf("Number = %d is
Enter an integer : 100 negative \n",num);
100 is an even integer }
else
$ a.out {
Enter an integer : 105 printf("Number = %d is
105 is an odd integer positive \n",num);
}
2. Program to find leap year or not /*If test condition is true,
#include <stdio.h> statement above will be executed,
int main() otherwise it will not be
{ executed */
int year; return 0;
printf("Enter a year to check if it is a }
leap year\n"); Output:
scanf("%d", &year); Enter a number to check.
if ( year%400 == 0) -2
printf("%d is a leap year.\n", Number = -2 is negative
year); Enter a number to check.
else if ( year%100 == 0) 5
printf("%d is not a leap year.\n", Number = 5 is positive
year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", Program To Check Whether A Person Is
year); Eligible To Vote
else #include<stdio.h>
printf("%d is not a leap year.\n", #include<conio.h>
year); void main()
return 0; {
} int age;
Output: char name[50]; // or *name
Enter a year clrscr();
2012
2012 is a leap year printf("\n Type the Name of the
candidate: ");
gets(name);
3. Program to check the entered
no is positive or negative. printf("\n Enter The Age : ");
scanf("%d",&age);
#include <stdio.h>
int main() if(age>=18)
{ {
int num; printf("\n %s is Eligible for
printf("Enter a number to Vote",name);
check.\n"); }
scanf("%d",&num); else
{ 2
printf("\n %s is Not Eligible for 2 is even.
Vote",name);
}
4. Program to find grades of a student
getch(); using else if ladder
} #include<stdio.h>
#include<conio.h>
Output: void main()
{
int s1,s2,s3,s4,s5,t,p;
clrscr();
printf("\n Enter marks of 5 subjects
each out of 100 ");
printf("\n\n
**************************************
******");
printf("\n\n Maths = ");
scanf("%d",&s1);
printf("\n Science = ");
scanf("%d",&s2);
printf("\n English = ");
scanf("%d",&s3);
printf("\n History = ");
scanf("%d",&s4);
printf("\n Geography = ");
Program to Check if a given Integer is scanf("%d",&s5);
Odd or Even printf("\n
#include <stdio.h> **************************************
int main() ******");
{ t=s1+s2+s3+s4+s5; //Total
int num; printf("\n Total marks = %d/500",t);
printf("Enter a number you p=t/5; //Percentage
want to check.\n"); printf("\n\n Percentage = %d%",p);
scanf("%d",&num); printf("\n
if((num%2)==0) **************************************
//checking whether remainder is ******");
0 or not. //////////// Ladder If
printf("%d is even.",num); Statement ////////////
else if(p>=80)
printf("%d is odd.",num); printf("\n\n Your Grade : A+");
return 0; else if(p>=75)
} printf("\n\n Your Grade : A");
Output 1 else if(p>=60)
Enter a number you want to check. printf("\n\n Your Grade : B");
25 else if(p>=45)
25 is odd. printf("\n\n Your Grade : C");
Output 2 else if(p>=35)
Enter a number you want to check. printf("\n\n Your grade : D");
else {
printf("\n\n You Are Fail"); char ch;
//////// Ladder If printf("Enter a character\n");
Statement ///////////// scanf("%c", &ch);
getch(); if (ch == 'a' || ch == 'A' || ch == 'e' || ch
} == 'E' || ch == 'i' || ch == 'I' || ch =='o' ||
Output :
ch=='O' || ch == 'u' || ch == 'U')
printf("%c is a vowel.\n", ch);
else
printf("%c is not a vowel.\n", ch);
return 0;
}
OUTPUT
Enter a charcter a
a is a vowel

9. Program to check whether a


number entered by user is
Armstrong or not.

#include <stdio.h>
int main()
{
5. Program to find greatest of three int n, n1, rem, num=0;
numbers. printf("Enter a positive integer: ");
#include<stdio.h> scanf("%d", &n);
int main() { n1=n;
int a, b, c; while(n1!=0)
printf("\nEnter value of a, b & c : "); {
scanf("%d %d %d", &a, &b, &c); rem=n1%10;
if ((a > b) && (a > c)) num+=rem*rem*rem;
printf("\na is greatest"); n1/=10;
if ((b > c) && (b > a)) }
printf("\nb is greatest"); if(num==n)
if ((c > a) && (c > b)) printf("%d is an Armstrong
printf("\nc is greatest"); number.",n);
return(0); else
} printf("%d is not an Armstrong
Output : number.",n);
Enter value for a,b & c : 15 17 21 }
c is greatest
Output:
6. Program to find whether a Enter a positive integer: 153
character is vowel or consonant 153 is an Armstrong number.
using if condition
Enter a positive integer: 371
#include <stdio.h> 371 is not an Armstrong number.
int main()
10. Program to Reverse an int main()
Integer {
#include<stdio.h> int k,r;
int main() int f1=0,f2=1,f3,n;
{ clrscr();
int n, reverse=0, rem; printf("Enter the number range:");
printf("Enter an integer: "); scanf("%d",&n);
scanf("%d", &n); printf("FIBONACCI SERIES: ");
while(n!=0) printf("\t%d\t%d",f1,f2);
{ //printing firts two values.
rem=n%10; for(k=1;k<=n;k++)
reverse=reverse*10+rem; {
n/=10; f3=f1+f2;
} printf("\t%d",f3);
printf("Reversed Number = f1=f2;
%d",reverse); f2=f3;
return 0; }
} getch();
return 0;
Output }
Enter an integer: 2345
Reversed Number = 5432 Enter the number range:10
FIBONACCI SERIES: 0 1 1 2 3 5 8
11. Program to Print the sum 13 21 34 55..
of digit
#include <stdio.h> 13. Program to print Fibonacci
int main() series using while loop:
{ #include <stdio.h>
int n, sum=0, rem; #include <conio.h>
printf("Enter an integer: "); int main()
scanf("%d", &n); {
while(n!=0) int r;
{ int f1=0,f2=1,f3=0,n;
rem=n%10; clrscr();
sum=sum+rem; printf("Enter the number range:");
n/=10; scanf("%d",&n);
} printf("FIBONACCI SERIES: ");
printf("Sum of a Number = printf("\t%d\t%d",f1,f2);
%d",sum); //printing firts two values.
return 0; While(f3<=100)
} {
Output f3=f1+f2;
Enter an integer: 2345 printf("\t%d",f3);
Sum of a Number = 14. f1=f2;
f2=f3;
12. Program to print Fibonacci }
series using for loop: getch();
#include <stdio.h> return 0;
#include <conio.h> }
5
Enter the number range:10 *
FIBONACCI SERIES: 0 1 1 2 3 5 8 **
13 21 34 55.. ***
****
*****
14. Program to find the Sum of
n numbers using for loop 16. Program to display the
pattern2
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int n,i, sum=0; int i,space,rows,k=0;
printf("Enter an integer: "); printf("Enter the number of rows:
scanf("%d",&n); ");
for(i=1;i<=n;++i) /* for loop scanf("%d",&rows);
terminates if count>n */ for(i=1;i<=rows;++i)
{ {
sum+=i; /* for(space=1;space<=rows-i;+
sum=sum+count */ +space)
} {
printf("Sum = %d",sum); printf(" ");
return 0; }
} while(k!=2*i-1)
Output: {
Enter an integer: 100 printf("* ");
Sum = 5050 ++k;
}
15. Program to display the k=0;
pattern1 printf("\n");
#include <stdio.h> }
int main() return 0;
{ }
int i,j,rows;
printf("Enter the number of rows: OUTPUT:
");
scanf("%d\n",&rows); Enter the number of rows:
for(i=1;i<=rows;++i) 5
{ *
for(j=1;j<=i;++j) ***
{ *****
printf("* "); *******
} *********
printf("\n");
} 17. Program to display the
return 0; pattern3
} #include <stdio.h>
Output: int main()
Enter the number of rows: {
int i,j,rows; r=n%10;
printf("Enter the number of rows: while(i<=r)
"); {
scanf("%d",&rows); a=a*i;
for(i=1;i<=rows;++i) i++;
{ }
for(j=1;j<=i;++j) sum=sum+a;
{ n=n/10;
printf("%d ",j); }
} if(sum==temp)
printf("\n"); printf("%ld is a strong
} number\n",temp);
return 0; else
} printf("%ld is not a strong
number\n",temp);
OUTPUT: getch();
1 }
12 output:
123 Enter the number:145
1234 145 is a strong number.
12345
19. Program for Floyd’s
triangle.
18. Program to check entered #include<stdio.h>
number is strong or not. int main(){
This program checks whether the int i,j,r,k=1;
entered number is a strong number or printf("Enter the range: ");
not and prints the result on the compiler scanf("%d",&r);
screen. printf("FLOYD'S TRIANGLE\n\n");
A number is called strong number if for(i=1;i<=r;i++){
sum of the factorial of its digit is equal to for(j=1;j<=i;j++,k++)
number itself. For example: 145 as 1! + printf(" %d",k);
4! + 5! = 1 + 24 + 120 = 145. printf("\n");
The number is checked using while loop }
and result is printed on compiler screen. return 0;
}
#include<stdio.h>
#include<conio.h> Output:
void main() Enter the range: 10
{ FLOYD'S TRIANGLE
int i,r;
long n,temp,sum=0,a; 1
clrscr(); 23
printf("Enter the number:\n"); 456
scanf("%ld",&n); 7 8 9 10
temp=n; 11 12 13 14 15
while(n!=0) 16 17 18 19 20 21
{ 22 23 24 25 26 27 28
i=1,a=1; 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45 int main()
46 47 48 49 50 51 52 53 54 55 {
int n1, n2, i, temp, num, rem;
20. Program to display all printf("Enter two
prime numbers between numbers(intervals): ");
Two interval entered by scanf("%d %d", &n1, &n2);
user. printf("Armstrong numbers between
#include <stdio.h> %d an %d are: ", n1, n2);
int main() for(i=n1+1; i<n2; ++i)
{ {
int n1, n2, i, j, flag; temp=i;
printf("Enter two numbers(intevals): num=0;
"); while(temp!=0)
scanf("%d %d", &n1, &n2); {
printf("Prime numbers between %d rem=(temp%10);
and %d are: ", n1, n2); num+=rem*rem*rem;
for(i=n1+1; i<n2; ++i) temp/=10;
{ }
flag=0; if(i==num)
for(j=2; j<=i/2; ++j) {
{ printf("%d ",i);
if(i%j==0) }
{ }
flag=1; return 0;
break; }
} Output:
} Enter two numbers(intervals): 100
if(flag==0) 400
printf("%d ",i); Armstrong numbers between 100 and
} 400 are: 153
return 0; 22. Program to calculate
} power of number (ab).
#include<stdio.h>
Output: int main()
cc prime.c {
user@administrator-ThinkCentre- int a,b,i,pow;
M72e:~$ ./a.out printf("Enter value of a: ");
Enter two numbers(intevals): 23 67 scanf("%d",&a);
Prime numbers between 23 and 67 are: printf("Enter value of b: ");
29 31 37 41 43 47 53 59 61 scanf("%d",&b);
pow=1;
21. Program to Display for(i=1;i<=b;i++)
Armstrong Number {
Between Two Intervals pow=pow*a;
/* Source Code to display Armstrong }
number between two intervals entered printf("a(power)b = %d",pow);
by user. */ }
#include <stdio.h> OUTPUT:
Enter value of a: 5
Enter value of b: 2 Enter a string : welcome
a(power)b = 25 Enter the character to be searched : e
Occurence of character 'o' : 2

______________________________________________ 3. Program to calculate length of a


______________________________________________ string
______ #include<stdio.h>
String Programs void main( )
{
1. Calculate Length of String without char s[20];
Using strlen() Function int i;
#include <stdio.h> printf("Enter the string\n");
int main() scanf("%s", s);
{ i=strlen(s);
char s[100], i; printf(" No. of characters:%d", i);
printf("Enter a string: "); }
scanf("%s", s); OUTPUT:
for(i = 0; s[i] != '\0'; ++i); Enter the string
printf("Length of string: %d", i); Apple
return 0; No. of characters:5
}
Output Program to copy a string
Enter a string: Programiz #include<stdio.h>
Length of string: 9 void main( )
{
2. String program to count the char s1[20],s2[20];
occurrence of a character in a string. printf("Enter the string\n");
#include<stdio.h> scanf("%s",s1);
void main() { strcpy(s2,s1);
char str[20], ch; printf("Copied string: %s", s2);
int count = 0, i; }
printf("\nEnter a string : "); OUTPUT:
scanf("%s", &str); Enter the string
printf("\nEnter the character to be Lord
searched : "); Copied string : Lord
scanf("%c", &ch);
for (i = 0; str[i] != '\0'; i++) { Program to concatenate 2 strings
if (str[i] == ch) #include<stdio.h>
count++; void main( )
} {
if (count == 0) char s1[20],s2[20];
printf("\nCharacter '%c'is not present", printf("Enter the WORD1\n");
ch); scanf("%s",s1);
else printf("Enter the WORD2\n");
printf("\nOccurence of character '%c' : scanf("%s",s2);
%d", ch, count); strcat(s1,s2);
} printf("CONCATINATED STRING:%s", s1);
Output : }
OUTPUT: d++;
Enter the WORD1 else
GOOD c++;
Enter the WORD2 }
GIRL printf("Number of Space is:\n");
CONCATINATED STRING: GOODGIRL printf("%d",s);
printf("Number of vowel is:\n");
Program to compare two strings printf("%d",v);
#include<stdio.h> printf("Number of Consonant is:\n");
void main( ) printf("%d",c);
{ printf("Number of digit is:\n");
char s1[20],s2[20]; printf("%d",d);
printf("Enter the WORD1\n"); }
scanf("%s",s1);
printf("Enter the WORD2\n"); Write a C program to check whether
scanf("%s",s2); the string is palindrome or not.
if(strcmp(s1,s2)==0)
printf("Both strings are equal\n"); #include <stdio.h>
else if(strcmp(s1,s2)>0) #include <string.h>
printf("S1 is greater than S2\n"); void main ()
else {
printf("S2 is greater than S1\n"); char s[20];
} char p[20];
OUTPUT: int i,l,j;
Enter the WORD1 printf(“Enter String:”);
GOOD scanf("%s",s);
Enter the WORD2 for(i=0;s[i]!='\0';i++);
GIRL l=i;
S1 is greater than S2 for(i=l-1,j=0;i>=0;i--,j++)
p[j]=s[i];
String program to count vowels, p[j]='\0';
characters, consonants, spaces, digits. if(!strcmp(s,p))
printf("Palindrome");
#include<stdio.h> else
#include<string.h> printf("Not palindrome");
void main() }
{
char a[100]; Output:
int j=0,c=0,v=0,s=0,d=0; Enter String : amma
printf("enter the string:\n"); Palindrome.
gets(a);
for(j=0;a[j]!='\0';j++) Write a C program to convert
{ lowercase to uppercase and uppercase
if(isspace(a[j])) to lowercase character.
s++; #include<stdio.h>
else if(a[j]=='a'||a[j]=='e'||a[j]=='i'|| #include<string.h>
a[j]=='o'||a[j]=='u') void main()
v++; {
else if(isdigit(a[j])) char a[10];
int j=0; for(i=0;i<n;i++)
printf("enter the string"); for(j=i+1;j<n;j++)
scanf("%s",a); { if(strcmp(word[i],word[j])>0)
for(j=0;a[j]!='\0';j++) {
{ strcpy(temp,word[i]);
if(islower(a[j])) strcpy(word[i],word[j]);
printf("%c",toupper(a[j])); strcpy(word[j],temp);
else }
printf("%c",tolower(a[j])); }
} printf(“Sorted strings:”);
} for(i=0;i<n;i++)
Output: printf("%s\n",word[i]);
enter the string }
welcome
WELcome OUTPUT:
Enter the number of words
4. Write a C program to count the 5
number of words in a line. Enter the words
red
#include<stdio.h> blue
#include<string.h> white
void main() black
{ pink
char a[100]; Sorted strings:
int j=0,c=0; black
clrscr(); blue
printf("enter the string:\n"); pink
gets(a); red
for(j=0;a[j]!='\0';j++) white
{
if(isspace(a[j]))
c++; String Search:
} #include <stdio.h>
printf("Number of words is:\n"); #include <string.h>
printf("%d",c+1); #include <ctype.h>
} int main(void)
{
5. Program to sort strings char text[100];
#include<stdio.h> char substring[40];
#include<string.h> printf("\nEnter the string to be searche
void main( ) d(less than 100 characters):\n");
{ fgets(text, sizeof(text), stdin);
char word[50][20],temp[20]; printf("\nEnter the string sought (less t
int i,j,n; han 40 characters ):\n");
printf("Enter the number of words”); fgets(substring, sizeof(substring), stdin
scanf("%d",&n); );
printf(“Enter the words”);
for(i=0;i<n;i++) /* overwrite the newline character in e
scanf("%s",word[i]); ach string */
text[strlen(text)-1] = '\0'; int num[50],i,size,max;
substring[strlen(substring)-1] = '\0';
int i; printf("Enter size of an array\n");
for(i = 0 ; (text[i] = toupper(text[i])) ; i+ scanf("%d", &size);
+); printf("Enter Array elements\n");
for(i = 0 ; (substring[i] = toupper(subst for(i=0;i<size;i++)
ring[i])) ; i++); scanf("%d",&num[i]);
printf("\nThe second string %s found i max=num[0];
n the first.",((strstr(text, substring) == N for(i=0;i<size;i++)
ULL) ? "was not" : "was")); if(num[i]>max)
return 0; max=num[i];
} printf("Largest element;%d",max);
getch();
1. Program to find the average of n }
number OUTPUT:
#include<stdio.h> Enter size of an array
#include<conio.h> 5
void main() Enter Array elements
{ 45
int num[50],i,size,sum; 33
float avg; 22
printf("Enter size of an array\n"); 60
scanf("%d", &size); 57
printf("Enter Array elements\n"); Largest element:60
for(i=0;i<size;i++)
scanf("%d",&num[i]); 3. Program to sort element in an
for(i=0;i<size;i++) ascending order
sum=sum + num[i]; #include<stdio.h>
avg=(float) sum/size; #include<conio.h>
printf("average=%f",avg); void main()
getch(); {
} int num[50],i,j,size,temp;
OUTPUT: printf("Enter size of an array\n");
Enter size of an array scanf("%d", &size);
5 printf("Enter Array elements\n");
Enter Array elements for(i=0;i<size;i++)
20 scanf("%d",&num[i]);
21 for(i=0;i<size;i++)
19 {
18 for(j=i+1;j<size;j++)
20 {
Average=19.600 if(num[i]>num[j])
{ temp=num[i];
2. Program to find largest element in num[i]=num[j];
a array num[j]=temp;
#include<stdio.h> }
#include<conio.h> }
void main() }
{ for(i=0;i<size;i++)
printf("%d\n",num[i]); #include<conio.h>
getch(); void main()
} {
OUTPUT: int num[50],i,size,sum=0;
Enter size of an array printf("Enter size of an array\n");
5 scanf("%d", &size);
Enter Array elements printf("Enter Array elements\n");
33 for(i=0;i<size;i++)
22 scanf("%d",&num[i]);
55 for(i=0;i<size;i=i+2)
11 sum=sum + num[i];
44 printf("sum=%d",sum);
The sorted array: getch();
11 }
22
33
44
55 OUTPUT:
Enter size of an array
4. Program to print only even position 5
elements in an array Enter Array elements
#include<stdio.h> 12 345
#include<conio.h> Sum of even position=9
void main()
{
int num[50],i,size; 6. Program to count the no. of
printf("Enter size of an array\n"); students passed in a subject
scanf("%d", &size); #include<stdio.h>
printf("Enter Array elements\n"); #include<conio.h>
for(i=0;i<size;i++) void main()
scanf("%d",&num[i]); {
for(i=0;i<size;i=i+2) int mark[50],i,size,count=0;
printf("%d\n”,num[i]);
getch(); printf("Enter size of an
} array\n");
OUTPUT: scanf("%d", &size);
Enter size of an array printf("Enter Array
5 elements\n");
Enter Array elements for(i=0;i<size;i++)
12345 scanf("%d",&mark[i]);
Even elements for(i=0;i<size;i++)
1 { if(mark[i]>=50)
3 {
5 count++;
}
5. Program to find the sum even }
position element printf("No.of pass mark:
%d",count);
#include<stdio.h>
getch(); 55
} 66
OUTPUT: 77
Enter size of an array 88
5
Enter marks Enter the value to be searched
50 66
55 value is found in position:3
23
45 8. Program to find smallest element
78 in a array
No. of pass marks:3
#include<stdio.h>
7. Program to search an element in #include<conio.h>
an array void main()
#include<stdio.h> {
#include<conio.h> int num[50],i,size,min;
void main() printf("Enter size of an
{ array\n");
int A[20],i,n,value; scanf("%d", &size);
int flag=0; printf("Enter Array
printf("Enter the size of elements\n");
for(i=0;i<size;i++)
array\n");
scanf("%d",&num[i]);
scanf("%d",&n);
min=num[0];
printf("Enter Array
for(i=0;i<size;i++)
elements\n"); if(num[i]<min)
for(i=0;i<n;i++) min=num[i];
scanf("%d", &A[i]); printf("Largest element;
printf("Enter the value to be %d",min);
seacrched\n"); getch();
scanf("%d",&value); }
for(i=0;i<n;i++) OUTPUT:
{ Enter the size of array
if(A[i]== value) 5
{ printf("value is found in Enter Array elements
position:%d\n", i+1); 44 55 66 77 88
flag=1; Smallest element: 44
}
} 9. Program to copy elements of one
if(flag==0) array to another
printf("Value not found\n"); #include<stdio.h>
getch(); #include<conio.h>
} void main()
OUTPUT: {
Enter the size of array int A[50],i,size,B[50];
5
Enter Array elements printf("Enter size of an
44 array\n");
scanf("%d", &size); Enter Array elements
printf("Enter Array 44 55 66 77 88
elements\n"); Reverse array
for(i=0;i<size;i++) 88 77 66 55 44
scanf("%d",&A[i]);
for(i=0;i<size;i++) 11.Program to add 2 matrices
B[i]=A[i];
printf("First array\n"); #include<stdio.h>
for(i=0;i<size;i++) int main()
printf("%d\n",A[i]); {
printf("Second array\n"); int a[10][10], b[10][10],
for(i=0;i<size;i++) c1[10][10],r,c, i, j;
printf("%d\n",B[i]); printf("\nEnter the row and
getch(); column size of matrix: ");
} scanf("%d%d",&r,&c);
OUTPUT: printf("\nEnter First
Enter the size of an array Matrix :");
5 for (i = 0; i < r; i++)
Enter Array elements for (j = 0; j < c; j++)
44 55 66 77 88 scanf("%d", &a[i][j]);
First array printf("\nEnter Second
44 55 66 77 88 Matrix:\n");
Second array for (i = 0; i < r; i++)
44 55 66 77 88 for (j = 0; j < c; j++)
scanf("%d", &b[i][j]);
10.Program to display the array in printf("The First Matrix
is: \n");
reverse order
for (i = 0; i <r; i++)
#include<stdio.h>
for (j = 0; j <c; j++)
#include<conio.h>
c1[i][j]=a[i][j]+b[i][j];
void main()
printf("\nAddition Of Two
{
Matrices : \n");
int A[50],i,size;
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
printf("Enter size of an
printf(" %d ", c1[i][j]);
array\n"); }
scanf("%d", &size); printf("\n");
printf("Enter Array }
elements\n"); return (0);
for(i=0;i<size;i++) }
scanf("%d",&A[i]); OUTPUT:
printf("Reverse array\n");
for(i=size-1;i>=0;i++) Enter the row and column size
printf("%d\n",A[i]); of matrix: 3
getch(); 3
} Enter First Matrix :2
OUTPUT: 22222222
Enter the size of an array Enter Second Matrix:3
5 33333333
The First Matrix is:
2 2 2 333
2 2 2 333
2 2 2 333
The Second Matrix is : Enter Second Matrix:
3 3 3 222
3 3 3 222
3 3 3 222
Addition Of Two Matrices : Subtraction Of Two Matrices :
5 5 5 1 1 1
5 5 5 1 1 1
5 5 5 1 1 1

12.Program to subtract 2 matrices 13.Program to multiply two matrices


#include<stdio.h> #include<stdio.h>
int main( ) int main()
{ {
int a[10][10], b[10][10], int a[5][5], b[5][5], c[5][5], i,
c1[10][10],r,c, i, j; j, k,r1,c1,r2,c2;
printf("\nEnter the row and printf("\nEnter size of First
column size of matrix: "); Matrix : \n");
scanf("%d%d",&r,&c); scanf("%d%d",&r1,&c1);
printf("\nEnter First printf("\nEnter size of
Matrix :"); Second Matrix : \n");
for (i = 0; i < r; i++) scanf("%d%d",&r2,&c2);
for (j = 0; j < c; j++) if(c1==r2)
scanf("%d", &a[i][j]); {
printf("\nEnter Second printf("\nEnter First
Matrix:\n");
Matrix : \n");
for (i = 0; i < r; i++)
for (i = 0; i < r1; i++) {
for (j = 0; j < c; j++)
for (j = 0; j < c1; j++) {
scanf("%d", &b[i][j]);
scanf("%d", &a[i][j]);
for (i = 0; i <r; i++)
}
for (j = 0; j <c; j++)
}
c1[i][j]=a[i][j]- b[i][j];
printf("\nEnter Second
printf("\Subtraction Of Two
Matrices : \n"); Matrix:\n");
for (i = 0; i < r; i++) { for (i = 0; i < r2; i++) {
for (j = 0; j < c; j++) { for (j = 0; j < c2; j++) {
printf(" %d ", c1[i][j]); scanf("%d", &b[i][j]);
} }
printf("\n"); }
} printf("The First Matrix
return (0); is: \n");
} for (i = 0; i < r1; i++) {
OUTPUT: for (j = 0; j < c1; j++) {
printf(" %d ", a[i][j]);
Enter the row and column size }
of matrix: 3 3 printf("\n");
Enter First Matrix :3 }
printf("The Second Matrix 1 1 1
is : \n"); The Second Matrix is :
for (i = 0; i < r2; i++) { 2 2
for (j = 0; j < c2; j++) { 2 2
printf(" %d ", b[i][j]); 2 2
} Multiplication Of Two Matrices
printf("\n"); :
} 6 6
6 6
//Multiplication Logic
for (i = 0; i < r1; i++) { 14.Program to find the transpose of a
for (j = 0; j <c2; j++) { matrix
c[i][j]=0; #include<stdio.h>
for (k = 0; k < c1; k++) { int main( )
c[i][j] = c[i][j] + a[i][k] * {
b[k][j]; int a[5][5], b[5][5], r1,c1,i, j;
} printf("\nEnter size of First
Matrix : \n");
} scanf("%d%d",&r1,&c1);
} printf("\nEnter elements of
Matrix : \n");
printf("\nMultiplication Of for (i = 0; i < r1; i++)
Two Matrices : \n"); for (j = 0; j < c1; j++)
for (i = 0; i < r1; i++) { scanf("%d", &a[i][j]);
for (j = 0; j < c2; j++) { printf("The First Matrix
printf(" %d ", c[i][j]); is: \n");
} for (i = 0; i < r1; i++)
printf("\n"); {for (j = 0; j < c1; j++)
} printf(" %d ", a[i][j]);
} printf("\n");
else }
printf("First array col and for (i = 0; i <c1; i++) {
second array row same!"); for (j = 0; j < r1; j++) {
return (0); b[i][j]=a[j][i];
} }
OUTPUT: }
Enter size of First Matrix : printf("\nTranspose Matrix
2 is : \n");
3 for (i = 0; i < r1; i++) {
Enter size of Second Matrix : for (j = 0; j < c1; j++) {
3 printf(" %d ", b[i][j]);
2 }
Enter First Matrix : printf("\n") ;
1 11111 }
Enter Second Matrix: return (0);
222222 }
The First Matrix is: OUTPUT:
1 1 1 Enter size of Matrix :
2 2
3 Enter elements of Matrix :
Enter elements of Matrix : 3333
223456 The Matrix is:
The Matrix is: 3 3
2 2 3 3 3
4 5 6 The Sum of Matrix is : 12
The Transpose Matrix is :
2 4
2 5
3 6 16.Program to print only upper
triangle matrix
15. Program to find the
sum of all elements in a #include<stdio.h>
int main( )
matrix
{
int a[5][5], r1,c1,i, j;
#include<stdio.h>
printf("\nEnter size of First
int main()
{ Matrix : \n");
int a[5][5], r1,c1,i, j,sum=0; scanf("%d%d",&r1,&c1);
printf("\nEnter size of First printf("\nEnter elements of
Matrix : \n"); Matrix : \n");
scanf("%d%d",&r1,&c1); for (i = 0; i < r1; i++) {
printf("\nEnter elements of for (j = 0; j < c1; j++) {
scanf("%d", &a[i][j]);
Matrix : \n");
}
for (i = 0; i < r1; i++) {
}
for (j = 0; j < c1; j++) {
printf("The Matrix is: \n");
scanf("%d", &a[i][j]);
for (i = 0; i < r1; i++) {
}
for (j = 0; j < c1; j++) {
}
printf(" %d ", a[i][j]);
printf("The Matrix is: \n");
}
for (i = 0; i < r1; i++) {
printf("\n");
for (j = 0; j < c1; j++) {
}
printf(" %d ", a[i][j]);
printf(“Upper matrix:\n”);
}
for (i = 0; i <c1; i++) {
printf("\n");
for (j = 0; j < r1; j++) {
}
if(i<=j)
for (i = 0; i <c1; i++) {
printf("%d\t",a[i][j]);
for (j = 0; j < r1; j++) {
}
sum=sum+a[j][i];
printf("\n");
}
}
printf("The Sum of Matrix is :
return (0);
%d",sum); }
return (0); OUTPUT:
}
OUTPUT:
Enter size of Matrix :
2
17.Program to display the identity 0 2 6
matrix 0 0 5
#include<stdio.h>
int main( ) 18.Program to display the lower
{ triangle matrix
int a[5][5], r1,c1,i, j; #include<stdio.h>
printf("\nEnter size of First int main( )
Matrix : \n"); {
scanf("%d%d",&r1,&c1); int a[5][5], r1,c1,i, j;
for (i = 0; i < r1; i++) { printf("\nEnter size of First
for (j = 0; j < c1; j++) { Matrix : \n");
if(i==j) scanf("%d%d",&r1,&c1);
a[i][j]=1; printf("\nEnter elements of
else Matrix : \n");
a[i][j]=0; for (i = 0; i < r1; i++) {
} for (j = 0; j < c1; j++) {
} scanf("%d", &a[i][j]);
printf("The Identity }
Matrix is: \n"); }
for (i = 0; i < r1; i++) { printf("The Matrix is: \n");
for (j = 0; j < c1; for (i = 0; i < r1; i++) {
j++) { for (j = 0; j < c1; j++) {
printf(" printf(" %d ", a[i][j]);
%d ", a[i][j]); }
} printf("\n");
printf("\n"); }
} printf(“lower matrix:\n”);
return (0); for (i = 0; i <c1; i++) {
} for (j = 0; j < r1; j++) {
OUTPUT: if(i>=j)
Enter size of First Matrix : printf("%d\t",a[i][j]);
3 }
3 printf("\n");
Enter elements of Matrix : }
2 return (0);
3 }
4 OUTPUT:
1 Enter size of First Matrix :
2 3
6 3
7 Enter elements of Matrix :
4 456489765
5 The Matrix is:
The Matrix is: 4 5 6
2 3 4 4 8 9
1 2 6 7 6 5
7 4 5 Lower matrix:
Upper matrix: 4 0 0
2 3 4 4 8 0
7 6 5 The Diagonal Element of
Matrix is:
19.Program to display the matrix 5
with only diagonal 2
5
#include<stdio.h>
int main( )
{ FACTORIAL OF A NUMBER
int a[5][5], r1,c1,i, j; NO ARGUMENT AND NO RETURN TYPE:
printf("\nEnter size of First #include<stdio.h>
Matrix : \n"); void fun();
scanf("%d%d",&r1,&c1); void main()
printf("\nEnter elements of {
Matrix : \n"); fun();
for (i = 0; i < r1; i++) { }
for (j = 0; j < c1; j++) { void fun()
scanf("%d", &a[i][j]); {
} int i,n,fact=1;
} printf("enter the value");
printf("The Matrix is: \n"); scanf("%d",&n);
for (i = 0; i < r1; i++) { for(i=1;i<=n;i++)
for (j = 0; j < c1; j++) { {
printf(" %d ", a[i][j]); fact=fact*i;
} }
printf("\n"); printf("%d",fact);
} }
printf(“Diagonal elements OUTPUT:
:\n”); enter the value5
for (i = 0; i <c1; i++) { 120
for (j = 0; j < r1; j++) {
if(i==j)
printf("%d\t",a[i][j]); WITH ARGUMENT AND NO RETURN
else TYPE:
printf(“\t”);
} #include<stdio.h>
printf("\n"); void fun(int n);
} void main()
return (0); {
} int n;
OUTPUT: printf("enter a number");
Enter size of Matrix : scanf("%d",&n);
3 3 fun(n);
Enter Element of Matrix : }
5 6 4 3 25 7 6 5 void fun(int n)
Matrix is: {
564 int i,fact=1;
325 for(i=1;i<=n;i++)
765 {
fact=fact*i;
} for(i=1;i<=n;i++)
printf("%d",fact); {
} fact=fact*i;
}
OUTPUT: return fact;
enter a number5 }
120
OUTPUT:
WITH ARGUMENT AND WITH RETURN enter a number5
TYPE: 120
#include<stdio.h>
int fun(int n);
void main() Maximum of two numbers using
{ function
int n,result;
printf("enter the terms");
#include<stdio.h>
scanf("%d",&n);
result=fun(n); int maximum(int n1, int n2);
printf("%d",result); int main ()
} {
fun(n) int x = 100;
{ int y= 200;
int i,fact=1; int max;
for(i=1;i<=n;i++)
max= maximum(x, y);
{
fact=fact*i; printf( "Max value is : %d\n", max);
} return 0;
return fact; }
} int maximum(int n1, int n2)
OUTPUT: {
enter a number5 int result;
120
if (n1 > n2)
result = n1;
WITHOUT ARGUMENT WITH RETURN else
TYPE: result = n2;
#include<stdio.h> return result;
int fun(); }
void main()
{ Output:
int result;
result=fun(); Max value is :200
printf("%d",result);
} Function with no argument and no
int fun() return value
{
int n,i=1,fact=1; Example Program to find the square of a
printf("enter the term"); number
scanf("%d",&n); #include<stdio.h>
void square( ); int square(int a )
void main( ) {
{ int result
square( ); result=a*a;
} return result;
void square( ) }
{ Output:
int n; Enter number: 15
printf(“Enter number:”); Result: 225
scanf("%d",&n);
result=n*n;
printf("Result:%d\n",result); Function with no argument and return
} value
Output: #include<stdio.h>
Enter number: 15 int square( );
Result: 225 void main( )
{
With argument and no return value int ans;
#include<stdio.h> ans= square( );
void square(int ); }
void main( ) void square( )
{ { int n, result;
int n; printf(“Enter number:”);
printf(“Enter number:”); scanf("%d",&n);
scanf("%d",&n); result=n*n;
square(n); return result;
} }
void square( int a ) Output:
{ Enter number: 15
int result; Result: 225
result=a*a;
printf("Result:%d\n",result); Passing arguments to a function
}
Output:  Pass by Value/call by value
Enter number: 15
Result: 225
void swap(int x, int y)
Function with argument and return {
value int temp;
temp = x;
#include<stdio.h> x = y;
int square(int ); y = temp;
void main( ) printf("After swap, value of a : %d\n",
{ x );
int n,ans; printf("After swap, value of b : %d\n",
printf(“Enter number:”); y );
scanf("%d",&n); }
ans=square(n); #include <stdio.h>
} void swap(int x, int y);
int main () 1. Sum of first n natural numbers
{ #include <stdio.h>
int a = 100; int sum(int n);
int b = 200; int main()
printf("Before swap, value of a : %d\n", {
a ); int number, result;
printf("Before swap, value of b : printf("Enter a positive integer: ");
%d\n", b ); scanf("%d", &number);
swap(a, b); result = sum(number);
printf("sum=%d", result);
return 0; }
} int sum(int n)
{
Pass by Reference/Call by reference if (n!=0)
return (n + sum(n-1));
#include <stdio.h> else
void swap(int *x, int *y); return n;
int main () }
{
int a = 100; Output
int b = 200;
Enter a positive integer:
3
printf("Before swap, value of a :
6
%d\n", a );
printf("Before swap, value of b :
%d\n", b ); 2. Factorial of a number by recursion
swap(&a, &b);
printf("After swap, value of a : %d\n",
#include <stdio.h>
a );
printf("After swap, value of b : %d\n", int factorial(int i);
b ); int main()
return 0; {
} int i = 5;
Output: printf("Factorial of %d is %d\n", i,
Before swap, value of a :100 factorial(i));
Before swap, value of b :200 return 0;
After swap, value of a :200 }
After swap, value of b :100 int factorial(int i)
{
void swap(int *x, int *y)
{ if(i <= 1)
int temp; {
temp = *x; return 1;
*x = *y; }
*y = temp; return (i * factorial(i - 1));
} }
Output:
RECURSION Factorial of 5 is 120
3. Fibonacci series by recursion {
#include <stdio.h> int x;
int fibonacci(int n); int y;
int main(){ };
int n, i; void main()
printf("Enter number : "); {
scanf("%d", &n); struct complex c1,c2;
printf("Fibonacci series”); printf("Enter the first complex
for(i= 0; i < n; i++){ number\n");
printf("%d ", fibonacci(counter)); scanf("%d %d",&c1.x,&c1.y);
} printf("Enter the second complex
return 0; number\n");
} scanf("%d %d",&c2.x,&c2.y);
int fibonacci(int n){ sum(c1,c2);
if(n < 2) }
return n; void sum(struct complex c1, struct
return fibonacci(n - 1) + fibonacci(n - 2); complex c2)
} {
struct complex c3;
Dynamic memory allocation c3.x=c1.x+c2.x;
Sum of n elements using malloc c3.y=c1.y+c2.y;
#include <stdio.h> printf("Resultant :%d+i%d",c3.x,c3.y);
#include <stdlib.h> }
int main()
{ Student structure
int num, i, *ptr, sum = 0; #include <stdio.h>
printf("Enter number of elements: "); struct student
scanf("%d", &num); {
//memory allocated using malloc char name[50];
int roll;
ptr = (int*) malloc(num * sizeof(int)); float marks;
//ptr = (int*) calloc(num, sizeof(int)); };
// memory allocated using calloc int main()
printf("Enter elements of array: "); {
for(i = 0; i < num; ++i) struct student s;
{ printf("Enter information:\n");
scanf("%d", ptr + i); printf("Enter name: ");
sum += *(ptr + i); scanf("%s", s.name);
} printf("Enter roll number: ");
printf("Sum = %d", sum); scanf("%d", &s.roll);
free(ptr); printf("Enter marks: ");
return 0; scanf("%f", &s.marks);
}
Sum of 2 complex numbers using printf("Displaying Information:\n");
structure as function arguments printf("Name: ");
#include<stdio.h> puts(s.name);
void sum(structcomplex,struct printf("Roll number: %d\n",s.roll);
complex); printf("Marks: %.1f\n", s.marks);
struct complex return 0;
} struct book b1 ;

/*C program to read and print printf("\nEnter names, prices & no. of
employee's record using structure*/ pages of 3 books\n");
scanf("%c %f %d", &b1.name,
#include <stdio.h> &b1.price, &b1.pages);
struct employee{
char name[30]; printf("\n\nAnd this is what you
int empId; entered");
float salary; printf("\n%c %f %d", b1.name, b1.price,
}; b1.pages);
}
int main()
{ Passing structure to function
/*declare structure variable*/
struct employee emp; #include<stdio.h>
printf("\nEnter name id and salary void sum(struct complex, struct
:\n"); complex);
gets(emp.name); struct complex
scanf("%d",&emp.empId); {
scanf("%f",&emp.salary); int x;
/*print employee details*/ int y;
printf("\nEntered detail is:"); };
printf("Name: %s",emp.name); void main()
printf("Id: %d",emp.empId); {
printf("Salary: %f\n",emp.salary); struct complex c1,c2;
return0; printf("Enter the first complex
} number\n");
Output scanf("%d %d",&c1.x,&c1.y);
Enter name id and salary: printf("Enter the second complex
Mike number\n");
1120 scanf("%d %d",&c2.x,&c2.y);
76543 sum(c1,c2);
}
Entered detail is: void sum(struct complex c1, struct
Name: Mike complex c2)
Id: 1120 {
Salary: 76543.000000 struct complex c3;
c3.x=c1.x+c2.x;
BOOK Structure c3.y=c1.y+c2.y;
#include<stdio.h> printf("Resultant complex number:%d+i
main() %d",c3.x,c3.y);
{ }
struct book
{
char name; //Program to enter two points and
float price; then calculate the distance between
int pages; them.
};
#include<stdio.h> // If inch is greater than 12, changing
#include<conio.h> it to feet.
struct distance
{ intx,y; if (sum.inch>12.0)
}; {
int main() sum.inch = sum-12.0;
{ ++sum.feet;
struct distance d1,d2; }
float dis;
printf("Enter fisrt point x,y: "); printf("\nSum of distances = %d\'-
scanf("%d%d",&d1.x,&d1.y); %.1f\"",sum.feet, sum.inch);
printf("Enter second point x,y: "); return 0;
scanf("%d%d",&d2.x,&d2.y); }
d=sqrt((d2.x-d1.x)*(d2.x-d1.x)+(d2.y-
d1.y)*(d2.y-d1.y)); Output
printf("Distance is %f",dis);
getch(); Enter information for 1st distance
return 0; Enter feet: 23
} Enter inch: 8.6

//Program to add two distance in form Enter information for 2nd distance
of inch and feet. Enter feet: 34
Enter inch: 2.4
#include <stdio.h>
struct Distance Sum of distances = 57'-11.0"
{
int feet; Example : Program to calculate the sum
float inch; of 2 complex numbers using structure as
} d1, d2, sum; function arguments
#include<stdio.h>
int main() void sum(structcomplex,struct
{ complex);
printf("Enter information for 1st struct complex
distance\n"); {
printf("Enter feet: "); int x;
scanf("%d", &d1.feet); int y;
printf("Enter inch: "); };
scanf("%f", &d1.inch); void main()
{
printf("\nEnter information for 2nd struct complex c1,c2;
distance\n"); printf("Enter the first complex
printf("Enter feet: "); number\n");
scanf("%d", &d2.feet); scanf("%d %d",&c1.x,&c1.y);
printf("Enter inch: "); printf("Enter the second complex
scanf("%f", &d2.inch); number\n");
scanf("%d %d",&c2.x,&c2.y);
sum.feet = d1.feet+d2.feet; sum(c1,c2);
sum.inch = d1.inch+d2.inch; }
void sum(struct complex c1, struct float salary;
complex c2) intworker_no;
{ }u;
struct complex c3; int main(){
c3.x=c1.x+c2.x; printf("Enter name:\n");
c3.y=c1.y+c2.y; scanf("%s",&u.name);
printf("Resultant complex number: printf("Enter salary: \n");
%d+i%d",c3.x,c3.y); scanf("%f ",&u.salary);
} printf("Displaying\nName :
%s\n",u.name);
printf("Salary: %.1f ",u.salary);
Self Referential structure EXAMPLE
return 0;
PROGRAM:TO LINK 2 NODES
}
#include <stdio.h>
#include <stdlib.h> Output
struct node { Enter name
int x; Hillary
struct node *next; Enter salary
}; 1234.23
int main() Displaying
{ Name: f%Bary
struct node *node1,*node2; Salary: 1234.2
node1 = (struct node *)
malloc(sizeof(struct node) );
node2 = (struct node *) ACCESSING DATA THROUGH
malloc(sizeof(struct node) ); POINTERS
printf("Enter first node value:\n"); 1. #include<stdio.h>
scanf("%d",&node1->x); main()
node1->next =node2; {
printf("Enter second node value:\n"); int a=22,*a;
scanf("%d",&node2->x); float b=2.25,*b;
node2->next=node1; a=&a;
printf("Node1 is stored in b=&b;
%u\n",node1); printf(“\n value of a=%d”,*a);
printf("Node1:%d-> %u\n",node1- printf(“\n value of b=%d”,*b);
>x,node1->next); }
printf("Node2 is stored in
%u\n",node2);
printf("Node2:%d-> %u\n",node2- OUTPUT
>x,node2->next); Value of a=22
} Value of b=2.25

2. #include<stdio.h>
Example program for C union: main()
{
#include <stdio.h> int a=22,*a;
union job { printf(“\n Value of a=%d”,*a);
char name[32]; printf(“\n Address of a=%u”,&a);
printf(“\n Value at address %u= #include<stdio.h>
%d”,&a,*(&a)); #include<conio.h>
} void main()
{
OUTPUT int num1,num2,opt,result;
Value of a=22 char ch;
Address of a=4000 int *p1,*p2;
Value at address 4000=22 p1=&num1;
p2=&num2;
Write a C program to Add two numbers printf("Enter two numbers\n");
using pointer scanf("%d %d",p1, p2);
do
Program to calculate the area of {
circle using pointers printf("Choose the option(1-5) from
#include<stdio.h> the following menu\n");
void main( ) printf("1.Addition\n");
{ printf("2.Subtraction\n");
float radius, area; printf("3.Multiplication\n");
float *p=&radius; printf("4.Division\n");
printf(“Enter the radius\n”); printf("5.Modulus\n");
scanf(“%f”,p); printf("6.Exit\n");
area=3.14 * (*p) * (*p); scanf("%d",&opt);
printf(“Area=%f”,area); switch(opt)
} {
case 1: result=*p1+*p2;
OUTPUT: break;
Enter the radius case 2: result=*p1-*p2;
4.5 break;
Area=63.585 case 3: result=*p1* *p2;
break;
Write a C program to find simple
case 4: result=(*p1)/(*p2);
interest using pointer.
break;
#include<stdio.h>
case 5: result=(*p1)%(*p2);
void main( )
break;
{
case 6: exit(0);
int p,n,r,*pp,*np,*rp;
break;
float si;
}
*pp=&p;
printf("Result=%d",result);
*np=&n;
}while(opt<=5);
*rp=&r;
getch();
printf(“Enter the amount, year, rate
}
of interest\n”);
scanf(“%d%d%d”,pp,np,rp); Program to calculate the distance
si=*pp**np**rp/100; between two points
printf(“interest=%f”,si); #include<stdio.h>
} #include<conio.h>
#include<math.h>
Program to perform arithmetic void main()
operations using pointers {
int x1,x2,y1,y2,*a,*b,*c,*d, dist;
a=&x1;
b=&x2;
c=&y1;
d=&y2;
printf("Enter the first point");
scanf("%d %d",a,c);
printf("Enter the second point");
scanf("%d %d",b,d);
dist=sqrt((((*a)-(*b))*((*a)-(*b)))+
(((*c)-(*d))*((*c)-(*d))));
printf("Distance=%d",dist);
getch();
}
OUTPUT:
Enter the first point
8 10
Enter the second point
5 6
Distance= 5

Potrebbero piacerti anche