KEMBAR78
C Programs | PDF | Namespace | Computer Programming
0% found this document useful (0 votes)
18 views7 pages

C Programs

The document contains ten C++ programs demonstrating various programming concepts. These include sorting an array, checking for prime numbers, calculating string length, finding factorials, using structures, swapping values with pointers, displaying numbers in words, calculating the sum of squares, checking for palindromes, and more. Each program is self-contained with input and output statements, showcasing fundamental programming techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

C Programs

The document contains ten C++ programs demonstrating various programming concepts. These include sorting an array, checking for prime numbers, calculating string length, finding factorials, using structures, swapping values with pointers, displaying numbers in words, calculating the sum of squares, checking for palindromes, and more. Each program is self-contained with input and output statements, showcasing fundamental programming techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1)

Create an array to store the heights of some students and sort the values

Program:-

#include <iostream>
using namespace std;
int main() {
int height[10],i,j,temp;
cout<<"Enter height of 10 students ";
for(i=0;i<10;i++)
cin>>height[i];
cout<<"The heights of 10 students entered are \n";
for(i=0;i<10;i++)
cout<<height[i]<<"\n";

for(i=0; i<9; i++)


{
for(j=0; j<(10-i-1); j++)
{
if(height[j]>height[j+1])
{
temp = height[j];
height[j] = height[j+1];
height[j+1] = temp;
}
}
}
cout<<"The heights of students in sorted order are \n";
for(i=0;i<10;i++)
cout<<height[i]<<"\n";
return 0;
}

2)Input an integer number and check whether it is a prime or not.

Program:-

#include <iostream>
using namespace std;

int main()
{

int n, i, c = 0;
cout << "Enter any number n: "; cin>>n;

for (i = 1; i <= n; i++)


{
if (n % i == 0)
{
c++;
}
}
if (c == 2)
{
cout << "n is a Prime number" << endl;
}
else
{
cout << "n is not a Prime number" << endl;
}
return 0;
}

3) Find the length of a string without using strlen() function.

Program :-

#include<iostream>
using namespace std;
int main( )
{
char name[1000];
int count=0;
cout<<"Enter the String";
cin.getline(name,1000);
while(name[count]!='\0')
{
count ++;
}
cout<<"The length of the string is"<<count;
return 0;
}

4)Define a function to find the factorial of a number. Using this function find the value of nCr.
Program:-

#include<iostream>
using namespace std;
int fact(int k)
{
int i,f=1;
for(i=1;i<=k;i++)
{
f=f*i;
}
return f;
}
int main()
{
int n,r,ncr;
cout<<"Enter N and R";
cin>>n>>r;
ncr=fact(n)/fact(r)* fact(n-r);
cout<<n<<"C"<<r<<"="<<ncr;
}

5)Create a structure to represent admission number, name and marks given for CE, PE and
TE of a subject. Input the details of a student and display admission number, name and total
marks obtained.

Program:-

#include <iostream>
using namespace std;
struct student {
char name[50];
int admsnno;
float ce;
float pe;
float te;
float total;

};

int main() {
student s;
cout << "Enter name of student: ";
cin.getline(s.name, 50);
cout<<"Enter admission number: ";
cin >>s.admsnno;
cout<<"Enter ce mark : ";
cin>>s.ce;
cout<<"Enter pe mark : ";
cin >>s.pe;
cout<<"Enter te mark : ";
cin >>s.te;
s.total=s.ce+s.pe+s.te;
cout << "\n*** Student Details ***" << endl;
cout << "Name : " << s.name <<"\n" << "Admission Number: " << s.admsnno << "\n";
cout << "Total marks: " <<s.total << "\n";
return 0;
}

6)Input two numbers and swap them by defining a function with pointer arguments.

Program:-

#include<iostream>
using namespace std;

void swapValue(int *a,int *b);

int main(){

int a;
int b;

cout<<"Enter first number ";


cin>>a;
cout<<"Enter second number ";
cin>>b;

swapValue(&a,&b);

cout<<"After swap function \n";


cout<<"a= "<<a<<", b="<<b;
return 0;
}

void swapValue(int *x,int *y){

int temp;
temp = *x;
*x=*y;
*y=temp;

8) Input a digit and display the same in word. (Zero for 0, One for 1,...., Nine for 9)

Program:-

#include <iostream>
using namespace std;
int main()
{
int n, num = 0, i;
cout << "\n\n Print a number in words:\n";
cout << "-----------------------------\n";
cout << " Input any number: ";
cin >> n;
while (n != 0) {
num = (num * 10) + (n % 10);
n /= 10;
}
for (i = num; i > 0; i = i / 10) {

switch (i % 10) {
case 0:
cout << "Zero ";
break;
case 1:
cout << "One ";
break;
case 2:
cout << "Two ";
break;
case 3:
cout << "Three ";
break;
case 4:
cout << "Four ";
break;
case 5:
cout << "Five ";
break;
case 6:
cout << "Six ";
break;
case 7:
cout << "Seven ";
break;
case 8:
cout << "Eight ";
break;
case 9:
cout << "Nine ";
break;
}
}
cout << endl;
}

9). Find the sum of the squares of the first N natural numbers without using formula

Program:-

#include<iostream>
using namespace std;

int main() {
int sum = 0, n = 5;

for (int i = 1; i <= n; i++)


{

sum += i * i;
}
cout << "The sum of squares of first " << n << " numbers is: " << sum << endl;
return 0;
}

10)Input an integer number and check whether it is palindrome or not

Program:-

#include <iostream>
using namespace std;
int main()
{
int n, num, digit, rev = 0;

cout << "Enter a positive number: ";


cin >> num;

n = num;

do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);

cout << " The reverse of the number is: " << rev << endl;

if (n == rev)
cout << " The number is a palindrome";
else
cout << " The number is not a palindrome.";

return 0;

You might also like