Addn.c - Read A Positive Number N. Then Read N Integers and Print Them Out Together With Their Sum.
Addn.c - Read A Positive Number N. Then Read N Integers and Print Them Out Together With Their Sum.
int main(void) {
int n; /* The number of numbers to be read */
int sum; /* The sum of numbers already read */
int current; /* The number just read */
int lcv; /* Loop control variable, it counts the number
of numbers already read */
=========================================================================
#include <stdio.h>
#define SENTINEL 0
int main(void) {
int sum = 0; /* The sum of numbers already read */
int current; /* The number just read */
do {
printf("\nEnter an integer > ");
scanf("%d", ¤t);
if (current > SENTINEL)
sum = sum + current;
} while (current > SENTINEL);
printf("\nThe sum is %d\n", sum);
}
#include <stdio.h>
#define N 16
int main(void) {
int n; /* The current exponent */
int val = 1; /* The current power of 2 */
printf("\t n \t 2^n\n");
printf("\t================\n");
for (n=0; n<=N; n++) {
printf("\t%3d \t %6d\n", n, val);
val = 2*val;
}
return 0;
}
/* It prints out :
n 2^n
================
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 4096
13 8192
14 16384
15 32768
16 65536
*/
=====================================================================
/* homework1.c -- This is how the code for the first homework
* appears when we have a single block letter.
* In our Unix system you can compile, link,
* load, and run this program with the commands
* % cc homework1.c
* % a.out
*/
#include <stdio.h>
/* It prints out:
gggggg
g g
g
g ggg
g g
gggggg
*/
/* FILE: coins.c
* DETERMINES THE VALUE OF A COIN COLLECTION
* A Variation of the Hanly/Koffman book's example
*/
#include <stdio.h>
void main ()
{
// Local data ...
int pennies; // input: count of pennies
int nickels; // input: count of nickels
int dimes; // input: count of dimes
int quarters; // input: count of quarters
int temp, left; // temporaries for various
// computations
int main(void) {
int current;
int main(void) {
int n;
int i;
int flag;
if (flag)
printf("%d is prime\n", n);
else
printf("%d has %d as a factor\n", n, i);
return 0;
}
/* factor1.c -- It prompts the user to enter an integer
N. It prints out
* if it is a prime or not. If not, it prints
out all of its
* proper factors.
*/
#include <stdio.h>
int main(void) {
int n,
lcv,
flag; /* flag initially is 1 and becomes 0 if we determine that n
is not a prime */
int main(void) {
printf("The value of 1<2 is %d\n", (1<2));
printf("The value of 2<1 is %d\n", (2<1));
}
*/
============================================================================/*
fibo.c -- It prints out the first N Fibonacci
* numbers.
*/
#include <stdio.h>
int main(void) {
int n; /* The number of fibonacci numbers we will print */
int i; /* The index of fibonacci number to be printed next */
int current; /* The value of the (i)th fibonacci number */
int next; /* The value of the (i+1)th fibonacci number */
int twoaway; /* The value of the (i+2)th fibonacci number */
I Fibonacci(I)
=====================
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
*/
/* funcs.c -- Examples of function declarations,
definitions, and use
*/
#include <stdio.h>
int area(int b, int h); /* Example of a function with two input parameters
and with integer return value */
*/
#include <stdio.h>
int getmax(int a, int b, int c); /*It returns value of largest of a, b, c*/
x = getint();
y = getint();
z = getint();
printf("The largest of %d, %d, and %d is %d\n", x, y, z, getmax(x,y,z));
}
int getint(void) {
int a;
if (m<b)
m = b;
if (m<c)
m = c;
return(m);
}
/*scope1.c -- Simple example showing effects of the
scope rules
*/
#include <stdio.h>
void foo(void);
int main(void) {
int a=2; /* This is a variable local to main */
int b=3; /* This is a variable local to main */
void foo(void){
int b=4; /* This is a variable local to foo */
/* The output is
moo : x = 5, *y = 6, y = 1073742056, z = 3
main: x = 21, y = 6, z = 4
*/
/* array.c -- Operations on arrays
*/
#include <stdio.h>
int main(void) {
int a[2] = {1,2}; /* The aggregates like {1,2} are literals for arrays */
int b[2] = {2,3};
int i;
#include <stdio.h>
#define N 10
void oneWay(void);
void anotherWay(void);
int main(void) {
printf("\noneWay:\n");
oneWay();
printf("\nantherWay:\n");
anotherWay();
}
oneWay:
i = 0 vect[i] = 1
i = 1 vect[i] = 2
i = 2 vect[i] = 3
i = 3 vect[i] = 4
i = 4 vect[i] = 5
i = 5 vect[i] = 6
i = 6 vect[i] = 7
i = 7 vect[i] = 8
i = 8 vect[i] = 9
i = 9 vect[i] = 0
antherWay:
i = 0 vect[i] = 1
i = 1 vect[i] = 2
i = 2 vect[i] = 3
i = 3 vect[i] = 4
i = 4 vect[i] = 5
i = 5 vect[i] = 6
i = 6 vect[i] = 7
i = 7 vect[i] = 8
i = 8 vect[i] = 9
i = 9 vect[i] = 10
*/
/* array2.c -- Read/writing/reversing integer arrays
*/
#include <stdio.h>
#define NMAX 10
int main(void) {
int x[NMAX];
int hmny;
do {
printf("Enter integer [%d to terminate] : ", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full\n");
else
a[n++] = temp;
}while (1);
return n;
}
for(i=0;i<n/2;i++){
intSwap(&a[i],&a[n-i-1]);
}
}
#include <stdio.h>
int main(void) {
int answer;
short x = 1;
long y = 2;
float u = 3.0;
double v = 4.4;
long double w = 5.54;
char c = 'p';;
/*
The output from a run was:
Date : Feb 11 1997
Time : 13:51:31
File : white.c
Line : 20
Enter 1 or 0 : 1
You sayd YES
The size of int 4
The size of short 2
The size of long 8
The size of float 4
The size of double 8
The size of long double 8
The size of char 1
*/
/* addresses.c -- Playing with addresses of variables and their contents:
* what is done by C with variables, addresses, and values.
*/
#include <stdio.h>
int main(void) {
int x;
int *y;
x=1;
y=&x;
printf("Address of x = %d, value of x = %d\n", &x, x);
printf("Address of y = %d, value of y = %d, value of *y = %d\n", &y, y, *y);
moo(9,y);
}
*/
int main(void){
int c;
printf("\tCharacter Code\n"
"\t===============\n");
for (c=32; c<127; c++)
printf("\t %c %4d\n", c, c);
}
Character Code
===============
32
! 33
" 34
# 35
$ 36
% 37
& 38
' 39
( 40
) 41
* 42
+ 43
, 44
- 45
. 46
/ 47
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
: 58
; 59
< 60
= 61
> 62
? 63
@ 64
A 65
B 66
C 67
D 68
E 69
F 70
G 71
H 72
I 73
J 74
K 75
L 76
M 77
N 78
O 79
P 80
Q 81
R 82
S 83
T 84
U 85
V 86
W 87
X 88
Y 89
Z 90
[ 91
\ 92
] 93
^ 94
_ 95
` 96
a 97
b 98
c 99
d 100
e 101
f 102
g 103
h 104
i 105
j 106
k 107
l 108
m 109
n 110
o 111
p 112
q 113
r 114
s 115
t 116
u 117
v 118
w 119
x 120
y 121
z 122
{ 123
| 124
} 125
~ 126
*/
/* Generating random number sequences using the formula
(linear congruence)
x[k+1] = (a*x[k] + c)mod m
where a, c, and m are parameters set by the user and
passed as command line
parameters together with a seed i.e. x[0]
As a simple example try a=7, c=1, m=13, and seed=5
A more sophisticated selection would be a=69069,
c=0,
m=2^32=4294967296, and seed=31
It will print out, in a sort of random order, up to
m-1 distinct values.
Then it loops.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void random_init(long s) {
if (s != 0) seed = s;
}
long random() {
seed = (a*seed + c)%m;
return seed;
}
int main(void) {
int limit = 6;
int *a;
int k;
// Print 7 permutations
for (k = 0; k < 7; k++) {
a = rpermute(limit);
printarray(limit, a);
printf("\n");
}
return 0;
}
#include <stdio.h>
int main(void) {
char c;
int count;
for(;;){
count=0;
printf("Please enter a line [blank line to terminate]> ");
do{
c=getchar();
putchar(c);
count++;
}while (c!='\n');
if(count==1)break;
}
}
#include <stdio.h>
#define NMAX 10
int main(void) {
int x[NMAX];
int hmny;
int who;
int where;
do {
printf("Enter integer [%d to terminate] : ", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full\n");
else
a[n++] = temp;
}while (1);
return n;
}
#include <stdio.h>
#define MAXN 8
getarray(table, &howmany);
if (howmany==0) {
printf("Sorry, you entered the null sequence. Good bye.\n");
}else {
do {
printarray(table,howmany);
printf("By how much do you want to shift[0 to terminate]? ");
scanf("%d",&amount);
if (amount!=0)
shift(table,howmany,amount);
}while(amount!=0);
}
}
if (m<0)
m = n-(abs(m)%n);
for (lcv=0;lcv<n;lcv++)
temp[(lcv+m)%n] = a[lcv];
for (lcv=0; lcv<n;lcv++)
a[lcv]=temp[lcv];
}
do {
answer = getint();
if (answer>0 && (i<MAXN))
a[i++]=answer;
}while(answer>0 && (i<MAXN));
*n = i;
}
for (lcv=0;lcv<n;lcv++){
printf(" %d",a[lcv]);
}
printf("\n");
}
#include <stdio.h>
int main(void) {
int n;
int i,j;
int flag;
int primes[NPRIMES]; /*It will contain the primes smaller than n
*that we have already encountered*/
int level; /*1+Number of primes currently in PRIMES*/
/*Introduction*/
printf("Enter value of N > ");
scanf("%d",&n);
level = 0;
/*Main body*/
for(i=2;i<=n;i++) {
for(j = 0, flag = TRUE; j<level && flag; j++)
flag = (i%primes[j]);
if (flag) { /*I is a prime */
printf("%12d\n", i);
if (level < NPRIMES)
primes[level++] = i;
}
}
}
/* string1.c -- Simple string operations
String literals.
printf, scanf, %s, %c
strlen
strcpy
strcmp
*/
#include <stdio.h>
#define MAXBUFF 128
int main(void) {
char c[] = "012345";
char line[MAXBUFF];
int lcv;
int cmp;
sizeof(c)= 7
sizeof(line)= 128
c[lcv]= 48 = 0
c[lcv]= 49 = 1
c[lcv]= 50 = 2
c[lcv]= 51 = 3
c[lcv]= 52 = 4
c[lcv]= 53 = 5
c[lcv]= 0 =
Please enter a string : roses are red
strlen(line) = 5
line = [roses]
012345 is less than roses
012345 is equal to 012345
int main(void){
int len;
char buffer[MAXBUF];
while(1){
len = getline(buffer, MAXBUF);
if (len==0)break;
printf("len = %d, line = %s\n", len, buffer);
};
}
len = 0;
printf("Enter a string [CR to exit]: ");
while(((c=getchar())!='\n') && len<nmax-1)
line[len++]=c;
line[len]='\0';
return len;
}
/* string2.c -- Compacting sequences of spaces in a
string.
We use two different methods
*/
#include <stdio.h>
#define MAXBUFF 128
int main(void) {
char buffer1[MAXBUFF];
char buffer2[MAXBUFF];
int len;
len=compact1(buffer1);
printf("compact1: len=%d, %s\n",len, buffer1);
len=compact2(buffer2);
printf("compact2: len=%d, %s\n",len, buffer2);
}
len = 0;
printf("Enter a string [CR to exit]: ");
while(((c=getchar())!='\n') && len<nmax-1)
line[len++]=c;
line[len]='\0';
return len;
}
if(line[cursor]=='\0')
return 0;
do{
if((line[cursor]==' ')&&prevspace){
/*If we have a space preceded by a space, move rest of string
left one position */
for(lcv=cursor;line[lcv];lcv++)
line[lcv]=line[lcv+1];
}else
prevspace=(line[cursor++]==' ');
}while(line[cursor]);
return cursor;
}
do{
if(!((line[cursor]==' ')&&prevspace)){
line[lcv++]=line[cursor];
prevspace=(line[cursor]==' ');
}
}while(line[cursor++]);
return(lcv-1); /*We need the -1 since it counts also the '\0' */
}
#include <stdio.h>
if(argc!=2){
printf("Usage: %s filename\n", argv[0]);
exit(0);
}
if((fp=fopen(argv[1],"r"))==NULL){
perror("fopen");
exit(0);
}
nchars=nwords=nlines=lastnblank=0;
while((c=getc(fp))!=EOF){
nchars++;
if (c=='\n'){
if (lastnblank)
nwords++;
printf("words=%d, characters=%d\n", nwords, nchars);
nchars=nwords=lastnblank=0;
nlines++;
}else{
if (((c==' ')||(c=='\t'))&(lastnblank))
nwords++;
lastnblank=((c!=' ')&&(c!='\t'));
}
}
printf("lines=%d\n", nlines);
fclose(fp);
}
/* cpfile.c -- Similar to Unix's cp command.
* This program will be called with two
parameters,
* the names of two files. It copies the
first to the second.
*/
#include <stdio.h>
#include <stdlib.h>
if (argc!=3){
printf("Usage: %s filein fileout\n", argv[0]);
exit(0);
}
if ((fin=fopen(argv[1],"r"))==NULL){
perror("fopen filein");
exit(0);
}
if ((fout=fopen(argv[2],"w"))==NULL){
perror("fopen fileout");
exit(0);
}
while ((c=getc(fin))!=EOF)
putc(c,fout);
fclose(fin);
fclose(fout);
return 0;
}
GCm
#include<stdio.h>
void main()
{
int a,b,c,d,i,gcd;
printf("Enter Number 1");
scanf("%d",a);
printf("Enter Number 2");
scanf("%d",b);
printf("Enter Number 3");
scanf("%d",c);
printf("Enter Number 4");
scanf("%d",d);
for(i=1;i++;)
{
if((n%a==0)&&(n%b==0)&&(n%c==0)&&(n%d==0)
{break;}
)
lcm = ((a*b*c*d)/i);
}
……………………………………………………………………………………………………………………………………………..
#include <stdio.h>
#include <conio.h>
#include <dos.h>
void main()
{
int i=1,j,f;
clrscr();
while(i<=1000)
{
j=1;
f=0;
while(j<=i)
{
if(i%j==0)
f++;
j++;
}
if(f<=2)
printf("\n\t\t\t %d",i);
i++;
delay(100);
}
getch();
*****************************************************************************
#include <stdio.h>
#include <conio.h>
void main()
{
int n,f=0;
clrscr();
printf("\n\n\n\t\t\t Program for generating a sequence");
printf("\n\n\n\t\t\t Enter any no. = ");
scanf("%d",&n);
if(n<1)
{
printf("\n\n\t\t\t Error !");
exit(0);
}
else
{
while(n!=1)
{
if(n%2==1)
{
n=n*3+1;
f++;
}
else
{
n=n/2;
f++;
}
printf("\n\n\t\t\t Next value %d",n);
}
printf("\n\n\t\t\t Final value %d, Operation performed %d",n,f);
}
getch();
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
#include <stdio.h>
#include <conio.h>
void main()
{
int n,r,res=0,t;
clrscr();
printf("\n\n\n\t\t\t Program for calculating an Armstrong
number");
while(n>0)
{
r=n%10;
res=res+r*r*r;
n=n/10;
}
if(t==res)
printf("\n\n\n\t\t\t %d is an Armstrong number.",t);
else
printf("\n\n\n\t\t\t %d is Not an Armstrong number.",t);
getch();
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
while(n)
{
f=fibonacci();
printf("\n\t\t\t %d",f);
n--;
}
getch();
}
int fibonacci()
{
static a=0,b=1,c=0;
c=a+b;
b=a;
a=c;
return(c);
#include <stdio.h>
#include <conio.h>
void main()
{
char str[80],ch;
int len=0,i=0;
clrscr();
printf("\n\n\n\t\t\t Program for Reversing a line of text");
printf("\n\n\n\t\t\t Enter a line of Text:");
printf("\n\t >> ");
fflush(stdin);
scanf("%[^'\n']",str);
i=0;
while(str[i]!='\0')
{
len++;
i++;
}
len=len-1;
printf("\n\n\t Reverse = ");
for(i=len;i>=0;i--)
{
ch=str[i];
if(ch>=97 && ch<=123)
ch=ch-32;
printf("%c",ch);
}
getch();
}
#include <stdio.h>
#include <conio.h>
void main()
{
char str[80],ch;
int len=0,i=0;
clrscr();
printf("\n\n\n\t\t\t Program for Reversing a line of text");
printf("\n\n\n\t\t\t Enter a line of Text:");
printf("\n\t >> ");
fflush(stdin);
scanf("%[^'\n']",str);
i=0;
while(str[i]!='\0')
{
len++;
i++;
}
len=len-1;
printf("\n\n\t Reverse = ");
for(i=len;i>=0;i--)
{
ch=str[i];
if(ch>=97 && ch<=123)
ch=ch-32;
printf("%c",ch);
}
getch();
}
________________________________________________________________________-
#include <stdio.h>
#include <conio.h>
long int factorial(int n);
void main()
{
int n,i;
float s,r;
char c;
clrscr();
repeat : printf("
#include <stdio.h>
#include <conio.h>
void main()
{
int n,m,k,i,max;
char c;
clrscr();
repeat: max=0;
k=2;
n=1;
printf("You want prime numbers upto:- ");
scanf("%d",&max);
printf(" ");
for (i=1;i<=max;i++)
{
again: m=(n/k)*k;
if (m!=n)
k=k+1;
else
goto try1;
if (k < n/2)
goto again;
else
printf("%d",n);
printf(" ");
try1: n=n+1;
k=2;
}
fflush(stdin);
printf ("Do you want to continue?(y/n):- ");
scanf("%c",&c);
if (c=='y')
goto repeat;
getch();}