Programming For Problem Solving (PPS)
Programming For Problem Solving (PPS)
#include<stdio.h>
#include<conio.h>
Void main()
{
int a, b, sum=0;
printf(“Enter the number in a”);
scanf(“%d”, &a);
printf(“Enter the number in b”);
scanf(“%d”, &b);
sum= a+b;
printf(“Addition of two number is %d”, sum);
getch();
}
PROGRAM 2
AIM: Write a Program to calculate Simple Interest
#include<stdio.h>
#include<conio.h>
void main()
{
int amount, rate, time, si ;
printf("\n Enter Principal Amount : ");
scanf("%d", &amount);
printf("\n Enter Rate of Interest : ");
scanf("%d", &rate);
printf("\n Enter Period of Time : ");
scanf("%d", &time);
si = (amount * rate * time) / 100;
printf("\n Simple Interest : %d", si);
getch();
}
PROGRAM 3
AIM: Write a Program to find larger among two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,large;
printf(“enter the first number :”);
scanf(“%d”,&a);
printf(“enter the second number :”);
scanf(“%d”,&b);
if(b>a)
{
printf(“largest number =%d”,b);
}
else
{
printf(“largest number =%d”, a);
}
getch();
}
PROGRAM 4
AIM: Write a Program to find largest among three numbers
#include<stdio.h>
#include<conio.h>
void main()
{
Int n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
else
printf("%d is the largest number.", n3);
getch();
}
PROGRAM 5
Write a Program to design calculator using switch case.
#include <stdio.h>
void main()
{
int num1,num2;
float result=0;
char ch;
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1;i<=10;i++)
printf("%d\n" ,i );
getch();
}
PROGRAM 7
#include <stdio.h>
#include<conio.h>
void main()
{
int n, t, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}
printf("Sum of digits of %d = %d\n", n, sum);
getch();
}
PROGRAM 9
#include <stdio.h>
int main()
#include<stdio.h>
#include<conio.h>
Void main()
{
int n, reversedNumber = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
printf("Reversed Number = %d", reversedNumber);
getch();
}
PROGRAM 10
AIM: Write a Program to print table of a number
#include <stdio.h>
#include<conio.h>
void main()
{
int n, i;
printf("Enter an integer: ");
scanf("%d",&n);
for(i=1; i<=10; ++i)
{
printf("%d * %d = %d \n", n, i, n*i);
}
getch();
}
PROGRAM 11
AIM: Write a Program to print the Fibonacci series
#include<stdio.h>
#include<conio.h>
void main()
{
int n, first = 0, second = 1, next, c;
getch();
}
PROGRAM 13
AIM: Write a Program to find a^b
# include <stdio.h>
# include <conio.h>
void main()
{
int x, n, count = 1, sum = 1 ;
clrscr() ;
printf("Enter the value of x : ") ;
scanf("%d", &x) ;
printf("\nEnter the value of n : ") ;
scanf("%d", &n) ;
while(count <= n)
{
sum = sum * x ;
count ++ ;
}
printf("\nThe power of %d^%d is : %d", x, n, sum) ;
getch() ;
}
PROGRAM 14
AIM: Write a Program to check if number is Prime
#include<stdio.h>
#include<conio.h>
Void main()
{
int i,n,c=0;
printf ("Enter a number \n");
scanf ("%d",&n);
for (i=1;i<=n;i++)
{
if(n%i==0)
c=c+1;
}
if (c==2)
printf ("The number is PRIME");
else
printf ("The number is COMPOSITE");
getch();
}
PROGRAM 15
AIM: . Write a Program to find largest and smallest element in an array
#include<stdio.h>
#include<conio.h>
Void main()
{
int i, n, lar,sm, elem;
printf ("Enter total number of elements \n");
scanf ("%d", &elem);
printf ("Enter first number \n");
scanf ("%d", &n);
lar = n;
sm=n;
for (i=1; i<= elem -1 ; i++)
{
printf ("\n Enter another number \n");
scanf ("%d",&n);
if (n>lar)
lar=n;
if (n<sm)
sm=n;
}
printf ("\n The largest number is %d", lar);
printf ("\n The smallest number is %d", sm);
getch();
}
PROGRAM 16
getch();
}
PROGRAM 17
AIM: Write a Program to multiply two 2-D arrays
#include<stdio.h>
#include<conio.h>
Void main() {
int a[10][10], b[10][10], c[10][10], i, j, k;
int sum = 0;
printf("\nEnter First Matrix : n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}
printf("\nEnter Second Matrix:n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &b[i][j]);
}
}
printf("The First Matrix is: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", a[i][j]);
}
printf("\n");
}
printf("The Second Matrix is : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", b[i][j]);
}
printf("\n");
}
//Multiplication Logic
for (i = 0; i <= 2; i++) {
for (j = 0; j <= 2; j++) {
sum = 0;
for (k = 0; k <= 2; k++) {
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}
printf("\n Multiplication Of Two Matrices : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", c[i][j]);
}
printf("\n");
}
getch();
}
PROGRAM 18
#include<stdio.h>
#include<conio.h>
void largest();
void main()
{
largest();
getch();
}
void largest()
{
int x, y, largest;
printf("\n Enter values for a and b:");
scanf("%d%d",&x,&y);
if(x>y)
printf("\n Largest No. is %d ",x);
else
printf("\n Largest No. is %d ",y);
}
#include<stdio.h>
#include<conio.h>
int largest();
void main()
int large;
{
large = largest();
printf("\n Larger num is %d”, large);
getch();
}
int largest()
{
int x, y, largest;
printf("\n Enter values for a and b:");
scanf("%d%d",&x,&y);
if(x>y)
return x;
else
return y;
}
#include<stdio.h>
#include<conio.h>
void largest(int x, int y);
void main()
{
int a, b;
printf("\n Enter values for a and b:");
scanf("%d%d",&a,&b);
largest(a,b);
getch();
}
void largest(int x, int y)
{
if(x>y)
printf("\n Largest No. is %d ",x);
else
printf("\n Largest No. is %d ",y);
}
#include<stdio.h>
#include<conio.h>
int largest(int x, int y);
void main()
{
int a, b, large;
printf("\n Enter values for a and b:");
scanf("%d%d",&a,&b);
large = largest(a,b);
printf("\n Larger amongst %d and %d is %d\n",a, b, large);
getch();
}
int largest(int x, int y)
{
if(x>y)
return x;
else
return y; }
PROGRAM 19
AIM: Write a Program to calculate factorial of a number using functions
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f;
clrscr();
printf("enter a number");
scanf("%d",&n);
f=factorial(n);
printf("the factorial of the number %d is %d",n,f);
getch();
}
int factorial(int num)
{
int fact=1,i;
{
for(i=1;i<=num;i++)
{
fact=(fact*i);
}
}
return(fact);
}
PROGRAM 20
AIM: Write a Program to calculate factorial of a number using recursion
#include<stdio.h>
int fact(int);
void main()
{
int n,t;
clrscr();
printf("Enter any Number : ");
scanf("%d",& n);
t=fact(n);
printf("Factorial of %d is : %d",n,t);
getch();
}
int fact(int a)
{
int p;
if(a==1)
return(1);
else
{
p=a*fact(a-1);
return(p);
}
}
PROGRAM 21
AIM: Write a Program to calculate marks using array of structures.
#include<stdio.h>
struct student
{
int sub1;
int sub2;
int sub3;
};
void main()
{
struct student s[10];
int i,total=0;
clrscr();
for(i=0;i<=2;i++)
{
printf("\nEnter Marks in Three Subjects = ");
scanf("%d%d%d",& s[i].sub1,&s[i].sub2,&s[i].sub3);
total=s[i].sub1+s[i].sub2+s[i].sub3;
printf("\nTotal marks of s[%d] Student= %d",i,total);
}
getch();
}
PROGRAM 22
AIM: Write a Program to use inbuilt string functions
• String length(strlen)
#include<stdio.h>
#include<conio.h>
Void main() {
char str[20];
int length ;
printf("\nEnter the String : ");
gets(str);
length = strlen(str);
printf("\nLength of String : %d ", length);
getch();
}
a. String copy (strcpy)
#include <stdio.h>
#include <string.h>
void main () {
char src[40];
char dest[40];
printf("\n Enter the String : ");
gets(src);
strcpy(dest, src);
printf("\n Source String : %s\n ", src);
printf("Final copied string : %s\n", dest);
getch();
}
b. string compare(strcmp)
#include <stdio.h>
#include <string.h>
void main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
if (strcmp(a,b) == 0)
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
getch();
}
#include<stdio.h>
#include<string.h>
void main()
{
char name[30] = "Hello";
printf("String before strrev( ) : %s\n",name);
printf("String after strrev( ) : %s",strrev(name));
getch();
}
d. String Concatenation (strcat)
#include <stdio.h>
#include <string.h>
void main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a);
getch();
}
PROGRAM 23
. AIM: Write a Program to check whether entered string is palindrome
#include <stdio.h>
#include <string.h>
int main(){
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
for(i=0;i < length ;i++){
if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}
if (flag) {
printf("%s is not a palindrome", string1);
}
else {
printf("%s is a palindrome", string1);
}
return 0;
}
PROGRAM 24
AIM: Write a Program to calculate marks using array of structures.
#include<stdio.h>
struct student
{
int sub1;
int sub2;
int sub3;
};
void main()
{
struct student s[10];
int i,total=0;
clrscr();
for(i=0;i<=2;i++)
{
printf("\nEnter Marks in Three Subjects = ");
scanf("%d%d%d",& s[i].sub1,&s[i].sub2,&s[i].sub3);
total=s[i].sub1+s[i].sub2+s[i].sub3;
printf("\nTotal marks of s[%d] Student= %d",i,total);
}
getch();
}
PROGRAM 25
AIM: Write a Program to copy the contents of one file to another file
#include <stdio.h>
Void main()
{
FILE *fp1, *fp2;
char ch;
fp1 = fopen("abc.txt", "r");
fp2 = fopen("xyz.txt", "w");
while((ch = getc(fp1)) != EOF)
putc(ch, fp2);
fclose(fp1);
fclose(fp2);
getch();
}
That's it.
Now a new file will be created with a name "xyz.c" and all the content would
copy from abc.txt to xyz.txt.
PROGRAM 26
AIM: Write a Program to calculate roots of a quadratic equation
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void main()
{
float a, b, c, root1, root2;
float realp, imagp, disc;
printf("Enter the values of a, b and c \n");
scanf("%f %f %f", &a, &b, &c);
/* If a = 0, it is not a quadratic equation */
if (a == 0 || b == 0 || c == 0)
{
printf("Error: Roots cannot be determined \n");
exit(1);
}
else
{
disc = b * b - 4.0 * a * c;
if (disc < 0)
{
printf("Imaginary Roots\n");
realp = -b / (2.0 * a) ;
imagp = sqrt(abs(disc)) / (2.0 * a);
printf("Root1 = %f +i %f\n", realp, imagp);
printf("Root2 = %f -i %f\n", realp, imagp);
}
else if (disc == 0)
{
printf("Roots are real and equal\n");
root1 = -b / (2.0 * a);
root2 = root1;
printf("Root1 = %f\n", root1);
printf("Root2 = %f\n", root2);
}
else if (disc > 0 )
{
printf("Roots are real and distinct \n");
root1 =(-b + sqrt(disc)) / (2.0 * a);
root2 =(-b - sqrt(disc)) / (2.0 * a);
printf("Root1 = %f \n", root1);
printf("Root2 = %f \n", root2);
}}
getch();
}