Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
#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
/*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