CPPM
CPPM
CPPM
(Sem I)
Name=paresh suthar
104- Computer Programming & Programming Methodology Journal Program
Write a program to take input of name, rollno and marks obtained by a student in
4 subjects of 100 marks each and display the name, rollno with percentage score
secured.
solution--->
#include<stdio.h>
#include<conio.h>
int main(){
char name;
int rollno,sub1,sub2,sub3,sub4,sum;
float per;
printf("Student Full Name : ");
scanf("%s",&name);
printf("\nStudent Rollno. : ");
scanf("%d",&rollno);
//entering marks of subject
printf("CPPM : ");
scanf("%d",&sub1);
printf("MATHS : ");
scanf("%d",&sub2);
printf("DMA : ");
scanf("%d",&sub3);
printf("IC : ");
scanf("%d",&sub4);
sum=sub1+sub2+sub3+sub4;
per=sum*100/400;
printf("YOUR TOTAL MARKS IS : %d",sum);
printf("\nYOUR PERCENTAGE IS : %.2f",per);
return 0;
}
Output-
Student Full Name : paresh
Student Rollno. : 657
CPPM : 82
MATHS : 71
DMA : 80
IC : 77
YOUR TOTAL MARKS IS : 310
YOUR PERCENTAGE IS : 77.50
Write a program to find GCD (greatest common divisor or HCF) and LCM (least
common multiple) of two numbers.
Solution-
#include<stdio.h>
#include<conio.h>
int main()
{
int num1, num2, gcd, lcm, i;
clrscr();
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
for(i=1; i<=num1; i++)
{
if(num1%i==0 && num2%i==0)
{
gcd = i;
}
}
Output-
Enter number to check: 7
Prime number.
Write a program to check whether the entered year is leap year or not (a year is
leap if it is divisible by 4 and divisible by 100 or 400.)
Solution
#include<stdio.h>
#include<conio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
Output-
Enter a year: 2012
2012 is a leap year.
Output-
Enter a number: 6
Factorial of 6 is: 720
#include<stdio.h>
#include<conio.h>
int main()
{
int n, r, sum = 0, temp;
printf("enter the number=");
scanf("%d", &n);
temp = n;
while (n > 0)
{
r = n % 10;
sum = sum + (r * r * r);
n = n / 10;
}
if (temp == sum)
printf("armstrong number ");
else
printf("not armstrong number");
getch();
return 0;
}
Output-
enter the number=153
armstrong number
Write a program to take the value from user as input any character and check
whether it is the alphabet, digit or special character. Using the switch statement.
Solution
#include<stdio.h>
#include<conio.h>
int main(void)
{
char i;
printf("Enter a character: \n");
scanf("%c",&i);
switch(i)
{
case 'A'...'Z':
printf("Upper case alphabet\n");
break;
case 'a'...'z':
printf("Lower case alphabet\n");
break;
case '0'...'9':
printf("Digit \n" );
break;
default:
printf("Special character\n");
}
getch();
return 0;
}
Output-
Enter a character: D
Upper case alphabet
#include<stdio.h>
#include<conio.h>
int main()
{
char c;
for (c = 'A'; c <= 'Z'; ++c)
{
printf("%c ", c);
}
getch();
return 0;
}
Output-
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
int i, n;
int t1 = 0, t2 = 1;
int nextterm = t1 + t2;
printf("Enter the number : ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
for (i = 3; i <= n; ++i)
{
printf("%d, ", nextterm);
t1 = t2;
t2 = nextterm;
nextterm = t1 + t2;
}
getch();
return 0;
}
Output-
Enter the number : 5
Fibonacci Series: 0, 1, 1, 2, 3,5
11. Write a program that will display large & small number from 1-D array.
Solution
#include<stdio.h>
#include<conio.h>
int main()
{
int a[50],i,n,large,small;
printf("\nEnter the number of elements :");
scanf("%d",&n);
printf("\nInput the array elements : ");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
getch();
return 0;
}
Output-
Enter the number of elements :5
Input the array elements : 5 4 2 3 1
The smallest element is 1
The largest element is 5
12. Write a program that will search an element in 1- D array & if it is found than
display the position of it in the array.
Solution
#include<stdio.h>
#include<conio.h>
int main()
{
int a[100], n, element, pos=0;
int i;
13. Write a program that will merge two 1-D arrays into single & display the
resultant array in ascending order.
Solution
#include<stdio.h>
#include<conio.h>
int main()
{
int n1,n2,n3;
int a[10000], b[10000], c[20000];
printf("Enter the size of first array: ");
scanf("%d",&n1);
printf("Enter the array elements: ");
for(int i = 0; i < n1; i++)
scanf("%d", &a[i]);
printf("Enter the size of second array: ");
scanf("%d",&n2);
printf("Enter the array elements: ");
for(int i = 0; i < n2; i++)
scanf("%d", &b[i]);
n3 = n1 + n2;
for(int i = 0; i < n1; i++)
c[i] = a[i];
for(int i = 0; i < n2; i++)
c[i + n1] = b[i];
getch();
return 0;
}
Output-
Enter the size of first array: 3
Enter the array elements: 12 43 54
Enter the size of second array: 4
Enter the array elements: 43 56 78 34
The merged array: 12 43 54 43 56 78 34
Final array after sorting: 12 34 43 43 54 56 78
14. Write a program that will check whether inputted string is palindrome or not.
For example, Input: str="madam” Output: string is palindrome.
Solution
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char a[100],b[100];
int n;
strcpy(b,a);
strrev(a);
n = strcmp(a,b);
printf("The reverse of string : %s\n",a);
if(n==0)
{
printf("\nThe string is palindrome...\n");
}
else
{
printf("\nThe string is not palindrome...\n");
}
getch();
return 0;
}
Output-
Enter the string : madam
The reverse of string : madam
The string is palindrome...
15. Write a program to print Inverted half Pyramid of * Pattern
Solution
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, rows;
printf (" Enter a number to define the rows: \n ");
scanf("%d", &rows);
printf("\n");
for (i = rows; i > 0; i--)
{
for (j = i; j > 0; j--)
{
printf ("* ");
}
printf ("\n");
}
getch();
}
Output-
Enter a number to define the rows: 5
* * * * *
* * * *
* * *
* *
*
Solution
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, rows, k = 0;
printf (" Enter a number to define the rows: \n");
scanf ("%d", &rows);
Output-
Enter a number to define the rows: 5
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
printf( " Enter a number to define the rows in Floyd's triangle: \n");
scanf( "%d", &num);
for (i = 1; i <= num; i++)
{
for (j = 1; j <= i; j++)
{
printf(" %2d", k++);
}
printf( "\n");
}
getch();
}
Output-
Enter a number to define the rows in Floyd's triangle: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Divesh Karwasara